From 811b03fd1b108e1b8d453f096a703282ea762d76 Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 26 Nov 2019 23:19:06 +0100 Subject: [PATCH] Fixing bug where subscribers are skipped, when subscriber unsubscribes directly --- package-lock.json | 2 +- package.json | 2 +- src/awaiter.ts | 21 ++++----------------- src/observable.ts | 10 +++++++++- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index bb9f27e..5bc9387 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.1.0", + "version": "2.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 115ff29..a815faf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/utils", - "version": "2.2.1", + "version": "2.2.3", "description": "Different Utilities, that are not worth own packages", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/awaiter.ts b/src/awaiter.ts index 150f413..a86c02c 100644 --- a/src/awaiter.ts +++ b/src/awaiter.ts @@ -18,8 +18,7 @@ export default class AwaitStore { subscribe(handler: ObserverCallback) { handler(this._value); - this.observable.subscribe(handler); - return () => this.unsubscribe(handler); + return this.observable.subscribe(handler); } unsubscribe(handler: ObserverCallback) { @@ -34,23 +33,11 @@ export default class AwaitStore { const cb = () => { if (this._value === val) { yes(); - this.observable.unsubscribe(cb); - return true; - } - - return false - } - - if (this._value === val) { - yes(); - } else { - ignore = () => { - this.observable.unsubscribe(cb); - } - if (!cb()) { - this.observable.subscribe(cb); + this.unsubscribe(cb); } } + + this.subscribe(cb); }); return { diff --git a/src/observable.ts b/src/observable.ts index 5770394..b9835a4 100755 --- a/src/observable.ts +++ b/src/observable.ts @@ -92,7 +92,15 @@ export default class Observable { send(data: T) { if (this[ClosedSymbol]) throw new Error("Observable is closed!") - this.subscriber.forEach(e => e(data)); + + Array.from(this.subscriber.values()).forEach(e => { + try { + e(data) + } catch (err) { + // Catch error, so it doesn't affect other subscribers + console.error(err) + } + }); this.events.push(data); if (!this.timeout) { this.timeout = setTimeout(() => {