Skip to content

Conversation

ibilux
Copy link

@ibilux ibilux commented Jun 24, 2025

This add a remove function to remove persisted state from storage like window.localstorage.removeitem for #208.

If the initialValue is set to null the PersistedState will not create a localstorage item. Ex:

const count = $state(new PersistedState("count", null));

If the PersistedState is removed the current value will be set to null. Ex:

count.remove();

This add a `remove` function to remove persisted state from storage like `window.localstorage.removeitem`
Copy link

changeset-bot bot commented Jun 24, 2025

🦋 Changeset detected

Latest commit: 8d3c5af

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

@TGlide
Copy link
Member

TGlide commented Jul 6, 2025

Maybe lets call it reset?

@ibilux
Copy link
Author

ibilux commented Jul 6, 2025

Maybe lets call it reset?

I think Reset means to restore the state to its initial or default value. But this (remove) is meant to remove the persisted state from local storage entirely.

@@ -43,7 +43,7 @@ export class PersistedState<T> {
#subscribe?: VoidFunction;
#version = $state(0);

constructor(key: string, initialValue: T, options: PersistedStateOptions<T> = {}) {
constructor(key: string, initialValue: T | null, options: PersistedStateOptions<T> = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should it be potentially null? Shouldn't that be a part of T?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removeItem() method of the Storage interface sets the item to null when it's removed. So if the user chooses a null value it won't be saved to the local storage (this is just a work around).
The #current: T | undefined; is already typed as T | undefined i didn't want to use undefined and used null instead so it won't interfere with the current codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants