feat: Initial clean release of CHM
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
这个提交包含在:
49
packages/lib/EventDispatcher.ts
普通文件
49
packages/lib/EventDispatcher.ts
普通文件
@@ -0,0 +1,49 @@
|
||||
type Listener<Value> = (data: Value)=> void;
|
||||
type CallbackHandler<EventType> = (data: EventType)=> void;
|
||||
|
||||
// EventKeyType is used to distinguish events (e.g. a 'ClickEvent' vs a 'TouchEvent')
|
||||
// while EventMessageType is the type of the data sent with an event (can be `void`)
|
||||
export default class EventDispatcher<EventKeyType extends string|symbol|number, EventMessageType> {
|
||||
// Partial marks all fields as optional. To initialize with an empty object, this is required.
|
||||
// See https://stackoverflow.com/a/64526384
|
||||
private listeners: Partial<Record<EventKeyType, Listener<EventMessageType>[]>>;
|
||||
public constructor() {
|
||||
this.listeners = {};
|
||||
}
|
||||
|
||||
public dispatch(eventName: EventKeyType, event: EventMessageType = null) {
|
||||
if (!this.listeners[eventName]) return;
|
||||
|
||||
const ls = this.listeners[eventName];
|
||||
for (let i = 0; i < ls.length; i++) {
|
||||
ls[i](event);
|
||||
}
|
||||
}
|
||||
|
||||
public on(eventName: EventKeyType, callback: CallbackHandler<EventMessageType>) {
|
||||
if (!this.listeners[eventName]) this.listeners[eventName] = [];
|
||||
this.listeners[eventName].push(callback);
|
||||
|
||||
return {
|
||||
// Returns false if the listener has already been removed, true otherwise.
|
||||
remove: (): boolean => {
|
||||
const originalListeners = this.listeners[eventName];
|
||||
this.off(eventName, callback);
|
||||
|
||||
return originalListeners.length !== this.listeners[eventName].length;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Equivalent to calling .remove() on the object returned by .on
|
||||
public off(eventName: EventKeyType, callback: CallbackHandler<EventMessageType>) {
|
||||
if (!this.listeners[eventName]) return;
|
||||
|
||||
// Replace the current list of listeners with a new, shortened list.
|
||||
// This allows any iterators over this.listeners to continue iterating
|
||||
// without skipping elements.
|
||||
this.listeners[eventName] = this.listeners[eventName].filter(
|
||||
otherCallback => otherCallback !== callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户