UNPKG

2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.enableRio = enableRio;
7
8var _lodash = require('lodash');
9
10var _lodash2 = _interopRequireDefault(_lodash);
11
12var _reduxBatchedActions = require('redux-batched-actions');
13
14var _rio = require('./rio');
15
16var _rio2 = _interopRequireDefault(_rio);
17
18var _status = require('./status');
19
20var _ReduxApiStateDenormalizer = require('./denormalizer/ReduxApiStateDenormalizer');
21
22var _ReduxApiStateDenormalizer2 = _interopRequireDefault(_ReduxApiStateDenormalizer);
23
24var _storage = require('./reducers/storage');
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
29
30function discoverSchemaPaths(obj) {
31 var currentPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
32 var discoveredPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
33
34 var status = (0, _status.getStatus)(obj);
35 if (status && status.type === _storage.STORAGE_TYPE) {
36 // TODO: add validation if schema path are repeating to throw warning
37 // eslint-disable-next-line no-param-reassign
38 discoveredPaths[status.schema] = currentPath;
39 return discoveredPaths;
40 }
41
42 _lodash2.default.forOwn(obj, function (propValue, prop) {
43 if (_lodash2.default.isPlainObject(propValue)) {
44 discoverSchemaPaths(propValue, [].concat(_toConsumableArray(currentPath), [prop]), discoveredPaths);
45 }
46 });
47
48 return discoveredPaths;
49}
50
51function enableRio(reducer) {
52 var initialState = reducer(undefined, { type: 'unknown' });
53 var paths = discoverSchemaPaths(initialState);
54
55 _rio2.default.setSchemaPaths(paths);
56 _rio2.default.setDenormalizer(new _ReduxApiStateDenormalizer2.default());
57
58 return (0, _reduxBatchedActions.enableBatching)(reducer);
59}
\No newline at end of file