make sure once callback is only called once

This commit is contained in:
Martti Malmi 2023-09-09 21:18:04 +03:00
parent 6522c373ad
commit d7a7c752aa

View File

@ -226,14 +226,13 @@ export default class Node {
/** /**
* Same as on(), but will unsubscribe after the first callback * Same as on(), but will unsubscribe after the first callback
* @param callback * @param callback
* @param unsubscribe
*/ */
once(callback?: Callback, returnIfUndefined = false, unsubscribe?: Unsubscribe): Promise<any> { once(callback?: Callback, returnIfUndefined = false): Promise<any> {
return new Promise((resolve) => { return new Promise((resolve) => {
let resolved = false;
const cb = (value, updatedAt, path, unsub) => { const cb = (value, updatedAt, path, unsub) => {
if (unsubscribe) { if (resolved) return;
unsubscribe(); resolved = true;
}
resolve(value); resolve(value);
callback?.(value, updatedAt, path, () => {}); callback?.(value, updatedAt, path, () => {});
unsub(); unsub();