mirror of
https://github.com/laurent22/joplin.git
synced 2025-03-17 20:48:11 +02:00
39 lines
941 B
TypeScript
39 lines
941 B
TypeScript
|
import MultiplexHandler from "./MultiplexHandler";
|
||
|
import { Handler } from "./Parser";
|
||
|
|
||
|
export class CollectingHandler extends MultiplexHandler {
|
||
|
_cbs: Partial<Handler>;
|
||
|
events: [keyof Handler, ...unknown[]][];
|
||
|
|
||
|
constructor(cbs: Partial<Handler> = {}) {
|
||
|
super((name, ...args) => {
|
||
|
this.events.push([name, ...args]);
|
||
|
// @ts-ignore
|
||
|
if (this._cbs[name]) this._cbs[name](...args);
|
||
|
});
|
||
|
|
||
|
this._cbs = cbs;
|
||
|
this.events = [];
|
||
|
}
|
||
|
|
||
|
onreset() {
|
||
|
this.events = [];
|
||
|
if (this._cbs.onreset) this._cbs.onreset();
|
||
|
}
|
||
|
|
||
|
restart() {
|
||
|
if (this._cbs.onreset) this._cbs.onreset();
|
||
|
|
||
|
for (let i = 0; i < this.events.length; i++) {
|
||
|
const [name, ...args] = this.events[i];
|
||
|
|
||
|
if (!this._cbs[name]) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// @ts-ignore
|
||
|
this._cbs[name](...args);
|
||
|
}
|
||
|
}
|
||
|
}
|