Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/capaj/mobx-stored
Mobx observables persistent across browser tabs/sessions
https://github.com/capaj/mobx-stored
localstorage mobx mobx-observables single-page-applications
Last synced: 4 months ago
JSON representation
Mobx observables persistent across browser tabs/sessions
- Host: GitHub
- URL: https://github.com/capaj/mobx-stored
- Owner: capaj
- License: mit
- Created: 2016-01-04T10:43:34.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-12-26T14:35:40.000Z (about 6 years ago)
- Last Synced: 2024-10-13T12:34:23.425Z (4 months ago)
- Topics: localstorage, mobx, mobx-observables, single-page-applications
- Language: TypeScript
- Homepage:
- Size: 194 KB
- Stars: 39
- Watchers: 5
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mobx-stored
a simple utility for persisting POJO objects into localStorage via mobx observables. Not only they persist through sessions, they also synchronize via storage events across all browser tabs.
## usage
```javascript
import { localStored } from 'mobx-stored'
import { sessionStored } from 'mobx-stored' // for using sessionStorage rather than localStorageconst defaultUser = { email: null, firstname: null, lastname: null }
const observableUserProfile = localStored('userProfile', defaultUser, { // observableUserProfile is the default extended with the data from localStorage
delay: 500
}) // last parameter is optional-mobx autorun options for running the save operation into the storage. Use higher delay if you store a lot of data// now any changes made to the observableUserProfile are synced in localStorage
observableUserProfile.name = 'Michael'
// after 500ms and reloading the page
observableUserProfile.name === 'Michael' // true
// revert to the default values
observableUserProfile.reset()
// need to add new properties?
observableUserProfile.extend({
myNewProp: 1
})//Don't need it anymore?
observableUserProfile.destroy() // removes it from localStorage
```