feat: add once to EventEmitters (#5818)

This commit is contained in:
Nick Perez 2024-11-08 10:49:50 +01:00 committed by GitHub
parent 62c6dddf80
commit 53673fbfe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
feat: add `once` to EventEmitters

View File

@ -46,6 +46,15 @@ export class EventEmitter<T extends Record<string, any>> {
return this
}
public once<EventName extends StringKeyOf<T>>(event: EventName, fn: CallbackFunction<T, EventName>): this {
const onceFn = (...args: CallbackType<T, EventName>) => {
this.off(event, onceFn)
fn.apply(this, args)
}
return this.on(event, onceFn)
}
public removeAllListeners(): void {
this.callbacks = {}
}