From c6b14531fc90e7628b3a4617b7a30dc1cdd7f75a Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Sun, 12 May 2024 00:09:25 +0100 Subject: [PATCH] fix previous --- .../src/lib/utilities/Previous/Previous.svelte.ts | 15 ++++++++------- .../runed/src/lib/utilities/watch/watch.svelte.ts | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/runed/src/lib/utilities/Previous/Previous.svelte.ts b/packages/runed/src/lib/utilities/Previous/Previous.svelte.ts index 3dba7ab6..c3afbe5f 100644 --- a/packages/runed/src/lib/utilities/Previous/Previous.svelte.ts +++ b/packages/runed/src/lib/utilities/Previous/Previous.svelte.ts @@ -1,4 +1,5 @@ -import { type PreviousValue, watch } from "../index.js"; +import { untrack } from "svelte"; +import { watch } from "../index.js"; import type { Getter } from "$lib/internal/types.js"; /** @@ -7,17 +8,17 @@ import type { Getter } from "$lib/internal/types.js"; * @see {@link https://runed.dev/docs/utilities/use-previous} */ export class Previous { - #previous = $state | undefined>(); + #previous = $state(undefined); + #curr = $state(undefined); constructor(getter: Getter) { - // eslint-disable-next-line ts/no-explicit-any - this.#previous = (Array.isArray(getter()) ? [] : undefined) as any; - watch(getter, (_, prev) => { - this.#previous = prev; + $effect(() => { + this.#previous = untrack(() => this.#curr); + this.#curr = getter(); }); } get current() { return this.#previous; } -} +} \ No newline at end of file diff --git a/packages/runed/src/lib/utilities/watch/watch.svelte.ts b/packages/runed/src/lib/utilities/watch/watch.svelte.ts index 30720899..9f4931e2 100644 --- a/packages/runed/src/lib/utilities/watch/watch.svelte.ts +++ b/packages/runed/src/lib/utilities/watch/watch.svelte.ts @@ -31,7 +31,7 @@ export type WatchOptions = { once?: boolean; }; -export type PreviousValue = T extends Array ? Array : T | undefined; +type PreviousValue = T extends Array ? Array : T | undefined; function runWatcher( source: Getter,