Remove unnecessary timer from obeservable
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user