1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-20 20:55:18 +02:00
joplin/packages/fork-htmlparser2/src/CollectingHandler.ts

39 lines
941 B
TypeScript
Raw Normal View History

2020-11-05 16:58:23 +00:00
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);
}
}
}