GitXplorerGitXplorer
j

mst-async-storage

public
31 stars
11 forks
5 issues

Commits

List of commits on branch master.
Unverified
69bb31a4b68e9086428ba8b7abbb50bc00469f53

chore(release): 0.4.0 [skip ci]

ssemantic-release-bot committed 3 years ago
Unverified
0513a40cad6b8799af0dff59c06013c66d2aa7b3

feat: Adds onSave and onLoad settings (#30 by @djMax, @markrickert, and @jamonholmgren)

jjamonholmgren committed 3 years ago
Unverified
4cf302842aaed13ebbad8422a58e2a4e54df0656

chore(release): 0.3.0 [skip ci]

ssemantic-release-bot committed 3 years ago
Verified
700d488c7c25f1ef4f41a8ec5a9dccb6aa06ed8b

feat: Adds onSave and onLoad settings (#30 by @djMax, @markrickert, and @jamonholmgren)

jjamonholmgren committed 3 years ago
Unverified
eb45cfdb49a57e916387b60f2994014aea0403b5

chore(release): 0.2.3 [skip ci]

ssemantic-release-bot committed 3 years ago
Verified
6e3de823cfeb8a0eaa0ff3bfcc9bd4607ef7adcd

fix(AsyncStorage): Switches to @react-native-async-storage/async-storage (#23 by @danstepanov and @markrickert)

ddanstepanov committed 3 years ago

README

The README file for this repository.

mst-async-storage

A mobx-state-tree extension granting your models React Native's AsyncStorage powers of persistance.

Requirements

  • mobx-state-tree 2.x or 3.x
  • react-native >= 0.56
  • @react-native-async-storage/async-storage >= 1.x.x

Installing

yarn add mst-async-storage

Usage

The following async actions are added:

import { types } from "mobx-state-tree"
import { withAsyncStorage } from "mst-async-storage"

export const NiceThingsModel = types
  .model("NiceThings")
  .props({
    unicorns: true,
    dragons: true,
    cake: true,
    spiders: false,
    nickleback: false,
  })
  .actions(self => ({
    setSpiders(value: boolean) {
      self.spiders = value
    },
  }))
  .extend(withAsyncStorage({ key: "nice.things" })) // <-- 🎉

Now you can load it:

async demo () {
  // create your model as usual
  const happy = NiceThingsModel.create()

  // now load the data from async storage
  await happy.load()

  // and when you change something
  happy.setSpiders(true)

  // it will automatically save back to async storage!
}

You can also intercept the snapshot after it is loaded and before it is saved with onLoad and onSave.

import { types } from "mobx-state-tree"
import { withAsyncStorage } from "mst-async-storage"

export const NiceThingsModel = types
  .model("NiceThings")
  .props({
    unicorns: true,
    dragons: true,
    cake: true,
    spiders: false,
    nickleback: false,
  })
  .actions(self => ({
    setSpiders(value: boolean) {
      self.spiders = value
    },
  }))
  .extend(
    withAsyncStorage({
      key: "nice.things",
      // always ensures that nickelback is loaded into MST as `false` here
      onLoad: snapshot => {
        return {
          ...snapshot,
          nickleback: false,
        }
      },
      // always ensures that nickelback is saved to AsyncStorage as `false` here
      onSave: snapshot => {
        return {
          ...snapshot,
          nickelback: false,
        }
      },
    }),
  )

Options

withAsyncStorage() accepts an optional object as a parameter with these keys:

key type what it does
key string The key to use when saving to AsyncStorage (default: the model type name)
autoSave boolean Should we automatically save when any values change on the model? (default: true)
only string, string[] will only include the keys with these names
except string, string[] will omit keys with these names
onLoad (snapshot) => any Will be called after state is loaded from AsyncStorage. Return snapshot to change it.
onSave (snapshot) => any Will be called before AsyncStorage, allowing you to modify the snapshot by returning it.

Contributing?

Yes plz!

Fork it, Clone it, Branch it, Yarn it
Build it, Test it, Push it, PR it

To run the tests, I like to open two shells yarn test:compile:watch and yarn test --watch. If you run ava manually, make sure to add -s to run the tests serially because I fail mocking (because the mocks are shared state and bleed into each other).