Skip to content

Commit

Permalink
fix previous
Browse files Browse the repository at this point in the history
  • Loading branch information
TGlide committed May 11, 2024
1 parent 36b6405 commit c6b1453
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions packages/runed/src/lib/utilities/Previous/Previous.svelte.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -7,17 +8,17 @@ import type { Getter } from "$lib/internal/types.js";
* @see {@link https://runed.dev/docs/utilities/use-previous}
*/
export class Previous<T> {
#previous = $state<PreviousValue<T> | undefined>();
#previous = $state<T | undefined>(undefined);
#curr = $state<T | undefined>(undefined);

This comment has been minimized.

Copy link
@abdel-17

abdel-17 May 11, 2024

Collaborator

Since curr is always untracked, this can just be a regular variable. No need to make it $state.

#curr?: T;

constructor(getter: Getter<T>) {
// 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;
}
}
}
2 changes: 1 addition & 1 deletion packages/runed/src/lib/utilities/watch/watch.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type WatchOptions = {
once?: boolean;
};

export type PreviousValue<T> = T extends Array<infer U> ? Array<U | undefined> : T | undefined;
type PreviousValue<T> = T extends Array<infer U> ? Array<U | undefined> : T | undefined;

function runWatcher<T>(
source: Getter<T>,
Expand Down

0 comments on commit c6b1453

Please sign in to comment.