AriaFallah / mobx-store

A data store with declarative querying, observable state, and easy undo/redo.
MIT License
282 stars 9 forks source link

React Native support? #31

Closed aksonov closed 8 years ago

aksonov commented 8 years ago

Does it work with react native? Is it possible to load/persist store to RN AsyncLocalStorage?

AriaFallah commented 8 years ago

Does it work with react native?

I think it should. I'm not exactly sure what would prevent it from working with RN.

Is it possible to load/persist store to RN AsyncLocalStorage?

Yep, if you look at the source code for my localstorage adapter that I include with the library, it's extremely simple.

// @flow

function read(source: string) {
  const data = localStorage.getItem(source)
  if (data) {
    return JSON.parse(data)
  }
  return {}
}

function write(dest: string, store: Object) {
  return localStorage.setItem(dest, JSON.stringify(store.contents()))
}

export default { read, write }

All you would have to do is create your own read and write functions for AsyncLocalStorage. Then you would use it like you would anything else:

import asyncLocalStorage from './your-implementation-of-it'

// Read
const store = mobxstore(asyncLocalStorage.read('whateveryouwant'))

// Write
store.schedule([asyncLocalStorage.write, 'whateveryouwant', store])

Make sense?

aksonov commented 8 years ago

Thanks!

AriaFallah commented 8 years ago

Let me know if you have any other questions.