feat: support pen erasing (#7496)

This commit is contained in:
David Luzar 2024-01-01 13:27:03 +01:00 committed by GitHub
parent d19b51d4f8
commit e6c3c06c2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 240 additions and 184 deletions

View file

@ -4,13 +4,6 @@ type Subscriber<T extends any[]> = (...payload: T) => void;
export class Emitter<T extends any[] = []> {
public subscribers: Subscriber<T>[] = [];
public value: T | undefined;
private updateOnChangeOnly: boolean;
constructor(opts?: { initialState?: T; updateOnChangeOnly?: boolean }) {
this.updateOnChangeOnly = opts?.updateOnChangeOnly ?? false;
this.value = opts?.initialState;
}
/**
* Attaches subscriber
@ -45,16 +38,14 @@ export class Emitter<T extends any[] = []> {
);
}
trigger(...payload: T): any[] {
if (this.updateOnChangeOnly && this.value === payload) {
return [];
trigger(...payload: T) {
for (const handler of this.subscribers) {
handler(...payload);
}
this.value = payload;
return this.subscribers.map((handler) => handler(...payload));
return this;
}
destroy() {
clear() {
this.subscribers = [];
this.value = undefined;
}
}