joshnuss / svelte-persisted-store

A Svelte store that persists to localStorage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

persistency does not work with Sets

ptrtht opened this issue · comments

First of all mate, thanks for the work and for contributing to open source.

Secondly here is the issue

Reproduction:

export const somePersisted = persisted<{
  num: number;
  someArr: Array<string>;
  someSet: Set<string>;
}>('somePersisted', {
  num: 1,
  someArr: [],
  someSet: new Set(),
});

Now if I were to update these values:

somePersisted.set({
num: 2,
someArr: [1,2],
someSet: new Set(1,2,3),
})

The set values, and only the set values do not get placed into local storage.

This is due to the fact that you JSON serialise the data?
Suggestion:

Add to documentation that the package only works for JSON serialisable types.

Hi @qty0,

You are correct, it's because Set is not serializable with JSON.
See: https://stackoverflow.com/a/31190928

The solution is to use devalue as the serializer, as it supports Set. There's a small example in the readme.