Skip to content

Instantly share code, notes, and snippets.

@iMoses
Created July 30, 2019 10:40
Show Gist options
  • Save iMoses/b1bbc8b16ead4d9e3a446450fff72816 to your computer and use it in GitHub Desktop.
Save iMoses/b1bbc8b16ead4d9e3a446450fff72816 to your computer and use it in GitHub Desktop.
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