Fixing bug where subscribers are skipped, when subscriber unsubscribes directly

This commit is contained in:
Fabian 2019-11-26 23:19:06 +01:00
parent 6720432e24
commit 811b03fd1b
4 changed files with 15 additions and 20 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.1.0", "version": "2.2.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.2.1", "version": "2.2.3",
"description": "Different Utilities, that are not worth own packages", "description": "Different Utilities, that are not worth own packages",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@ -18,8 +18,7 @@ export default class AwaitStore<T = any> {
subscribe(handler: ObserverCallback<T>) { subscribe(handler: ObserverCallback<T>) {
handler(this._value); handler(this._value);
this.observable.subscribe(handler); return this.observable.subscribe(handler);
return () => this.unsubscribe(handler);
} }
unsubscribe(handler: ObserverCallback<T>) { unsubscribe(handler: ObserverCallback<T>) {
@ -34,23 +33,11 @@ export default class AwaitStore<T = any> {
const cb = () => { const cb = () => {
if (this._value === val) { if (this._value === val) {
yes(); yes();
this.observable.unsubscribe(cb); this.unsubscribe(cb);
return true; }
} }
return false this.subscribe(cb);
}
if (this._value === val) {
yes();
} else {
ignore = () => {
this.observable.unsubscribe(cb);
}
if (!cb()) {
this.observable.subscribe(cb);
}
}
}); });
return { return {

View File

@ -92,7 +92,15 @@ export default class Observable<T = any> {
send(data: T) { send(data: T) {
if (this[ClosedSymbol]) if (this[ClosedSymbol])
throw new Error("Observable is closed!") 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); this.events.push(data);
if (!this.timeout) { if (!this.timeout) {
this.timeout = setTimeout(() => { this.timeout = setTimeout(() => {