Remove unnecessary timer from obeservable

This commit is contained in:
Fabian Stamm
2020-04-13 03:11:12 +02:00
parent 2545f3a050
commit 858a508190
7 changed files with 106 additions and 98 deletions

View File

@ -41,24 +41,28 @@ export default class AwaitStore<T = any> {
}
/**
* Await a specific value and return.
*
* Await a specific value and return.
*
* For example if val = true then this function would block until the value
* is actually true. If it is true, then the promise will resolve immediatly
*
*
* @param val Value to await
*/
awaitValue(val: T): PromiseLike<void> & { catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>, ignore: () => void } {
awaitValue(
val: T
): PromiseLike<void> & {
catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>;
ignore: () => void;
} {
let ignore: () => void = () => undefined;
let prms = new Promise<void>(yes => {
let prms = new Promise<void>((yes) => {
const cb = () => {
if (this._value === val) {
yes();
this.unsubscribe(cb);
}
}
};
this.subscribe(cb);
});
@ -66,23 +70,23 @@ export default class AwaitStore<T = any> {
return {
then: prms.then.bind(prms),
catch: prms.catch.bind(prms),
ignore: () => ignore()
}
ignore: () => ignore(),
};
}
/**
* Creates Public API with subscribe and unsubscribe
*
* @returns {object}
*/
* Creates Public API with subscribe and unsubscribe
*
* @returns {object}
*/
getPublicApi() {
if (this.observable.closed)
throw new Error("Observable is closed!");
if (this.observable.closed) throw new Error("Observable is closed!");
return {
subscribe: (callback: ObserverCallback<T>) => this.subscribe(callback),
unsubscribe: (callback: ObserverCallback<T>) => this.unsubscribe(callback),
awaitValue: (value: T) => this.awaitValue(value)
}
unsubscribe: (callback: ObserverCallback<T>) =>
this.unsubscribe(callback),
awaitValue: (value: T) => this.awaitValue(value),
};
}
/**
@ -91,4 +95,4 @@ export default class AwaitStore<T = any> {
close() {
this.observable.close();
}
}
}