Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve JS Object types in state persistance #1

Open
wants to merge 1 commit into
base: store
Choose a base branch
from

Conversation

Jackbennett
Copy link

Fantastic example repo and thanks for the write up, Instead of fighting json when hydrating the state form the store with JS types, I found since this example sticks to indexedDB that supports JS object directly, you only need to tangle with JSON if you want to use another store for compatibility like webSQL or localstorage.

It would be nice to not pull in lodash but that's a better solution that anything I tried.

IndexedDB uses the structured clone algorithm[1] which understands
JS object types like `Date` or `Set` that is lost casting the state
back and forth to JSON strings.

Reactivity is removed from the object being stored which
is why cloneDeep is included as indexedDB requires plain objects.
Removing the Proxy[2] on this.state with lodash cloneDeep

1: indexedDB structured clone algorithm

2: Proxy

IndexedDB uses the structured clone algorithm[1] which understands
JS object types like `Date` or `Set` that is lost casting the state
back and forth to JSON strings.

Reactivity is removed from the object being stored which
is why cloneDeep is included as indexedDB requires plain objects.
Removing the Proxy[2] on this.state with lodash cloneDeep.

1: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

2: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
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.

1 participant