New event bus

This commit is contained in:
Mr. Stallion 2021-01-20 14:52:33 -06:00
parent 28527c1840
commit a020e71ad4
3 changed files with 46 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import Vue from 'vue';
// import Vue from 'vue';
import _ from 'lodash';
import { Character } from '../../site/character_page/interfaces';
import { Message } from '../common';
import { Conversation } from '../interfaces';
@ -41,10 +42,43 @@ export interface SelectConversationEvent extends EventBusEvent {
conversation: Conversation;
}
export type EventCallback = (data: any) => void | Promise<void>;
// tslint:disable-next-line no-empty-interface
export interface NoteCountsUpdate extends EventBusEvent, NoteCheckerCount {}
class EventBusManager {
private eventCallbacks: Record<string, EventCallback[]> = {};
export const EventBus = new Vue();
$on(eventName: string, callback: EventCallback): void {
this.$off(eventName, callback);
if (!(eventName in this.eventCallbacks)) {
this.eventCallbacks[eventName] = [];
}
this.eventCallbacks[eventName].push(callback);
}
$off(eventName: string, callback: EventCallback): void {
if (!(eventName in this.eventCallbacks)) {
return;
}
this.eventCallbacks[eventName] = _.filter(
this.eventCallbacks[eventName],
(cb) => (cb !== callback)
);
}
$emit(eventName: string, eventData: EventBusEvent): void {
_.each(this.eventCallbacks[eventName] || [], (cb) => (cb(eventData)));
}
}
export const EventBus = new EventBusManager();
// export const EventBus = new Vue();

View File

@ -86,6 +86,8 @@ export class CacheManager {
this.queue.push(entry);
console.log('Added to queue', entry.name, entry.added.toISOString());
// console.log('AddProfileForFetching', name, this.queue.length);
}
@ -237,9 +239,12 @@ export class CacheManager {
const scheduleNextFetch = () => {
this.profileTimer = setTimeout(
async() => {
const d = Date.now();
const next = this.consumeNextInQueue();
if (next) {
console.log('Next in queue', next.name, (Date.now() - d) / 1000.0);
try {
// tslint:disable-next-line: binary-expression-operand-order
if ((false) && (next)) {
@ -253,6 +258,8 @@ export class CacheManager {
this.queue.push(next); // return to queue
}
console.log('Completed', next.name, (Date.now() - d) / 1000.0);
}
scheduleNextFetch();

View File

@ -61,7 +61,9 @@ export default class NoteStatus extends Vue {
@Hook('beforeDestroy')
destroying(): void {
EventBus.$off('note-counts-update', this.callback);
if (this.callback) {
EventBus.$off('note-counts-update', this.callback);
}
}