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",
"version": "2.1.0",
"version": "2.2.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -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",

View File

@ -18,8 +18,7 @@ export default class AwaitStore<T = any> {
subscribe(handler: ObserverCallback<T>) {
handler(this._value);
this.observable.subscribe(handler);
return () => this.unsubscribe(handler);
return this.observable.subscribe(handler);
}
unsubscribe(handler: ObserverCallback<T>) {
@ -34,23 +33,11 @@ export default class AwaitStore<T = any> {
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 {

View File

@ -92,7 +92,15 @@ export default class Observable<T = any> {
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(() => {