UNPKG

4.35 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(state, dispatch, action.vnode);
61 break;
62 case UPDATE_THUNK:
63 // Prevent the clearing of dirtiness
64 // / updating of state if we're just
65 // rendering a cached node
66 if (!action.vnode.vnode) {
67 delete dirty[action.vnode.path];
68 update(state, action.vnode, action.prev);
69 }
70 break;
71 case DESTROY_THUNK:
72 delete dirty[action.vnode.path];
73 destroy(dispatch, action.vnode);
74 break;
75 }
76
77 if ((0, _reduxEphemeral.isEphemeral)(action)) {
78 var prevState = getState();
79 var result = next(action);
80 var nextState = getState();
81
82 if (prevState !== nextState) {
83 dirty[action.meta.ephemeral.key] = true;
84 }
85
86 return result;
87 }
88
89 return next(action);
90 };
91 };
92 };
93}
94
95function create(getState, dispatch, thunk) {
96 var component = thunk.type;
97 var _component$initialSta = component.initialState;
98 var initialState = _component$initialSta === undefined ? function () {
99 return {};
100 } : _component$initialSta;
101
102
103 var priorState = (0, _reduxEphemeral.lookup)(getState(), thunk.path);
104 prepare(thunk, priorState || initialState);
105
106 // If a component does not have a reducer, it does not
107 // get any local state
108 if (component.reducer && !priorState) {
109 dispatch((0, _reduxEphemeral.createEphemeral)(thunk.path, thunk.state));
110 }
111}
112
113function update(getState, thunk, prev) {
114 prepare(thunk, (0, _reduxEphemeral.lookup)(getState(), thunk.path));
115}
116
117function destroy(dispatch, thunk) {
118 thunk.type.reducer && dispatch((0, _reduxEphemeral.destroyEphemeral)(thunk.path));
119}
120
121function shouldUpdate(prev, next) {
122 return prev.state !== next.state || !(0, _arrayEqual2.default)(prev.children, next.children) || !(0, _objectEqual2.default)(prev.props, next.props);
123}
124
125function prepare(thunk, state) {
126 thunk.type.shouldUpdate = thunk.type.shouldUpdate || shouldUpdate;
127 thunk.local = function (fn) {
128 for (var _len = arguments.length, outerArgs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
129 outerArgs[_key - 1] = arguments[_key];
130 }
131
132 if (typeof fn !== 'function') throw new Error('virtex-local: non-function passed to `local()`. Did you pass the wrong handler?');
133 return function () {
134 for (var _len2 = arguments.length, innerArgs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
135 innerArgs[_key2] = arguments[_key2];
136 }
137
138 return (0, _reduxEphemeral.toEphemeral)(thunk.path, thunk.type.reducer, fn.apply(thunk, outerArgs.concat(innerArgs)));
139 };
140 };
141
142 thunk.state = typeof state === 'function' ? state(thunk) : state;
143}
144
145/**
146 * Exports
147 */
148
149exports.default = local;
150exports.mount = _reduxEphemeral2.default;
\No newline at end of file