New event bus
This commit is contained in:
		
							parent
							
								
									28527c1840
								
							
						
					
					
						commit
						a020e71ad4
					
				@ -1,4 +1,5 @@
 | 
				
			|||||||
import Vue from 'vue';
 | 
					// import Vue from 'vue';
 | 
				
			||||||
 | 
					import _ from 'lodash';
 | 
				
			||||||
import { Character } from '../../site/character_page/interfaces';
 | 
					import { Character } from '../../site/character_page/interfaces';
 | 
				
			||||||
import { Message } from '../common';
 | 
					import { Message } from '../common';
 | 
				
			||||||
import { Conversation } from '../interfaces';
 | 
					import { Conversation } from '../interfaces';
 | 
				
			||||||
@ -41,10 +42,43 @@ export interface SelectConversationEvent extends EventBusEvent {
 | 
				
			|||||||
    conversation: Conversation;
 | 
					    conversation: Conversation;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type EventCallback = (data: any) => void | Promise<void>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// tslint:disable-next-line no-empty-interface
 | 
					// tslint:disable-next-line no-empty-interface
 | 
				
			||||||
export interface NoteCountsUpdate extends EventBusEvent, NoteCheckerCount {}
 | 
					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();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -86,6 +86,8 @@ export class CacheManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        this.queue.push(entry);
 | 
					        this.queue.push(entry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        console.log('Added to queue', entry.name, entry.added.toISOString());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // console.log('AddProfileForFetching', name, this.queue.length);
 | 
					        // console.log('AddProfileForFetching', name, this.queue.length);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -237,9 +239,12 @@ export class CacheManager {
 | 
				
			|||||||
        const scheduleNextFetch = () => {
 | 
					        const scheduleNextFetch = () => {
 | 
				
			||||||
            this.profileTimer = setTimeout(
 | 
					            this.profileTimer = setTimeout(
 | 
				
			||||||
                async() => {
 | 
					                async() => {
 | 
				
			||||||
 | 
					                    const d = Date.now();
 | 
				
			||||||
                    const next = this.consumeNextInQueue();
 | 
					                    const next = this.consumeNextInQueue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (next) {
 | 
					                    if (next) {
 | 
				
			||||||
 | 
					                        console.log('Next in queue', next.name, (Date.now() - d) / 1000.0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        try {
 | 
					                        try {
 | 
				
			||||||
                            // tslint:disable-next-line: binary-expression-operand-order
 | 
					                            // tslint:disable-next-line: binary-expression-operand-order
 | 
				
			||||||
                            if ((false) && (next)) {
 | 
					                            if ((false) && (next)) {
 | 
				
			||||||
@ -253,6 +258,8 @@ export class CacheManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                            this.queue.push(next); // return to queue
 | 
					                            this.queue.push(next); // return to queue
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        console.log('Completed', next.name, (Date.now() - d) / 1000.0);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    scheduleNextFetch();
 | 
					                    scheduleNextFetch();
 | 
				
			||||||
 | 
				
			|||||||
@ -61,7 +61,9 @@ export default class NoteStatus extends Vue {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @Hook('beforeDestroy')
 | 
					  @Hook('beforeDestroy')
 | 
				
			||||||
  destroying(): void {
 | 
					  destroying(): void {
 | 
				
			||||||
    EventBus.$off('note-counts-update', this.callback);
 | 
					    if (this.callback) {
 | 
				
			||||||
 | 
					      EventBus.$off('note-counts-update', this.callback);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user