1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.branch = exports.root = undefined;
|
7 |
|
8 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
9 |
|
10 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
11 |
|
12 | var _react = require('react');
|
13 |
|
14 | var _react2 = _interopRequireDefault(_react);
|
15 |
|
16 | var _baobab = require('baobab');
|
17 |
|
18 | var _baobab2 = _interopRequireDefault(_baobab);
|
19 |
|
20 | var _helpers = require('./utils/helpers');
|
21 |
|
22 | var _deepEqual = require('deep-equal');
|
23 |
|
24 | var _deepEqual2 = _interopRequireDefault(_deepEqual);
|
25 |
|
26 | var _context = require('./context');
|
27 |
|
28 | var _context2 = _interopRequireDefault(_context);
|
29 |
|
30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
31 |
|
32 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
33 |
|
34 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
35 |
|
36 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
37 |
|
38 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var makeError = _baobab2.default.helpers.makeError,
|
47 | isPlainObject = _baobab2.default.type.object;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function displayName(Component) {
|
53 | return Component.name || Component.displayName || 'Component';
|
54 | }
|
55 |
|
56 | function invalidMapping(name, mapping) {
|
57 | throw makeError('baobab-react/higher-order.branch: given cursors mapping is invalid (check the "' + name + '" component).', { mapping: mapping });
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function root(tree, Component) {
|
64 | if (!(0, _helpers.isBaobabTree)(tree)) throw makeError('baobab-react/higher-order.root: given tree is not a Baobab.', { target: tree });
|
65 |
|
66 | if (typeof Component !== 'function') throw Error('baobab-react/higher-order.root: given target is not a valid React component.');
|
67 |
|
68 | var name = displayName(Component);
|
69 |
|
70 | var value = { tree: tree };
|
71 |
|
72 | var ComposedComponent = function (_React$Component) {
|
73 | _inherits(ComposedComponent, _React$Component);
|
74 |
|
75 | function ComposedComponent() {
|
76 | _classCallCheck(this, ComposedComponent);
|
77 |
|
78 | return _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).apply(this, arguments));
|
79 | }
|
80 |
|
81 | _createClass(ComposedComponent, [{
|
82 | key: 'render',
|
83 |
|
84 |
|
85 | value: function render() {
|
86 | return _react2.default.createElement(
|
87 | _context2.default.Provider,
|
88 | { value: value },
|
89 | _react2.default.createElement(Component, this.props)
|
90 | );
|
91 | }
|
92 | }]);
|
93 |
|
94 | return ComposedComponent;
|
95 | }(_react2.default.Component);
|
96 |
|
97 | ComposedComponent.displayName = 'Rooted' + name;
|
98 |
|
99 | return ComposedComponent;
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function branch(cursors, Component) {
|
106 | if (typeof Component !== 'function') throw Error('baobab-react/higher-order.branch: given target is not a valid React component.');
|
107 |
|
108 | var name = displayName(Component);
|
109 |
|
110 | if (!isPlainObject(cursors) && typeof cursors !== 'function') invalidMapping(name, cursors);
|
111 |
|
112 | var ComposedComponent = function (_React$Component2) {
|
113 | _inherits(ComposedComponent, _React$Component2);
|
114 |
|
115 |
|
116 | function ComposedComponent(props, context) {
|
117 | _classCallCheck(this, ComposedComponent);
|
118 |
|
119 |
|
120 | var _this2 = _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).call(this, props, context));
|
121 |
|
122 | _this2.dispatcher = function (fn) {
|
123 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
124 | args[_key - 1] = arguments[_key];
|
125 | }
|
126 |
|
127 | return fn.apply(undefined, [_this2.context.tree].concat(args));
|
128 | };
|
129 |
|
130 | if (cursors) {
|
131 | _this2.mapping = (0, _helpers.solveMapping)(cursors, props, context);
|
132 |
|
133 | if (!_this2.mapping) invalidMapping(name, _this2.mapping);
|
134 |
|
135 | if (!_this2.context || !(0, _helpers.isBaobabTree)(_this2.context.tree)) throw makeError('baobab-react/higher-order.branch: tree is not available.');
|
136 |
|
137 |
|
138 | _this2.watcher = _this2.context.tree.watch(_this2.mapping);
|
139 |
|
140 |
|
141 | _this2.state = _this2.watcher.get();
|
142 | }
|
143 | return _this2;
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | _createClass(ComposedComponent, [{
|
150 | key: 'componentDidMount',
|
151 | value: function componentDidMount() {
|
152 | var _this3 = this;
|
153 |
|
154 | if (!this.watcher) return;
|
155 |
|
156 | var handler = function handler() {
|
157 | if (_this3.watcher) _this3.setState(_this3.watcher.get());
|
158 | };
|
159 |
|
160 | handler();
|
161 | this.watcher.on('update', handler);
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 | }, {
|
167 | key: 'render',
|
168 | value: function render() {
|
169 | var _props = this.props,
|
170 | decoratedComponentRef = _props.decoratedComponentRef,
|
171 | props = _objectWithoutProperties(_props, ['decoratedComponentRef']);
|
172 |
|
173 | var suppl = { dispatch: this.dispatcher };
|
174 |
|
175 | return _react2.default.createElement(Component, _extends({}, props, suppl, this.state, { ref: decoratedComponentRef }));
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 | }, {
|
181 | key: 'componentWillUnmount',
|
182 | value: function componentWillUnmount() {
|
183 | if (!this.watcher) return;
|
184 |
|
185 |
|
186 | this.watcher.release();
|
187 | this.watcher = null;
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 | }, {
|
193 | key: 'componentDidUpdate',
|
194 | value: function componentDidUpdate() {
|
195 | if (!this.watcher || typeof cursors !== 'function') return;
|
196 |
|
197 | var mapping = (0, _helpers.solveMapping)(cursors, this.props, this.context);
|
198 |
|
199 | if (!mapping) invalidMapping(name, mapping);
|
200 |
|
201 | if ((0, _deepEqual2.default)(mapping, this.mapping)) return;
|
202 |
|
203 | this.mapping = mapping;
|
204 |
|
205 |
|
206 | this.watcher.refresh(this.mapping);
|
207 | this.setState(this.watcher.get());
|
208 | }
|
209 | }]);
|
210 |
|
211 | return ComposedComponent;
|
212 | }(_react2.default.Component);
|
213 |
|
214 | ComposedComponent.displayName = 'Branched' + name;
|
215 |
|
216 | ComposedComponent.contextType = _context2.default;
|
217 |
|
218 | return ComposedComponent;
|
219 | }
|
220 |
|
221 |
|
222 | var curriedRoot = (0, _helpers.curry)(root, 2),
|
223 | curriedBranch = (0, _helpers.curry)(branch, 2);
|
224 |
|
225 | exports.root = curriedRoot;
|
226 | exports.branch = curriedBranch; |
\ | No newline at end of file |