1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.branch = exports.root = undefined;
|
7 |
|
8 | var _propTypes = require('./utils/prop-types');
|
9 |
|
10 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
11 |
|
12 | var _helpers = require('./utils/helpers');
|
13 |
|
14 | var _baobab = require('baobab');
|
15 |
|
16 | var _baobab2 = _interopRequireDefault(_baobab);
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | var makeError = _baobab2.default.helpers.makeError;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function displayName(instance) {
|
32 | return (instance.constructor || {}).displayName || 'Component';
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var RootMixin = {
|
39 |
|
40 |
|
41 | propTypes: {
|
42 | tree: _propTypes2.default.baobab
|
43 | },
|
44 |
|
45 |
|
46 | childContextTypes: {
|
47 | tree: _propTypes2.default.baobab
|
48 | },
|
49 |
|
50 |
|
51 | getChildContext: function getChildContext() {
|
52 | return {
|
53 | tree: this.props.tree
|
54 | };
|
55 | }
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | var BranchMixin = {
|
62 |
|
63 |
|
64 | contextTypes: {
|
65 | tree: _propTypes2.default.baobab
|
66 | },
|
67 |
|
68 |
|
69 | getInitialState: function getInitialState() {
|
70 | var name = displayName(this);
|
71 |
|
72 | if (this.cursors) {
|
73 | this.__cursorsMapping = this.cursors;
|
74 |
|
75 | var mapping = (0, _helpers.solveMapping)(this.__cursorsMapping, this.props, this.context);
|
76 |
|
77 |
|
78 | if (!mapping) throw makeError('baobab-react/mixins.branch: given mapping is invalid (check the "' + name + '" component).', { mapping: mapping });
|
79 |
|
80 |
|
81 | this.__watcher = this.context.tree.watch(mapping);
|
82 |
|
83 |
|
84 | return this.__watcher.get();
|
85 | }
|
86 |
|
87 | return null;
|
88 | },
|
89 |
|
90 |
|
91 |
|
92 | componentWillMount: function componentWillMount() {
|
93 | var _this = this;
|
94 |
|
95 |
|
96 | this.dispatch = function (fn) {
|
97 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
98 | args[_key - 1] = arguments[_key];
|
99 | }
|
100 |
|
101 | return fn.apply(undefined, [_this.context.tree].concat(args));
|
102 | };
|
103 |
|
104 | if (!this.__watcher) return;
|
105 |
|
106 | var handler = function handler() {
|
107 | if (_this.__watcher) _this.setState(_this.__watcher.get());
|
108 | };
|
109 |
|
110 | this.__watcher.on('update', handler);
|
111 | },
|
112 |
|
113 |
|
114 |
|
115 | componentWillUnmount: function componentWillUnmount() {
|
116 | if (!this.__watcher) return;
|
117 |
|
118 |
|
119 | this.__watcher.release();
|
120 | this.__watcher = null;
|
121 | },
|
122 |
|
123 |
|
124 |
|
125 | componentWillReceiveProps: function componentWillReceiveProps(props) {
|
126 | if (!this.__watcher || typeof this.__cursorsMapping !== 'function') return;
|
127 |
|
128 | var name = displayName(this);
|
129 |
|
130 |
|
131 | var mapping = (0, _helpers.solveMapping)(this.__cursorsMapping, props, this.context);
|
132 |
|
133 | if (!mapping) throw makeError('baobab-react/mixins.branch: given mapping is invalid (check the "' + name + '" component).', { mapping: mapping });
|
134 |
|
135 | this.__watcher.refresh(mapping);
|
136 | this.setState(this.__watcher.get());
|
137 | }
|
138 | };
|
139 |
|
140 | exports.root = RootMixin;
|
141 | exports.branch = BranchMixin; |
\ | No newline at end of file |