• Stars
    star
    561
  • Rank 79,400 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

persist mobx stores

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).then(() => console.log('someStore has been 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', someStore).then(() => console.log('someStore has been 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, 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

interface IHydrateResult

extends Promise

  • methods
    • rehydrate function
      • returns IHydrateResult

Examples

Dependency

More Repositories

1

react-native-webview-invoke

Invoke functions between React Native and WebView
JavaScript
232
star
2

revas

Use React and CSS to build UI interfaces on canvas
TypeScript
114
star
3

three-typescript-starter

Three.js + Typescript + Webpack Boilerplate
TypeScript
109
star
4

react-native-shadow-view

React Native's View Component with Shadows Both on Android and iOS, inspired by react-native-shadow
TypeScript
35
star
5

yoga-layout-wasm

yoga-layout webassembly module
C++
28
star
6

GLMultiCameraPreviewDemo

Android multiple camera preview surface with offscreen rendering and GPU filter powered by android-gpuimage-plus. 安卓相机多预览,外加离屏渲染和GPU滤镜 (来自 android-gpuimage-plus) 示例
Java
14
star
7

coverage-shaker

Minify JavaScript code from chrome/puppeteer coverage report. tree shaking.
TypeScript
11
star
8

react-native-emoji-rain

React native emoji rain component use react hooks - inspired by WeChat emoji rain
JavaScript
10
star
9

flutter_tencentmusician

An unofficial app for tencent musician using Flutter | 用Flutter实现的腾讯音乐人后台
Dart
5
star
10

mil.

TypeScript版冲顶大会、百万xx等辅助答题程序(还在开发中,预计今晚搞定吧)
TypeScript
3
star
11

apollo-client-tsx-starter-kit

React Webpack TypeScript Apollo Client Side Boilerplate For GraphQL API
JavaScript
2
star
12

five

An example app with firebase JSSDK + React Native + Redux
JavaScript
2
star
13

simple-tsx-starter-kit

Minimal React Webpack TypeScript Boilerplate
JavaScript
1
star
14

SShareNS3

Simulation of an ad-hoc Semantic Web Data System based on NS3
C++
1
star
15

react-native-webviewbridge-invoke

react-native-webview-invoke for react-native-webview-bridge
JavaScript
1
star