Fixing bug where subscribers are skipped, when subscriber unsubscribes directly
This commit is contained in:
parent
6720432e24
commit
811b03fd1b
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/utils",
|
||||
"version": "2.1.0",
|
||||
"version": "2.2.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -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",
|
||||
|
@ -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 {
|
||||
|
@ -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(() => {
|
||||
|
Loading…
Reference in New Issue
Block a user