UNPKG

2.64 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.dehydrate = dehydrate;
7exports.hydrate = hydrate;
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10
11function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
13var _rx = require('rx');
14
15var _invariant = require('invariant');
16
17var _invariant2 = _interopRequireDefault(_invariant);
18
19var _debug = require('debug');
20
21var _debug2 = _interopRequireDefault(_debug);
22
23var _utils = require('./utils');
24
25var assign = Object.assign;
26var __DEV__ = process.env.NODE_ENV === 'development';
27var debug = (0, _debug2['default'])('thundercats:translate');
28
29function dehydrate(cat) {
30 if (__DEV__) {
31 (0, _invariant2['default'])((0, _utils.isKitten)(cat), 'dehydrate should get an instance of the cat, but got %s', cat);
32 }
33
34 return _rx.Observable.from(cat.stores.values())
35 // store must have displayName and value
36 .filter(function (store) {
37 return !!(0, _utils.getNameOrNull)(store) && !!store.value;
38 }).map(function (store) {
39 return _defineProperty({}, (0, _utils.getName)(store), store.value);
40 }).reduce(function (allDats, storeDats) {
41 return assign(allDats, storeDats);
42 }, {}).map(function (allDats) {
43 return allDats;
44 }).tapOnError(function (err) {
45 debug('an error occurred while dehydrating stores', err);
46 });
47}
48
49function hydrate(cat, storesState) {
50 if (__DEV__) {
51 (0, _invariant2['default'])((0, _utils.isKitten)(cat), 'dehydrate should get an instance of the cat, but got %s', cat);
52 }
53
54 return _rx.Observable.combineLatest([_rx.Observable.from(cat.stores.values()), _rx.Observable.just(storesState).tap((0, _utils.createObjectValidator)('hydrate should get objects but got %s'))], function (store, stateMap) {
55 return {
56 store: store,
57 data: stateMap[(0, _utils.getNameOrNull)(store)]
58 };
59 })
60 // filter out falsey data and non objects
61 .filter(function (_ref2) {
62 var data = _ref2.data;
63 return data && typeof data === 'object';
64 })
65 // assign value to store
66 .tap(function (_ref3) {
67 var store = _ref3.store;
68 var data = _ref3.data;
69
70 debug('updating %s with value: ', (0, _utils.getName)(store), data);
71 store.value = data;
72 })
73 // wait to run through all the stores
74 ['do'](null, function (err) {
75 return debug('deserialize encountered a err', err);
76 }, function () {
77 return debug('deserialize completed');
78 }).last().map(function () {
79 return cat;
80 });
81}
\No newline at end of file