Created
July 30, 2019 10:40
-
-
Save iMoses/b1bbc8b16ead4d9e3a446450fff72816 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serialize, deserialize, serializable, custom } from 'serializr'; | |
import { deepObserve } from 'mobx-utils'; | |
import storage from 'utils/storage'; | |
import _ from 'lodash'; | |
export const deserializable = serializable(custom(() => {}, v => v)); | |
export const fromStorage = (model, defaults = {}) => | |
deserialize(model, _.merge(defaults, storage.get(model.name, {}))); | |
export const syncStorage = (model, obj, conditions) => { | |
const sync = _.throttle(() => storage.set(model.name, serialize(obj)), 0, {leading: false}); | |
deepObserve(obj, change => conditions ? conditions(change) && sync() : sync()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment