Gathering detailed insights and metrics for mobx-persist-store
Gathering detailed insights and metrics for mobx-persist-store
Gathering detailed insights and metrics for mobx-persist-store
Gathering detailed insights and metrics for mobx-persist-store
npm install mobx-persist-store
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
270 Stars
345 Commits
14 Forks
3 Watching
19 Branches
8 Contributors
Updated on 08 Nov 2024
TypeScript (98.43%)
Shell (0.86%)
JavaScript (0.71%)
Cumulative downloads
Total Downloads
Last day
3.6%
4,865
Compared to previous day
Last week
2.8%
24,105
Compared to previous week
Last month
10.6%
100,250
Compared to previous month
Last year
20.5%
1,034,033
Compared to previous year
1
20
1# by yarn 2yarn add mobx-persist-store 3 4# OR by npm 5npm i mobx-persist-store
Mobx Persist Store with MobX 6
1makePersistable(this, { name: 'SampleStore', properties: ['someProperty'], storage: window.localStorage });
To simply persist your MobX store use makePersistable
. Pass a reference of the store (this
) as the first argument.
The second argument is the StorageOptions for persisting the store data.
In the example below name
, properties
, and storage
properties are required but if you use configurePersistable you can set a global storage adapter, so you only have to set it once.
You can also pass a third argument (ReactionOptions) to control when data should be saved.
Hydration of the store will happen automatically when makePersistable
is created.
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable } from 'mobx-persist-store'; 3 4export class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this); 9 10 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'], storage: window.localStorage }); 11 } 12}
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable } from 'mobx-persist-store'; 3 4export class SampleStore { 5 someProperty: ['a', 'b', 'c']; 6 7 constructor() { 8 makeAutoObservable(this); 9 10 makePersistable(this, { 11 name: 'SampleStore', 12 properties: [ 13 { 14 key: 'someProperty', 15 serialize: (value) => { 16 return value.join(','); 17 }, 18 deserialize: (value) => { 19 return value.split(','); 20 }, 21 }, 22 ], 23 storage: window.localStorage, 24 }); 25 } 26}
1import { makePersistable } from 'mobx-persist-store'; 2import localForage from "localforage"; 3 4... 5makePersistable( 6 this, 7 { 8 name: 'SampleStore', 9 properties: ['someProperty'], 10 storage: localForage, // localForage, window.localStorage, AsyncStorage all have the same interface 11 expireIn: 86400000, // One day in milliseconds 12 removeOnExpiration: true, 13 stringify: false, 14 debugMode: true, 15 }, 16 { delay: 200, fireImmediately: false }, 17); 18...
If you plan on using the same values for some options you can set them globally with the configurePersistable
.
1import { configurePersistable } from 'mobx-persist-store'; 2 3// All properties are optional 4configurePersistable( 5 { 6 storage: window.localStorage, 7 expireIn: 86400000, 8 removeOnExpiration: true, 9 stringify: false, 10 debugMode: true, 11 }, 12 { delay: 200, fireImmediately: false } 13);
1export class SampleStore { 2 someProperty: []; 3 4 constructor() { 5 makeAutoObservable(this); 6 7 // Now makePersistable only needs `name` and `properties`: 8 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 9 } 10}
configurePersistable
sets items globally, but you can override them within makePersistable
.
You should only need makePersistable
but this library also provides other utils for more advance usage.
makePersistable sets up store persisting.
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this); 9 10 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 11 } 12}
makePersistable
is a Promise, so you can determine when the store has been initially hydrated. Also, you can use isHydrated to determine the hydration state.1import { makeAutoObservable, action } from 'mobx'; 2import { makePersistable } from 'mobx-persist-store'; 3... 4 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }).then( 5 action((persistStore) => { 6 console.log(persistStore.isHydrated); 7 }) 8 ); 9...
StorageOptions
name
(String) - Should be a unique identifier and will be used as the key for the data storage.properties
(Array of String) - A list of observable properties on the store you want to persist. Doesn't save MobX actions or computed values.storage
(localStorage Like API) - Facilitates the reading, writing, and removal of the persisted store data. For ReactNative it may beAsyncStorage
,FS
, etc. and for React -localStorage
,sessionStorage
,localForage
etc.
- If you have an app that is Server-side rendering (SSR) you can set the value
undefined
to prevent errors.expireIn
(Number) - A value in milliseconds to determine when the data in storage should not be retrieved by getItem. Never expires by default.removeOnExpiration
(Boolean) - If expireIn has a value and has expired, the data in storage will be removed automatically when getItem is called. The default value is true.stringify
(Boolean) - When true the data will be JSON.stringify before being passed to setItem. The default value is true.debugMode
(Boolean) - When true a console.info will be called for several of mobx-persist-store items. The default value is false.ReactionOptions MobX Reactions Options
delay
(Number) - Allows you to set adelay
option to limit the amount of times thewrite
function is called. No delay by default.
- For example if you have a
200
millisecond delay and two changes happen within the delay time then thewrite
function is only called once. If you have no delay then thewrite
function would be called twice.fireImmediately
(Boolean) - Determines if the store data should immediately be persisted or wait until a property in store changes.false
by default.1configurePersistable( 2 { 3 storage: window.localStorage, 4 expireIn: 86400000, 5 removeOnExpiration: true, 6 stringify: false, 7 debugMode: true, 8 }, 9 { delay: 200, fireImmediately: false } 10); 11... 12makePersistable( 13 this, 14 { 15 name: 'SampleStore', 16 properties: ['someProperty'], 17 storage: window.localStorage, 18 expireIn: 86400000, 19 removeOnExpiration: true, 20 stringify: false, 21 debugMode: true, 22 }, 23 { delay: 200, fireImmediately: false } 24);
isHydrated will be
true
once the store has finished being updated with the persisted data.1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, isHydrated } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 get isHydrated() { 13 return isHydrated(this); 14 } 15}
isPersisting determines if the store is being currently persisted. When calling
pausePersisting
the value will befalse
andtrue
withstartPersisting
is called.1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, isPersisting } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 get isPersisting() { 13 return isPersisting(this); 14 } 15}
pausePersisting pauses the store from persisting data.
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, pausePersisting } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 pauseStore() { 13 pausePersisting(this); 14 } 15}
startPersisting starts persisting the store data again after
pausePersisting
was called.1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, startPersisting } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 startStore() { 13 startPersisting(this); 14 } 15}
stopPersisting calls
pausePersisting
and internally removes reference to the store. You should only call this function if you have store(s) that are re-created or do not live for the entire life of your application.1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, stopPersisting } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 stopStore() { 13 stopPersisting(this); 14 } 15}
This function prevents memory leaks when you have store(s) that are removed or re-crated. In the React example below
stopPersisting
is called when the component is unmounted.1import React, { useEffect, useState } from 'react'; 2import { observer } from 'mobx-react-lite'; 3import { stopPersisting } from 'mobx-persist-store'; 4 5export const SamplePage = observer(() => { 6 const [localStore] = useState(() => new SampleStore()); 7 8 useEffect(() => { 9 // Called when the component is unmounted 10 return () => localStore.stopStore(); 11 }, []); 12 13 return ( 14 <div> 15 {localStore.someProperty.map((item) => ( 16 <div key={item.name}>{item.name}</div> 17 ))} 18 </div> 19 ); 20});
hydrateStore will update the store with the persisted data. This will happen automatically with the initial
makePersistable
call. This function is provide to manually hydrate the store. You should not have to call it unless you are doing something that modified the persisted data outside the store.1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, hydrateStore } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 async hydrateStore() { 13 await hydrateStore(this); 14 } 15}
clearPersistedStore will remove the persisted data. This function is provide to manually clear the store's persisted data.
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, clearPersistedStore } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 async clearStoredData() { 13 await clearPersistedStore(this); 14 } 15}
getPersistedStore will get the persisted data. This function is provide to manually get the store's persisted data.
1import { makeAutoObservable } from 'mobx'; 2import { makePersistable, getPersistedStore } from 'mobx-persist-store'; 3 4class SampleStore { 5 someProperty: []; 6 7 constructor() { 8 makeAutoObservable(this, {}, { autoBind: true }); 9 makePersistable(this, { name: 'SampleStore', properties: ['someProperty'] }); 10 } 11 12 async getStoredData() { 13 return getPersistedStore(this); 14 } 15}
PersistStoreMap is a JavaScript Map object where the key is a reference to the store, and the value is a reference to the persist store. Note: calling
stopPersisting(this)
will remove the store and persist store references from PersistStoreMap to prevent memory leaks.1import { PersistStoreMap } from 'mobx-persist-store'; 2 3Array.from(PersistStoreMap.values()).map((persistStore) => persistStore.getPersistedStore());
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
4 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 6/24 approved changesets -- score normalized to 2
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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