mirror of
https://github.com/irislib/iris-messenger.git
synced 2024-10-18 06:03:22 +00:00
make sure once callback is only called once
This commit is contained in:
parent
6522c373ad
commit
d7a7c752aa
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user