diff --git a/chat/preview/event-bus.ts b/chat/preview/event-bus.ts index 537fa71..2d651b4 100644 --- a/chat/preview/event-bus.ts +++ b/chat/preview/event-bus.ts @@ -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; + // tslint:disable-next-line no-empty-interface export interface NoteCountsUpdate extends EventBusEvent, NoteCheckerCount {} +class EventBusManager { + private eventCallbacks: Record = {}; -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(); diff --git a/learn/cache-manager.ts b/learn/cache-manager.ts index 3997f34..7c22e47 100644 --- a/learn/cache-manager.ts +++ b/learn/cache-manager.ts @@ -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(); diff --git a/site/NoteStatus.vue b/site/NoteStatus.vue index 0f8c20b..eea4356 100644 --- a/site/NoteStatus.vue +++ b/site/NoteStatus.vue @@ -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); + } }