-
-
Notifications
You must be signed in to change notification settings - Fork 60
Closed
Description
Describe the bug
Apologies if this isn't the right place to report this or if I'm using PersistedState wrong.
After version v0.28.0
the state does not react/update when it's changed and referenced on a different component.
In my case, I have PersistedState on a class property which is shared across different components via setContext/getContext
.
When setting the .current
property of the state, it does not trigger any updates.
Example:
import { PersistedState } from 'runed';
export class TestPersist {
_sessionToken = new PersistedState<{ token: string | null }>(
`--test-perist-state`,
{ token: null },
{
storage: 'session'
}
);
normalState = $state('');
derivedToken = $derived.by(() => this._sessionToken.current);
get sessionToken() {
return this._sessionToken.current;
}
updateToken() {
const rand = Math.random().toString(36).substring(2, 15);
this._sessionToken.current = { token: `${rand}` };
this.normalState = `${rand}`
}
}
On parent component
<script lang="ts">
import { setContext, type Snippet } from 'svelte';
import { TestPersist } from './test-persist.svelte';
type Props = { children?: Snippet<[]> };
const { children }: Props = $props();
setContext('test', new TestPersist());
</script>
{@render children?.()}
Then in the child component
<script lang="ts">
import { getContext } from "svelte";
import type { TestPersist } from "./test-persist.svelte";
const test = getContext<TestPersist>('test');
const token = $derived(test.sessionToken);
const stateToken = $derived(test.normalState);
const derivedToken = $derived(test.derivedToken);
const direct = $derived(test._sessionToken.current);
</script>
<pre>
Persist State using class getter. Does not update
{JSON.stringify(token, null, 2)}
</pre>
<pre>
Normal State. This updated properly
{JSON.stringify(stateToken, null, 2)}
</pre>
<pre>
Derived State. Does not update
{JSON.stringify(derivedToken, null, 2)}
</pre>
<pre>
Direct Access. Does not update
{JSON.stringify(direct, null, 2)}
</pre>
Reproduction
Here is a bare bones sveltekit app to replicate the "issue". It's under the /test
route
https://github.com/charleston-olaes/runed-persisted-state-bug/tree/main
Logs
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-1235U
Memory: 16.97 GB / 31.12 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
Yarn: 1.22.19 - ~/.yarn/bin/yarn
npm: 9.6.5 - ~/.npm-global/bin/npm
pnpm: 9.14.3 - ~/.nvm/versions/node/v22.14.0/bin/pnpm
npmPackages:
@sveltejs/kit: ^2.22.0 => 2.24.0
runed: ^0.31.0 => 0.31.0
svelte: ^5.0.0 => 5.36.4
Severity
blocking an upgrade
rajaiswal
Metadata
Metadata
Assignees
Labels
No labels