diff --git a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx index 389672c0f..2732633d3 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteEditor.tsx @@ -125,8 +125,7 @@ function NoteEditor(props: NoteEditorProps) { return async function() { const note = await formNoteToNote(formNote); reg.logger().debug('Saving note...', note); - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - const savedNote: any = await Note.save(note); + const savedNote = await Note.save(note); setFormNote((prev: FormNote) => { return { ...prev, user_updated_time: savedNote.user_updated_time, hasChanged: false }; diff --git a/packages/lib/eventManager.ts b/packages/lib/eventManager.ts index caab6ffb9..8a4da794d 100644 --- a/packages/lib/eventManager.ts +++ b/packages/lib/eventManager.ts @@ -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; private appStateWatchedProps_: string[]; - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - private appStateListeners_: any; + private appStateListeners_: Record; 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); } diff --git a/packages/lib/services/CommandService.ts b/packages/lib/services/CommandService.ts index 479c0013e..1b91d8692 100644 --- a/packages/lib/services/CommandService.ts +++ b/packages/lib/services/CommandService.ts @@ -1,5 +1,5 @@ import { State } from '../reducer'; -import eventManager, { EventName } from '../eventManager'; +import eventManager, { EventListenerCallback, EventName } from '../eventManager'; import BaseService from './BaseService'; import shim from '../shim'; import WhenClause from './WhenClause'; @@ -57,6 +57,16 @@ export interface Command { runtime?: CommandRuntime; } +interface CommandSpec { + declaration: CommandDeclaration; + runtime: ()=> CommandRuntime; +} + +interface ComponentCommandSpec { + declaration: CommandDeclaration; + runtime: (component: ComponentType)=> CommandRuntime; +} + interface Commands { [key: string]: Command; } @@ -114,13 +124,11 @@ export default class CommandService extends BaseService { this.stateToWhenClauseContext_ = stateToWhenClauseContext; } - // 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) { eventManager.on(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) { eventManager.off(eventName, callback); } @@ -204,29 +212,26 @@ export default class CommandService extends BaseService { command.runtime = runtime; } - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public registerCommands(commands: any[]) { + public registerCommands(commands: CommandSpec[]) { for (const command of commands) { CommandService.instance().registerRuntime(command.declaration.name, command.runtime()); } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public unregisterCommands(commands: any[]) { + public unregisterCommands(commands: CommandSpec[]) { for (const command of commands) { CommandService.instance().unregisterRuntime(command.declaration.name); } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public componentRegisterCommands(component: any, commands: any[]) { + public componentRegisterCommands(component: ComponentType, commands: ComponentCommandSpec[]) { for (const command of commands) { CommandService.instance().registerRuntime(command.declaration.name, command.runtime(component)); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - public componentUnregisterCommands(commands: any[]) { + public componentUnregisterCommands(commands: ComponentCommandSpec[]) { for (const command of commands) { CommandService.instance().unregisterRuntime(command.declaration.name); } diff --git a/packages/lib/services/KeymapService.ts b/packages/lib/services/KeymapService.ts index 602290987..4f529ba20 100644 --- a/packages/lib/services/KeymapService.ts +++ b/packages/lib/services/KeymapService.ts @@ -1,4 +1,4 @@ -import eventManager, { EventName } from '../eventManager'; +import eventManager, { EventListenerCallback, EventName } from '../eventManager'; import shim from '../shim'; import { _ } from '../locale'; import keysRegExp from './KeymapService_keysRegExp'; @@ -415,13 +415,11 @@ export default class KeymapService extends BaseService { return parts.join('+'); } - // 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) { eventManager.on(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) { eventManager.off(eventName, callback); } diff --git a/packages/lib/services/plugins/api/JoplinFilters.ts b/packages/lib/services/plugins/api/JoplinFilters.ts index df47bb54c..65ba69ee6 100644 --- a/packages/lib/services/plugins/api/JoplinFilters.ts +++ b/packages/lib/services/plugins/api/JoplinFilters.ts @@ -1,6 +1,7 @@ /* eslint-disable multiline-comment-style */ -import eventManager from '../../../eventManager'; +import eventManager, { FilterHandler } from '../../../eventManager'; + /** * @ignore @@ -9,13 +10,11 @@ import eventManager from '../../../eventManager'; * so for now disable filters. */ export default class JoplinFilters { - // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async on(name: string, callback: Function) { + public async on(name: string, callback: FilterHandler) { eventManager.filterOn(name, callback); } - // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async off(name: string, callback: Function) { + public async off(name: string, callback: FilterHandler) { eventManager.filterOff(name, callback); } } diff --git a/packages/lib/services/plugins/api/JoplinWorkspace.ts b/packages/lib/services/plugins/api/JoplinWorkspace.ts index 9d1709aa6..32ce7fa8f 100644 --- a/packages/lib/services/plugins/api/JoplinWorkspace.ts +++ b/packages/lib/services/plugins/api/JoplinWorkspace.ts @@ -44,9 +44,28 @@ interface ResourceChangeEvent { id: string; } -type ItemChangeHandler = (event: ItemChangeEvent)=> void; -type SyncStartHandler = (event: SyncStartEvent)=> void; -type ResourceChangeHandler = (event: ResourceChangeEvent)=> void; +interface NoteContentChangeEvent { + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- No plugin-api-accessible Note type defined. + note: any; +} + +interface NoteSelectionChangeEvent { + value: string[]; +} + +interface NoteAlarmTriggerEvent { + noteId: string; +} + +interface SyncCompleteEvent { + withErrors: boolean; +} + +type WorkspaceEventHandler = (event: EventType)=> void; + +type ItemChangeHandler = WorkspaceEventHandler; +type SyncStartHandler = WorkspaceEventHandler; +type ResourceChangeHandler = WorkspaceEventHandler; /** * The workspace service provides access to all the parts of Joplin that @@ -71,7 +90,7 @@ export default class JoplinWorkspace { * Called when a new note or notes are selected. */ // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async onNoteSelectionChange(callback: Function): Promise { + public async onNoteSelectionChange(callback: WorkspaceEventHandler): Promise { eventManager.appStateOn('selectedNoteIds', callback); return {}; @@ -87,8 +106,7 @@ export default class JoplinWorkspace { * Called when the content of a note changes. * @deprecated Use `onNoteChange()` instead, which is reliably triggered whenever the note content, or any note property changes. */ - // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async onNoteContentChange(callback: Function) { + public async onNoteContentChange(callback: WorkspaceEventHandler) { eventManager.on(EventName.NoteContentChange, callback); } @@ -121,8 +139,7 @@ export default class JoplinWorkspace { /** * Called when an alarm associated with a to-do is triggered. */ - // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async onNoteAlarmTrigger(handler: Function): Promise { + public async onNoteAlarmTrigger(handler: WorkspaceEventHandler): Promise { return makeListener(eventManager, EventName.NoteAlarmTrigger, handler); } @@ -136,8 +153,7 @@ export default class JoplinWorkspace { /** * Called when the synchronisation process has finished. */ - // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied - public async onSyncComplete(callback: Function): Promise { + public async onSyncComplete(callback: WorkspaceEventHandler): Promise { return makeListener(eventManager, EventName.SyncComplete, callback); } diff --git a/packages/lib/services/plugins/utils/makeListener.ts b/packages/lib/services/plugins/utils/makeListener.ts index 08b231231..016f33c1a 100644 --- a/packages/lib/services/plugins/utils/makeListener.ts +++ b/packages/lib/services/plugins/utils/makeListener.ts @@ -1,8 +1,7 @@ -import { EventManager, EventName } from '../../../eventManager'; +import { EventListenerCallback, EventManager, EventName } from '../../../eventManager'; import { Disposable } from '../api/types'; -// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied -export default function(eventManager: EventManager, eventName: EventName, callback: Function): Disposable { +export default function(eventManager: EventManager, eventName: EventName, callback: EventListenerCallback): Disposable { eventManager.on(eventName, callback); return {};