UNPKG

3.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.branch = exports.root = undefined;
7
8var _propTypes = require('./utils/prop-types');
9
10var _propTypes2 = _interopRequireDefault(_propTypes);
11
12var _helpers = require('./utils/helpers');
13
14var _baobab = require('baobab');
15
16var _baobab2 = _interopRequireDefault(_baobab);
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20var makeError = _baobab2.default.helpers.makeError;
21
22/**
23 * Helpers
24 */
25/**
26 * Baobab-React Mixins
27 * ====================
28 *
29 * Old style react mixins.
30 */
31function displayName(instance) {
32 return (instance.constructor || {}).displayName || 'Component';
33}
34
35/**
36 * Root mixin
37 */
38var RootMixin = {
39
40 // Component prop types
41 propTypes: {
42 tree: _propTypes2.default.baobab
43 },
44
45 // Context prop types
46 childContextTypes: {
47 tree: _propTypes2.default.baobab
48 },
49
50 // Handling child context
51 getChildContext: function getChildContext() {
52 return {
53 tree: this.props.tree
54 };
55 }
56};
57
58/**
59 * Branch mixin
60 */
61var BranchMixin = {
62
63 // Retrieving the tree from context
64 contextTypes: {
65 tree: _propTypes2.default.baobab
66 },
67
68 // Building initial state
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 // If the solved mapping is not valid, we throw
78 if (!mapping) throw makeError('baobab-react/mixins.branch: given mapping is invalid (check the "' + name + '" component).', { mapping: mapping });
79
80 // Creating the watcher
81 this.__watcher = this.context.tree.watch(mapping);
82
83 // Building initial state
84 return this.__watcher.get();
85 }
86
87 return null;
88 },
89
90
91 // On component mount
92 componentWillMount: function componentWillMount() {
93 var _this = this;
94
95 // Creating dispatcher
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 // On component unmount
115 componentWillUnmount: function componentWillUnmount() {
116 if (!this.__watcher) return;
117
118 // Releasing facet
119 this.__watcher.release();
120 this.__watcher = null;
121 },
122
123
124 // On new props
125 componentWillReceiveProps: function componentWillReceiveProps(props) {
126 if (!this.__watcher || typeof this.__cursorsMapping !== 'function') return;
127
128 var name = displayName(this);
129
130 // Refreshing the watcher
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
140exports.root = RootMixin;
141exports.branch = BranchMixin;
\No newline at end of file