Adding custom check function for await store
This commit is contained in:
parent
35323a095a
commit
7a649fa1b0
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/utils",
|
||||
"version": "2.2.5",
|
||||
"version": "2.2.6",
|
||||
"description": "Different Utilities, that are not worth own packages",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
|
@ -1,5 +1,7 @@
|
||||
import Observable, { ObserverCallback } from "./observable";
|
||||
|
||||
export type CheckFunction<T> = (val: T) => boolean;
|
||||
|
||||
export default class AwaitStore<T = any> {
|
||||
private observable = new Observable<T>();
|
||||
constructor(private _value: T) {
|
||||
@ -49,7 +51,7 @@ export default class AwaitStore<T = any> {
|
||||
* @param val Value to await
|
||||
*/
|
||||
awaitValue(
|
||||
val: T
|
||||
val: T | CheckFunction<T>
|
||||
): PromiseLike<void> & {
|
||||
catch: (cb: (err: any) => PromiseLike<void>) => PromiseLike<void>;
|
||||
ignore: () => void;
|
||||
@ -58,7 +60,12 @@ export default class AwaitStore<T = any> {
|
||||
|
||||
let prms = new Promise<void>((yes) => {
|
||||
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();
|
||||
this.unsubscribe(cb);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user