From 8698cf77fd7d83c662ec74f9449c1c4b11280c25 Mon Sep 17 00:00:00 2001 From: saifullah-N Date: Fri, 16 Jun 2023 22:28:14 +0530 Subject: [PATCH] updated waitForChange storeHelper in svelte --- src/svelte-components/src/lib/StoreHelpers.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/svelte-components/src/lib/StoreHelpers.ts b/src/svelte-components/src/lib/StoreHelpers.ts index 78604a5..f0e8bb2 100644 --- a/src/svelte-components/src/lib/StoreHelpers.ts +++ b/src/svelte-components/src/lib/StoreHelpers.ts @@ -1,18 +1,19 @@ import { get, type Writable } from "svelte/store"; -export function listenForChange(writable: Writable, cb: (value: T) => void) { - const priorValue = get(writable); - - const unsubscribe = writable.subscribe((value) => { - if (value !== priorValue) { - unsubscribe(); - cb(value); - } - }); -} - export function waitForChange(writable: Writable): Promise { - return new Promise((resolve) => { - listenForChange(writable, (value) => resolve(value)); + let unsubscribe:()=>{}; + const promise = new Promise(resolve => { + let receivedInitial = false; + let previousValue : any; + unsubscribe = writable.subscribe(value => { + if (!receivedInitial) { + receivedInitial = true; + previousValue = value; + } else if (value !== previousValue) { + resolve(value); + } }); -} + }); + promise.then(unsubscribe); + return promise; +} \ No newline at end of file