add PriorityEventEmitter

use PriorityEventEmitter for time api event handling
This commit is contained in:
David Tsay 2024-11-14 10:49:34 -08:00
parent ea9947cab5
commit 860bd06c8f
2 changed files with 27 additions and 3 deletions

View File

@ -20,7 +20,7 @@
* at runtime from the About dialog for additional information.
*****************************************************************************/
import { EventEmitter } from 'eventemitter3';
import PriorityEventEmitter from 'utils/PriorityEventEmitter.js';
import { FIXED_MODE_KEY, MODES, REALTIME_MODE_KEY, TIME_CONTEXT_EVENTS } from './constants.js';
@ -66,9 +66,9 @@ import { FIXED_MODE_KEY, MODES, REALTIME_MODE_KEY, TIME_CONTEXT_EVENTS } from '.
/**
* @class TimeContext
* @extends EventEmitter
* @extends PriorityEventEmitter
*/
class TimeContext extends EventEmitter {
class TimeContext extends PriorityEventEmitter {
constructor() {
super();

View File

@ -0,0 +1,24 @@
import { EventEmitter } from 'eventemitter3';
export default class PriorityEventEmitter extends EventEmitter {
constructor() {
super();
this.listeners = {};
}
on(event, listener, priority = 0) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push({ listener, priority });
this.listeners[event].sort((a, b) => b.priority - a.priority);
super.on(event, listener);
}
emit(event, ...args) {
this.listeners[event]?.forEach(({ listener }) => listener(...args));
}
}