mobx-persist
TypeScript icon, indicating that this package has built-in type declarations

0.4.1 • Public • Published

Mobx Persist

npm version

$ npm install mobx-persist --save

Usage

import { observable } from 'mobx'
import { create, persist } from 'mobx-persist'
 
class SomeItem {
    @persist @observable  name = 'some'
    @persist @observable count = 0
}
 
class SomeStore {
    @persist('object') @observable         obj = { a: 1, b: 2 }
    @persist('map')    @observable   stringMap = observable.map<string>({})
    @persist('list')   @observable     numList = [1,2,3,4]
    @persist('object', SomeItem) @observable s = new SomeItem
    @persist('map', SomeItem)    @observable m = observable.map<SomeItem>({})
    @persist('list', SomeItem)   @observable l = []
}
 
const hydrate = create({
    storage: localForage,   // or AsyncStorage in react-native.
                            // default: localStorage
    jsonify: false  // if you use AsyncStorage, here shoud be true
                    // default: true
})
 
// create the state
export const someStore = new SomeStore()
 
hydrate('some', someStore)
    // post hydration
    .then(() => console.log('some hydrated'))
 

without decorators

const data = observable({
    title: 'no decorator',
    someObject: {
        a: 1,
        b: 'b',
    },
    someArray: [{
        c: 1,
        d: 'd'
    }]
})
const schema = {
    title: true,
    someObject: {
        type: 'object',
        schema: {
            a: true,
            b: true
        }
    },
    someArray: {
        type: 'list',
        schema: {
            c: true,
            d: true
        }
    }
}
export const someStore = persist(schema)(data)
hydrate('some', state)
    .then(() => console.log('some hydrated'))

with initial state

const initialState = window.__STATE__.some || {
    obj: { a: 2, b: 1 }
}
export const someStore = new SomeStore()
 
hydrate('some', someStore, initialState)
    .then(() => console.log('some hydrated'))

re-hydration

const result = hydrate('some', someStore, initialState)
const rehydrate = result.rehydrate
result.then(() => console.log('some hydrated'))
 
setTimeout(() => {
    rehydrate().then(() => console.log('rehydrated'))
}, 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.
  • 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).
  • returns
    • hydrate function hydrate(key, store)
      • 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.
      • returns IHydrateResult

interface IHydrateResult

extends Promise

  • methods
    • rehydrate function
      • returns IHydrateResult

Examples

Dependency

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.4.16,090latest

Version History

VersionDownloads (Last 7 Days)Published
0.4.16,090
0.4.01
0.3.416
0.3.31
0.3.21
0.3.11
0.3.01
0.2.31
0.2.21
0.2.11
0.2.01
0.1.51
0.1.41
0.1.31
0.1.21
0.1.10
0.1.00
0.0.60
0.0.50
0.0.40
0.0.30
0.0.23
0.0.10

Package Sidebar

Install

npm i mobx-persist

Weekly Downloads

6,122

Version

0.4.1

License

MIT

Last publish

Collaborators

  • pinqy520