1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

Chore: Refactor EventManager to use stronger types (#10272)

This commit is contained in:
Henry Heino
2024-04-06 10:08:16 -07:00
committed by GitHub
parent 9713034f18
commit 58ca1a938b
7 changed files with 79 additions and 67 deletions

View File

@@ -1,6 +1,6 @@
const fastDeepEqual = require('fast-deep-equal');
const events = require('events');
import { EventEmitter } from 'events';
import type { State as AppState } from './reducer';
export enum EventName {
ResourceCreate = 'resourceCreate',
@@ -20,30 +20,34 @@ export enum EventName {
NoteResourceIndexed = 'noteResourceIndexed',
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
export type EventListenerCallback = (...args: any[])=> void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
type AppStateChangeCallback = (event: { value: any })=> void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
type FilterObject = any;
export type FilterHandler = (object: FilterObject)=> FilterObject;
export class EventManager {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private emitter_: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private appStatePrevious_: any;
private emitter_: EventEmitter;
private appStatePrevious_: Record<string, AppState[keyof AppState]>;
private appStateWatchedProps_: string[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private appStateListeners_: any;
private appStateListeners_: Record<string, AppStateChangeCallback[]>;
public constructor() {
this.reset();
}
public reset() {
this.emitter_ = new events.EventEmitter();
this.emitter_ = new EventEmitter();
this.appStatePrevious_ = {};
this.appStateWatchedProps_ = [];
this.appStateListeners_ = {};
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public on(eventName: EventName, callback: Function) {
public on(eventName: EventName, callback: EventListenerCallback) {
return this.emitter_.on(eventName, callback);
}
@@ -52,23 +56,19 @@ export class EventManager {
return this.emitter_.emit(eventName, object);
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public removeListener(eventName: string, callback: Function) {
public removeListener(eventName: string, callback: EventListenerCallback) {
return this.emitter_.removeListener(eventName, callback);
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public off(eventName: EventName, callback: Function) {
public off(eventName: EventName, callback: EventListenerCallback) {
return this.removeListener(eventName, callback);
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public filterOn(filterName: string, callback: Function) {
public filterOn(filterName: string, callback: FilterHandler) {
return this.emitter_.on(`filter:${filterName}`, callback);
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public filterOff(filterName: string, callback: Function) {
public filterOff(filterName: string, callback: FilterHandler) {
return this.removeListener(`filter:${filterName}`, callback);
}
@@ -95,8 +95,7 @@ export class EventManager {
return output;
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public appStateOn(propName: string, callback: Function) {
public appStateOn(propName: string, callback: AppStateChangeCallback) {
if (!this.appStateListeners_[propName]) {
this.appStateListeners_[propName] = [];
this.appStateWatchedProps_.push(propName);
@@ -105,8 +104,7 @@ export class EventManager {
this.appStateListeners_[propName].push(callback);
}
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public appStateOff(propName: string, callback: Function) {
public appStateOff(propName: string, callback: AppStateChangeCallback) {
if (!this.appStateListeners_[propName]) {
throw new Error('EventManager: Trying to unregister a state prop watch for a non-watched prop (1)');
}
@@ -117,10 +115,10 @@ export class EventManager {
this.appStateListeners_[propName].splice(idx, 1);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private stateValue_(state: any, propName: string) {
private stateValue_(state: AppState, propName: string) {
const parts = propName.split('.');
let s = state;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partially refactored old code from before rule was applied.
let s: any = state;
for (const p of parts) {
if (!(p in s)) throw new Error(`Invalid state property path: ${propName}`);
s = s[p];
@@ -131,8 +129,7 @@ export class EventManager {
// 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.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public appStateEmit(state: any) {
public appStateEmit(state: AppState) {
if (!this.appStateWatchedProps_.length) return;
for (const propName of this.appStateWatchedProps_) {
@@ -157,8 +154,7 @@ export class EventManager {
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public once(eventName: string, callback: any) {
public once(eventName: string, callback: EventListenerCallback) {
return this.emitter_.once(eventName, callback);
}