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