Skip to content

feat(PersistedState): connection & null value handling#359

Merged
huntabyte merged 2 commits intomainfrom
improve/persisted-state
Nov 26, 2025
Merged

feat(PersistedState): connection & null value handling#359
huntabyte merged 2 commits intomainfrom
improve/persisted-state

Conversation

@huntabyte
Copy link
Member

@huntabyte huntabyte commented Oct 14, 2025

This PR enhances PersistedState to handle null values and adds connection control capabilities.

Changes

Null Value Handling

Previously, users were not able to set the value of the PersistedState to null for reasons unknown to me:

const a = new PersistedState<string | null>("a", "hello")
a.current = null // a no-op
console.log(a.current) // still "hello"

This has been fixed, null can now be stored and retrieved from storage.

Closes #343
Closes #267

Connection Control

Multiple issues have been opened requesting something along the lines of having more control over the values being persisted to storage, mostly focused around removing a persisted value from storage.

This PR adds the ability to control when state connects/syncs to storage, enabling use cases like privacy modes that disable storage.

New API:

  1. connected option - start disconnected from storage:

       const state = new PersistedState("key", initialValue, {
         connected: false // start in memory-only mode
       });
  2. disconnect() method - disconnect/removes value from storage, preserves current value in memory, stops cross-tab synchronization, prevents future writes to storage (only updates memory)

    const state = new PersistedState("key", "someValue")
    state.disconnect() // removed from storage, value only in memory
  3. connect() method - connect to storage after disconnecting/initializing disconnected

    const state = new PersistedState("key", "someValue")
    state.disconnect() // removed from storage, value only in memory
    // later...
    state.connect() // added to storage, will sync when storage is updated
  4. connected getter - check connection status

    const state = new PersistedState("key", "someValue")
    console.log(state.connected) // true
    state.disconnect()
    console.log(state.connected) // false

When disconnected:

  • State changes are kept in memory only
  • Storage changes don't affect the state
  • Cross-tab sync is disabled
  • Both disconnect() and connect() are idempotent

Closes #208

@changeset-bot
Copy link

changeset-bot bot commented Oct 14, 2025

🦋 Changeset detected

Latest commit: cd5f90c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
runed Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Oct 14, 2025

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ❌ Failed (View Log) cd5f90c

@github-actions github-actions bot requested a deployment to Preview October 14, 2025 00:30 Abandoned
@huntabyte huntabyte requested review from TGlide and abdel-17 and removed request for abdel-17 October 14, 2025 00:34
@github-actions github-actions bot requested a deployment to Preview October 14, 2025 00:51 Abandoned
@huntabyte huntabyte marked this pull request as ready for review October 15, 2025 16:36
@1337MARCEL
Copy link

Hi! 👋 Just wondering if there’s any update or timeline for this PR.
These additions would help us a lot. Thanks so much for maintaining this project!

@huntabyte
Copy link
Member Author

cc @TGlide - waiting for his approval before merging!

@TGlide
Copy link
Member

TGlide commented Nov 24, 2025

I still don't think disconnect should remove from storage, but I think its okay for now! The changes are great overall :)

@huntabyte huntabyte merged commit 8be6a3c into main Nov 26, 2025
6 checks passed
@huntabyte huntabyte deleted the improve/persisted-state branch November 28, 2025 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants