Skip to content

Commit

Permalink
make sure once callback is only called once
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Sep 9, 2023
1 parent 6522c37 commit d7a7c75
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/js/state/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,13 @@ export default class Node {
/**
* Same as on(), but will unsubscribe after the first 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) => {
let resolved = false;
const cb = (value, updatedAt, path, unsub) => {
if (unsubscribe) {
unsubscribe();
}
if (resolved) return;
resolved = true;
resolve(value);
callback?.(value, updatedAt, path, () => {});
unsub();
Expand Down

0 comments on commit d7a7c75

Please sign in to comment.