-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Description
I suggest adding support for the signal option for each listener and the mergeRegister function, which will accept the AbortSignal object. An abort event subscriber will be added for the passed signal option, and the cleanup function will be called
Example implementation
export interface LexicalListenerOptions {
signal?: AbortSignal;
}
class LexicalEditor {
registerUpdateListener(
listener: UpdateListener,
options: LexicalListenerOptions,
): () => void {
const listenerSetOrMap = this._listeners.update;
listenerSetOrMap.add(listener);
if (options && options.signal instanceof AbortSignal) {
options.signal.addEventListener('abort', () => {
listenerSetOrMap.delete(listener);
});
}
return () => {
listenerSetOrMap.delete(listener);
};
}
}Impact
The AbortController API is compatible with other Web APIs such as fetch and EventTarget, so adding a signal to lexical listeners will simplify the management of the lifecycle dependent on asynchronous operations, including user events.
This can be especially useful for applications that use lexical editors outside of React with its familiar useEffect for lifecycle management. At the very least, it will reduce boilerplate code where cleanup functions from lexical listeners are already used together with AbortController