1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-14 18:27:44 +02:00
joplin/packages/fork-htmlparser2/src/CollectingHandler.ts
2023-06-30 10:22:47 +01:00

37 lines
907 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]);
if (this._cbs[name]) (this._cbs as any)[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;
}
(this._cbs as any)[name](...args);
}
}
}