mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-24 08:12:24 +02:00
Chore: Strengthen eventManager
types (#10505)
This commit is contained in:
parent
300d0e3ca5
commit
97274c95a5
@ -77,8 +77,8 @@ export default class NoteListUtils {
|
|||||||
const note = await Note.load(noteIds[i]);
|
const note = await Note.load(noteIds[i]);
|
||||||
const newNote = Note.changeNoteType(note, type);
|
const newNote = Note.changeNoteType(note, type);
|
||||||
if (newNote === note) continue;
|
if (newNote === note) continue;
|
||||||
await Note.save(newNote, { userSideValidation: true });
|
const savedNote = await Note.save(newNote, { userSideValidation: true });
|
||||||
eventManager.emit(EventName.NoteTypeToggle, { noteId: note.id });
|
eventManager.emit(EventName.NoteTypeToggle, { noteId: note.id, note: savedNote });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
const fastDeepEqual = require('fast-deep-equal');
|
const fastDeepEqual = require('fast-deep-equal');
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import type { State as AppState } from './reducer';
|
import type { State as AppState } from './reducer';
|
||||||
|
import { ModelType } from './BaseModel';
|
||||||
|
import { NoteEntity } from './services/database/types';
|
||||||
|
|
||||||
export enum EventName {
|
export enum EventName {
|
||||||
ResourceCreate = 'resourceCreate',
|
ResourceCreate = 'resourceCreate',
|
||||||
@ -20,8 +22,60 @@ export enum EventName {
|
|||||||
NoteResourceIndexed = 'noteResourceIndexed',
|
NoteResourceIndexed = 'noteResourceIndexed',
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
|
interface ItemChangeEvent {
|
||||||
export type EventListenerCallback = (...args: any[])=> void;
|
itemType: ModelType;
|
||||||
|
itemId: string;
|
||||||
|
eventType: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SyncCompleteEvent {
|
||||||
|
withErrors: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ResourceChangeEvent {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NoteContentChangeEvent {
|
||||||
|
note: NoteEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NoteAlarmTriggerEvent {
|
||||||
|
noteId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SettingsChangeEvent {
|
||||||
|
keys: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NoteChangeEvent {
|
||||||
|
noteId: string;
|
||||||
|
note: NoteEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventArgs = {
|
||||||
|
[EventName.ResourceCreate]: [];
|
||||||
|
[EventName.ResourceChange]: [ResourceChangeEvent];
|
||||||
|
[EventName.SettingsChange]: [SettingsChangeEvent];
|
||||||
|
[EventName.TodoToggle]: [];
|
||||||
|
[EventName.NoteTypeToggle]: [NoteChangeEvent];
|
||||||
|
[EventName.SyncStart]: [];
|
||||||
|
[EventName.SessionEstablished]: [];
|
||||||
|
[EventName.SyncComplete]: [SyncCompleteEvent];
|
||||||
|
[EventName.ItemChange]: [ItemChangeEvent];
|
||||||
|
[EventName.NoteAlarmTrigger]: [NoteAlarmTriggerEvent];
|
||||||
|
[EventName.AlarmChange]: [NoteChangeEvent];
|
||||||
|
[EventName.KeymapChange]: [];
|
||||||
|
[EventName.NoteContentChange]: [NoteContentChangeEvent];
|
||||||
|
[EventName.OcrServiceResourcesProcessed]: [];
|
||||||
|
[EventName.NoteResourceIndexed]: [];
|
||||||
|
};
|
||||||
|
|
||||||
|
type EventListenerCallbacks = {
|
||||||
|
[n in EventName]: (...args: EventArgs[n])=> void;
|
||||||
|
};
|
||||||
|
export type EventListenerCallback<Name extends EventName> = EventListenerCallbacks[Name];
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
|
// 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;
|
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
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
|
||||||
@ -47,20 +101,19 @@ export class EventManager {
|
|||||||
this.appStateListeners_ = {};
|
this.appStateListeners_ = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
public on(eventName: EventName, callback: EventListenerCallback) {
|
public on<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
return this.emitter_.on(eventName, callback);
|
return this.emitter_.on(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
public emit<Name extends EventName>(eventName: Name, ...args: EventArgs[Name]) {
|
||||||
public emit(eventName: EventName, object: any = null) {
|
return this.emitter_.emit(eventName, ...args);
|
||||||
return this.emitter_.emit(eventName, object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeListener(eventName: string, callback: EventListenerCallback) {
|
public removeListener<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
return this.emitter_.removeListener(eventName, callback);
|
return this.emitter_.removeListener(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public off(eventName: EventName, callback: EventListenerCallback) {
|
public off<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
return this.removeListener(eventName, callback);
|
return this.removeListener(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +122,7 @@ export class EventManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public filterOff(filterName: string, callback: FilterHandler) {
|
public filterOff(filterName: string, callback: FilterHandler) {
|
||||||
return this.removeListener(`filter:${filterName}`, callback);
|
return this.emitter_.off(`filter:${filterName}`, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
||||||
@ -154,7 +207,7 @@ export class EventManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public once(eventName: string, callback: EventListenerCallback) {
|
public once<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
return this.emitter_.once(eventName, callback);
|
return this.emitter_.once(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,11 +124,11 @@ export default class CommandService extends BaseService {
|
|||||||
this.stateToWhenClauseContext_ = stateToWhenClauseContext;
|
this.stateToWhenClauseContext_ = stateToWhenClauseContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public on(eventName: EventName, callback: EventListenerCallback) {
|
public on<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
eventManager.on(eventName, callback);
|
eventManager.on(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public off(eventName: EventName, callback: EventListenerCallback) {
|
public off<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
eventManager.off(eventName, callback);
|
eventManager.off(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,11 +416,11 @@ export default class KeymapService extends BaseService {
|
|||||||
return parts.join('+');
|
return parts.join('+');
|
||||||
}
|
}
|
||||||
|
|
||||||
public on(eventName: EventName, callback: EventListenerCallback) {
|
public on<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
eventManager.on(eventName, callback);
|
eventManager.on(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public off(eventName: EventName, callback: EventListenerCallback) {
|
public off<Name extends EventName>(eventName: Name, callback: EventListenerCallback<Name>) {
|
||||||
eventManager.off(eventName, callback);
|
eventManager.off(eventName, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,12 +35,6 @@ interface ItemChangeEvent {
|
|||||||
event: ItemChangeEventType;
|
event: ItemChangeEventType;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SyncStartEvent {
|
|
||||||
// Tells whether there were errors during sync or not. The log will
|
|
||||||
// have the complete information about any error.
|
|
||||||
withErrors: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ResourceChangeEvent {
|
interface ResourceChangeEvent {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
@ -59,13 +53,15 @@ interface NoteAlarmTriggerEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface SyncCompleteEvent {
|
interface SyncCompleteEvent {
|
||||||
|
// Tells whether there were errors during sync or not. The log will
|
||||||
|
// have the complete information about any error.
|
||||||
withErrors: boolean;
|
withErrors: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkspaceEventHandler<EventType> = (event: EventType)=> void;
|
type WorkspaceEventHandler<EventType> = (event: EventType)=> void;
|
||||||
|
|
||||||
type ItemChangeHandler = WorkspaceEventHandler<ItemChangeEvent>;
|
type ItemChangeHandler = WorkspaceEventHandler<ItemChangeEvent>;
|
||||||
type SyncStartHandler = WorkspaceEventHandler<SyncStartEvent>;
|
type SyncStartHandler = ()=> void;
|
||||||
type ResourceChangeHandler = WorkspaceEventHandler<ResourceChangeEvent>;
|
type ResourceChangeHandler = WorkspaceEventHandler<ResourceChangeEvent>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,8 +2,8 @@ import Plugin from '../Plugin';
|
|||||||
import { EventListenerCallback, EventManager, EventName } from '../../../eventManager';
|
import { EventListenerCallback, EventManager, EventName } from '../../../eventManager';
|
||||||
import { Disposable } from '../api/types';
|
import { Disposable } from '../api/types';
|
||||||
|
|
||||||
export default function(
|
export default function<Name extends EventName>(
|
||||||
plugin: Plugin, eventManager: EventManager, eventName: EventName, callback: EventListenerCallback,
|
plugin: Plugin, eventManager: EventManager, eventName: Name, callback: EventListenerCallback<Name>,
|
||||||
): Disposable {
|
): Disposable {
|
||||||
eventManager.on(eventName, callback);
|
eventManager.on(eventName, callback);
|
||||||
const dispose = () => {
|
const dispose = () => {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import SyncTargetRegistry from '../SyncTargetRegistry';
|
import SyncTargetRegistry from '../SyncTargetRegistry';
|
||||||
import eventManager from '../eventManager';
|
import eventManager, { EventName } from '../eventManager';
|
||||||
import Setting from '../models/Setting';
|
import Setting from '../models/Setting';
|
||||||
import { reg } from '../registry';
|
import { reg } from '../registry';
|
||||||
import Logger from '@joplin/utils/Logger';
|
import Logger from '@joplin/utils/Logger';
|
||||||
@ -44,7 +44,7 @@ const userFetcher = async () => {
|
|||||||
|
|
||||||
// Listen to the event only once
|
// Listen to the event only once
|
||||||
export const initializeUserFetcher = () => {
|
export const initializeUserFetcher = () => {
|
||||||
eventManager.once('sessionEstablished', userFetcher);
|
eventManager.once(EventName.SessionEstablished, userFetcher);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default userFetcher;
|
export default userFetcher;
|
||||||
|
Loading…
Reference in New Issue
Block a user