UNPKG

3.67 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.mount = undefined;
7
8var _reduxEphemeral = require('redux-ephemeral');
9
10var _reduxEphemeral2 = _interopRequireDefault(_reduxEphemeral);
11
12var _objectEqual = require('@f/object-equal');
13
14var _objectEqual2 = _interopRequireDefault(_objectEqual);
15
16var _arrayEqual = require('@f/array-equal');
17
18var _arrayEqual2 = _interopRequireDefault(_arrayEqual);
19
20var _getProp = require('@f/get-prop');
21
22var _getProp2 = _interopRequireDefault(_getProp);
23
24var _virtex = require('virtex');
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28/**
29 * Constants
30 */
31
32var _actions$types = _virtex.actions.types; /**
33 * Imports
34 */
35
36var CREATE_THUNK = _actions$types.CREATE_THUNK;
37var UPDATE_THUNK = _actions$types.UPDATE_THUNK;
38var DESTROY_THUNK = _actions$types.DESTROY_THUNK;
39
40/**
41 * Provide local state to virtex components
42 */
43
44function local(prop) {
45 var dirty = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
46
47 return function (_ref) {
48 var getState = _ref.getState;
49 var dispatch = _ref.dispatch;
50
51 var state = function state() {
52 return (0, _getProp2.default)(prop, getState());
53 };
54
55 return function (next) {
56 return function (action) {
57 switch (action.type) {
58 case CREATE_THUNK:
59 delete dirty[action.vnode.path];
60 create(dispatch, action.vnode);
61 break;
62 case UPDATE_THUNK:
63 delete dirty[action.vnode.path];
64 update(state, action.vnode, action.prev);
65 break;
66 case DESTROY_THUNK:
67 delete dirty[action.vnode.path];
68 destroy(dispatch, action.vnode);
69 break;
70 }
71
72 if ((0, _reduxEphemeral.isEphemeral)(action)) {
73 dirty[action.meta.ephemeral.key] = true;
74 }
75
76 return next(action);
77 };
78 };
79 };
80}
81
82function create(dispatch, thunk) {
83 var component = thunk.type;
84 var _component$initialSta = component.initialState;
85 var initialState = _component$initialSta === undefined ? function () {
86 return {};
87 } : _component$initialSta;
88
89
90 prepare(thunk, initialState(thunk));
91
92 // If a component does not have a reducer, it does not
93 // get any local state
94 if (component.reducer) {
95 component.shouldUpdate = component.shouldUpdate || shouldUpdate;
96 dispatch((0, _reduxEphemeral.createEphemeral)(thunk.path, thunk.state));
97 }
98}
99
100function update(getState, thunk, prev) {
101 prepare(thunk, (0, _reduxEphemeral.lookup)(getState(), thunk.path));
102}
103
104function destroy(dispatch, thunk) {
105 thunk.type.reducer && dispatch((0, _reduxEphemeral.destroyEphemeral)(thunk.path));
106}
107
108function shouldUpdate(prev, next) {
109 return prev.state !== next.state || !(0, _arrayEqual2.default)(prev.children, next.children) || !(0, _objectEqual2.default)(prev.props, next.props);
110}
111
112function prepare(thunk, state) {
113 thunk.state = state;
114 thunk.local = function (fn) {
115 for (var _len = arguments.length, outerArgs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
116 outerArgs[_key - 1] = arguments[_key];
117 }
118
119 return function () {
120 for (var _len2 = arguments.length, innerArgs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
121 innerArgs[_key2] = arguments[_key2];
122 }
123
124 return (0, _reduxEphemeral.toEphemeral)(thunk.path, thunk.type.reducer, fn.apply(thunk, outerArgs.concat(innerArgs)));
125 };
126 };
127}
128
129/**
130 * Exports
131 */
132
133exports.default = local;
134exports.mount = _reduxEphemeral2.default;
\No newline at end of file