1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.dehydrate = dehydrate;
|
7 | exports.hydrate = hydrate;
|
8 |
|
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
10 |
|
11 | function _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 |
|
13 | var _rx = require('rx');
|
14 |
|
15 | var _invariant = require('invariant');
|
16 |
|
17 | var _invariant2 = _interopRequireDefault(_invariant);
|
18 |
|
19 | var _debug = require('debug');
|
20 |
|
21 | var _debug2 = _interopRequireDefault(_debug);
|
22 |
|
23 | var _utils = require('./utils');
|
24 |
|
25 | var assign = Object.assign;
|
26 | var __DEV__ = process.env.NODE_ENV === 'development';
|
27 | var debug = (0, _debug2['default'])('thundercats:translate');
|
28 |
|
29 | function 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 |
|
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 |
|
49 | function 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 |
|
61 | .filter(function (_ref2) {
|
62 | var data = _ref2.data;
|
63 | return data && typeof data === 'object';
|
64 | })
|
65 |
|
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 |
|
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 |