2021-12-27 18:37:50 +02:00
|
|
|
const fastDeepEqual = require('fast-deep-equal');
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const events = require('events');
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
export enum EventName {
|
|
|
|
ResourceCreate = 'resourceCreate',
|
|
|
|
ResourceChange = 'resourceChange',
|
|
|
|
SettingsChange = 'settingsChange',
|
|
|
|
TodoToggle = 'todoToggle',
|
|
|
|
NoteTypeToggle = 'noteTypeToggle',
|
|
|
|
SyncStart = 'syncStart',
|
|
|
|
SessionEstablished = 'sessionEstablished',
|
|
|
|
SyncComplete = 'syncComplete',
|
|
|
|
ItemChange = 'itemChange',
|
|
|
|
NoteAlarmTrigger = 'noteAlarmTrigger',
|
|
|
|
AlarmChange = 'alarmChange',
|
|
|
|
KeymapChange = 'keymapChange',
|
|
|
|
NoteContentChange = 'noteContentChange',
|
|
|
|
OcrServiceResourcesProcessed = 'ocrServiceResourcesProcessed',
|
2024-02-09 13:55:29 +02:00
|
|
|
NoteResourceIndexed = 'noteResourceIndexed',
|
2023-12-13 21:24:58 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:08:41 +02:00
|
|
|
export class EventManager {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private emitter_: any;
|
|
|
|
private appStatePrevious_: any;
|
|
|
|
private appStateWatchedProps_: string[];
|
|
|
|
private appStateListeners_: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor() {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public reset() {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.emitter_ = new events.EventEmitter();
|
|
|
|
|
|
|
|
this.appStatePrevious_ = {};
|
|
|
|
this.appStateWatchedProps_ = [];
|
|
|
|
this.appStateListeners_ = {};
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-12-13 21:24:58 +02:00
|
|
|
public on(eventName: EventName, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.emitter_.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
public emit(eventName: EventName, object: any = null) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.emitter_.emit(eventName, object);
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public removeListener(eventName: string, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.emitter_.removeListener(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-12-13 21:24:58 +02:00
|
|
|
public off(eventName: EventName, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.removeListener(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public filterOn(filterName: string, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.emitter_.on(`filter:${filterName}`, callback);
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public filterOff(filterName: string, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.removeListener(`filter:${filterName}`, callback);
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:37:50 +02:00
|
|
|
public async filterEmit(filterName: string, object: any) {
|
|
|
|
let output = object;
|
2020-10-09 19:35:46 +02:00
|
|
|
const listeners = this.emitter_.listeners(`filter:${filterName}`);
|
|
|
|
for (const listener of listeners) {
|
2021-12-27 18:37:50 +02:00
|
|
|
// When we pass the object to the plugin, it is always going to be
|
|
|
|
// modified since it is serialized/unserialized. So we need to use a
|
|
|
|
// deep equality check to see if it's been changed. Normally the
|
|
|
|
// filter objects should be relatively small so there shouldn't be
|
|
|
|
// much of a performance hit.
|
|
|
|
const newOutput = await listener(output);
|
|
|
|
|
|
|
|
// Plugin didn't return anything - so we leave the object as it is.
|
|
|
|
if (newOutput === undefined) continue;
|
|
|
|
|
|
|
|
if (!fastDeepEqual(newOutput, output)) {
|
|
|
|
output = newOutput;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public appStateOn(propName: string, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!this.appStateListeners_[propName]) {
|
|
|
|
this.appStateListeners_[propName] = [];
|
|
|
|
this.appStateWatchedProps_.push(propName);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.appStateListeners_[propName].push(callback);
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public appStateOff(propName: string, callback: Function) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!this.appStateListeners_[propName]) {
|
|
|
|
throw new Error('EventManager: Trying to unregister a state prop watch for a non-watched prop (1)');
|
|
|
|
}
|
|
|
|
|
|
|
|
const idx = this.appStateListeners_[propName].indexOf(callback);
|
|
|
|
if (idx < 0) throw new Error('EventManager: Trying to unregister a state prop watch for a non-watched prop (2)');
|
|
|
|
|
|
|
|
this.appStateListeners_[propName].splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
private stateValue_(state: any, propName: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
const parts = propName.split('.');
|
|
|
|
let s = state;
|
|
|
|
for (const p of parts) {
|
|
|
|
if (!(p in s)) throw new Error(`Invalid state property path: ${propName}`);
|
|
|
|
s = s[p];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function works by keeping a copy of the watched props and, whenever this function
|
|
|
|
// is called, comparing the previous and new values and emitting events if they have changed.
|
|
|
|
// The appStateEmit function should be called from a middleware.
|
2023-03-06 16:22:01 +02:00
|
|
|
public appStateEmit(state: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!this.appStateWatchedProps_.length) return;
|
|
|
|
|
|
|
|
for (const propName of this.appStateWatchedProps_) {
|
|
|
|
let emit = false;
|
|
|
|
|
|
|
|
const stateValue = this.stateValue_(state, propName);
|
|
|
|
|
|
|
|
if (!(propName in this.appStatePrevious_) || this.appStatePrevious_[propName] !== stateValue) {
|
|
|
|
this.appStatePrevious_[propName] = stateValue;
|
|
|
|
emit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emit) {
|
|
|
|
const listeners = this.appStateListeners_[propName];
|
|
|
|
if (!listeners || !listeners.length) continue;
|
|
|
|
|
|
|
|
const eventValue = Object.freeze(stateValue);
|
|
|
|
for (const listener of listeners) {
|
|
|
|
listener({ value: eventValue });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:15:45 +02:00
|
|
|
public once(eventName: string, callback: any) {
|
|
|
|
return this.emitter_.once(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const eventManager = new EventManager();
|
|
|
|
|
|
|
|
export default eventManager;
|