Installations
npm install mobx-persist
Releases
Unable to fetch releases
Developer
pinqy520
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.8.1
NPM Version
5.4.2
Statistics
560 Stars
109 Commits
62 Forks
11 Watching
2 Branches
7 Contributors
Updated on 23 Oct 2024
Bundle Size
17.66 kB
Minified
5.64 kB
Minified + Gzipped
Languages
TypeScript (61.89%)
JavaScript (36.8%)
HTML (1.31%)
Total Downloads
Cumulative downloads
Total Downloads
3,071,834
Last day
-24.2%
1,236
Compared to previous day
Last week
-5.6%
7,038
Compared to previous week
Last month
24%
33,768
Compared to previous month
Last year
-22.3%
429,167
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Mobx Persist
$ npm install mobx-persist --save
Usage
1import { observable } from 'mobx' 2import { create, persist } from 'mobx-persist' 3 4class SomeItem { 5 @persist @observable name = 'some' 6 @persist @observable count = 0 7} 8 9class SomeStore { 10 @persist('object') @observable obj = { a: 1, b: 2 } 11 @persist('map') @observable stringMap = observable.map<string>({}) 12 @persist('list') @observable numList = [1,2,3,4] 13 @persist('object', SomeItem) @observable s = new SomeItem 14 @persist('map', SomeItem) @observable m = observable.map<SomeItem>({}) 15 @persist('list', SomeItem) @observable l = [] 16} 17 18const hydrate = create({ 19 storage: localForage, // or AsyncStorage in react-native. 20 // default: localStorage 21 jsonify: false // if you use AsyncStorage, here shoud be true 22 // default: true 23}) 24 25// create the state 26export const someStore = new SomeStore() 27hydrate('some', someStore).then(() => console.log('someStore has been hydrated')) 28
without decorators
1const data = observable({ 2 title: 'no decorator', 3 someObject: { 4 a: 1, 5 b: 'b', 6 }, 7 someArray: [{ 8 c: 1, 9 d: 'd' 10 }] 11}) 12const schema = { 13 title: true, 14 someObject: { 15 type: 'object', 16 schema: { 17 a: true, 18 b: true 19 } 20 }, 21 someArray: { 22 type: 'list', 23 schema: { 24 c: true, 25 d: true 26 } 27 } 28} 29export const someStore = persist(schema)(data) 30hydrate('some', someStore).then(() => console.log('someStore has been hydrated'))
with initial state
1const initialState = window.__STATE__.some || { 2 obj: { a: 2, b: 1 } 3} 4export const someStore = new SomeStore() 5 6hydrate('some', someStore, initialState) 7 .then(() => console.log('some hydrated'))
re-hydration
1const result = hydrate('some', someStore, initialState) 2const rehydrate = result.rehydrate 3result.then(() => console.log('some hydrated')) 4 5setTimeout(() => { 6 rehydrate().then(() => console.log('rehydrated')) 7}, 3000)
API
persist(schema)(object)
- arguments
- schema string/object Describes the type of data you are planning to persist. Not needed for JS primitive types. Options:
'object' | 'list' | 'map'
or a structured schema object. - observable any The observable that you are persisting.
- schema string/object Describes the type of data you are planning to persist. Not needed for JS primitive types. Options:
- returns a persistence-enabled version of observable
create(config)
- arguments
- config object Describes the storage container you want your data to reside in.
- storage localForage/AsyncStorage/localStorage localForage-style storage API. localStorage for Web (default), AsyncStorage for React Native
- jsonify bool Enables serialization as JSON
- debounce number Debounce interval applied to storage calls (in miliseconds, default 0).
- config object Describes the storage container you want your data to reside in.
- returns
- hydrate function
hydrate(key, store, initialState?, customArgs?)
- key string The key of your datastore that you want to hydrate from your persisted record.
- store object The store in which that key resides.
- initialState object Optional initial state the store is seeded with.
- customArgs object Optional custom arguments that are available during the deserialization process which can be used to pass in e.g. stores to model constructors during deserialization. See https://github.com/mobxjs/serializr#6-use-custom-arguments-to-inject-stores-to-models
- returns IHydrateResult
- hydrate function
interface IHydrateResult
extends Promise
- methods
- rehydrate function
- returns IHydrateResult
- rehydrate function
Examples
Dependency
No vulnerabilities found.
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
binaries present in source code
Details
- Warn: binary detected: examples/rn/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
Found 4/26 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More