Adding custom check function for await store

This commit is contained in:
Fabian Stamm 2020-08-21 00:21:32 +02:00
parent 35323a095a
commit 7a649fa1b0
2 changed files with 10 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/utils", "name": "@hibas123/utils",
"version": "2.2.5", "version": "2.2.6",
"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

@ -1,5 +1,7 @@
import Observable, { ObserverCallback } from "./observable"; import Observable, { ObserverCallback } from "./observable";
export type CheckFunction<T> = (val: T) => boolean;
export default class AwaitStore<T = any> { export default class AwaitStore<T = any> {
private observable = new Observable<T>(); private observable = new Observable<T>();
constructor(private _value: T) { constructor(private _value: T) {
@ -49,7 +51,7 @@ export default class AwaitStore<T = any> {
* @param val Value to await * @param val Value to await
*/ */
awaitValue( awaitValue(
val: T val: T | CheckFunction<T>
): PromiseLike<void> & { ): PromiseLike<void> & {
catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>; catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>;
ignore: () => void; ignore: () => void;
@ -58,7 +60,12 @@ export default class AwaitStore<T = any> {
let prms = new Promise<void>((yes) => { let prms = new Promise<void>((yes) => {
const cb = () => { const cb = () => {
if (this._value === val) { if (typeof val === "function") {
if ((val as CheckFunction<T>)(this._value)) {
yes();
this.unsubscribe(cb);
}
} else if (this._value === val) {
yes(); yes();
this.unsubscribe(cb); this.unsubscribe(cb);
} }