This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
observer.js
322 lines (289 loc) · 9.99 KB
/
observer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {Atom, Reaction, extras} from 'mobx';
import React from 'react';
import ReactDOM from 'react-dom';
import EventEmitter from './utils/EventEmitter';
import inject from './inject';
/**
* dev tool support
*/
let isDevtoolsEnabled = false;
let isUsingStaticRendering = false;
let warnedAboutObserverInjectDeprecation = false;
// WeakMap<Node, Object>;
export const componentByNodeRegistery = typeof WeakMap !== "undefined" ? new WeakMap() : undefined;
export const renderReporter = new EventEmitter();
function findDOMNode(component) {
if (ReactDOM)
return ReactDOM.findDOMNode(component);
return null;
}
function reportRendering(component) {
const node = findDOMNode(component);
if (node && componentByNodeRegistery)
componentByNodeRegistery.set(node, component);
renderReporter.emit({
event: 'render',
renderTime: component.__$mobRenderEnd - component.__$mobRenderStart,
totalTime: Date.now() - component.__$mobRenderStart,
component: component,
node: node
});
}
export function trackComponents() {
if (typeof WeakMap === "undefined")
throw new Error("[mobx-react] tracking components is not supported in this browser.");
if (!isDevtoolsEnabled)
isDevtoolsEnabled = true;
}
export function useStaticRendering(useStaticRendering) {
isUsingStaticRendering = useStaticRendering;
}
/**
* Utilities
*/
function patch(target, funcName, runMixinFirst = false) {
const base = target[funcName];
const mixinFunc = reactiveMixin[funcName];
if (!base) {
target[funcName] = mixinFunc;
} else {
target[funcName] = runMixinFirst === true
? function() {
mixinFunc.apply(this, arguments);
base.apply(this, arguments);
}
: function() {
base.apply(this, arguments);
mixinFunc.apply(this, arguments);
}
}
}
function isObjectShallowModified(prev, next) {
if (null == prev || null == next || typeof prev !== "object" || typeof next !== "object") {
return prev !== next;
}
const keys = Object.keys(prev);
if (keys.length !== Object.keys(next).length) {
return true;
}
let key;
for (let i = keys.length - 1; i >= 0, key = keys[i]; i--) {
if (next[key] !== prev[key]) {
return true;
}
}
return false;
}
/**
* ReactiveMixin
*/
const reactiveMixin = {
componentWillMount: function() {
if (isUsingStaticRendering === true)
return;
// Generate friendly name for debugging
const initialName = this.displayName
|| this.name
|| (this.constructor && (this.constructor.displayName || this.constructor.name))
|| "<component>";
const rootNodeID = this._reactInternalInstance && this._reactInternalInstance._rootNodeID;
/**
* If props are shallowly modified, react will render anyway,
* so atom.reportChanged() should not result in yet another re-render
*/
let skipRender = false;
/**
* forceUpdate will re-assign this.props. We don't want that to cause a loop,
* so detect these changes
*/
let isForcingUpdate = false;
function makePropertyObservableReference(propName) {
let valueHolder = this[propName];
const atom = new Atom("reactive " + propName);
Object.defineProperty(this, propName, {
configurable: true, enumerable: true,
get: function() {
atom.reportObserved();
return valueHolder;
},
set: function set(v) {
if (!isForcingUpdate && isObjectShallowModified(valueHolder, v)) {
valueHolder = v;
skipRender = true;
atom.reportChanged();
skipRender = false;
} else {
valueHolder = v;
}
}
})
}
// make this.props an observable reference, see #124
makePropertyObservableReference.call(this, "props")
// make state an observable reference
makePropertyObservableReference.call(this, "state")
// wire up reactive render
const baseRender = this.render.bind(this);
let reaction = null;
let isRenderingPending = false;
const initialRender = () => {
reaction = new Reaction(`${initialName}#${rootNodeID}.render()`, () => {
if (!isRenderingPending) {
// N.B. Getting here *before mounting* means that a component constructor has side effects (see the relevant test in misc.js)
// This unidiomatic React usage but React will correctly warn about this so we continue as usual
// See #85 / Pull #44
isRenderingPending = true;
if (typeof this.componentWillReact === "function")
this.componentWillReact(); // TODO: wrap in action?
if (this.__$mobxIsUnmounted !== true) {
// If we are unmounted at this point, componentWillReact() had a side effect causing the component to unmounted
// TODO: remove this check? Then react will properly warn about the fact that this should not happen? See #73
// However, people also claim this migth happen during unit tests..
let hasError = true;
try {
isForcingUpdate = true;
if (!skipRender)
React.Component.prototype.forceUpdate.call(this);
hasError = false;
} finally {
isForcingUpdate = false;
if (hasError)
reaction.dispose();
}
}
}
});
reactiveRender.$mobx = reaction;
this.render = reactiveRender;
return reactiveRender();
};
const reactiveRender = () => {
isRenderingPending = false;
let exception = undefined;
let rendering = undefined;
reaction.track(() => {
if (isDevtoolsEnabled) {
this.__$mobRenderStart = Date.now();
}
try {
rendering = extras.allowStateChanges(false, baseRender);
} catch (e) {
exception = e;
}
if (isDevtoolsEnabled) {
this.__$mobRenderEnd = Date.now();
}
});
if (exception)
throw exception;
return rendering;
};
this.render = initialRender;
},
componentWillUnmount: function() {
if (isUsingStaticRendering === true)
return;
this.render.$mobx && this.render.$mobx.dispose();
this.__$mobxIsUnmounted = true;
if (isDevtoolsEnabled) {
const node = findDOMNode(this);
if (node && componentByNodeRegistery) {
componentByNodeRegistery.delete(node);
}
renderReporter.emit({
event: 'destroy',
component: this,
node: node
});
}
},
componentDidMount: function() {
if (isDevtoolsEnabled) {
reportRendering(this);
}
},
componentDidUpdate: function() {
if (isDevtoolsEnabled) {
reportRendering(this);
}
},
shouldComponentUpdate: function(nextProps, nextState) {
if (isUsingStaticRendering) {
console.warn("[mobx-react] It seems that a re-rendering of a React component is triggered while in static (server-side) mode. Please make sure components are rendered only once server-side.");
}
// update on any state changes (as is the default)
if (this.state !== nextState) {
return true;
}
// update if props are shallowly not equal, inspired by PureRenderMixin
// we could return just 'false' here, and avoid the `skipRender` checks etc
// however, it is nicer if lifecycle events are triggered like usually,
// so we return true here if props are shallowly modified.
return isObjectShallowModified(this.props, nextProps);
}
};
/**
* Observer function / decorator
*/
export function observer(arg1, arg2) {
if (typeof arg1 === "string") {
throw new Error("Store names should be provided as array");
}
if (Array.isArray(arg1)) {
// component needs stores
if (!warnedAboutObserverInjectDeprecation) {
warnedAboutObserverInjectDeprecation = true;
console.warn('Mobx observer: Using observer to inject stores is deprecated since 4.0. Use `@inject("store1", "store2") @observer ComponentClass` or `inject("store1", "store2")(observer(componentClass))` instead of `@observer(["store1", "store2"]) ComponentClass`')
}
if (!arg2) {
// invoked as decorator
return componentClass => observer(arg1, componentClass);
} else {
return inject.apply(null, arg1)(observer(arg2));
}
}
const componentClass = arg1;
if (componentClass.isMobxInjector === true) {
console.warn('Mobx observer: You are trying to use \'observer\' on a component that already has \'inject\'. Please apply \'observer\' before applying \'inject\'');
}
// Stateless function component:
// If it is function but doesn't seem to be a react class constructor,
// wrap it to a react class automatically
if (
typeof componentClass === "function" &&
(!componentClass.prototype || !componentClass.prototype.render) && !componentClass.isReactClass && !React.Component.isPrototypeOf(componentClass)
) {
return observer(React.createClass({
displayName: componentClass.displayName || componentClass.name,
propTypes: componentClass.propTypes,
contextTypes: componentClass.contextTypes,
getDefaultProps: function() { return componentClass.defaultProps; },
render: function() { return componentClass.call(this, this.props, this.context); }
}));
}
if (!componentClass) {
throw new Error("Please pass a valid component to 'observer'");
}
const target = componentClass.prototype || componentClass;
mixinLifecycleEvents(target)
componentClass.isMobXReactObserver = true;
return componentClass;
}
function mixinLifecycleEvents(target) {
patch(target, "componentWillMount", true);
[
"componentDidMount",
"componentWillUnmount",
"componentDidUpdate"
].forEach(function(funcName) {
patch(target, funcName)
});
if (!target.shouldComponentUpdate) {
target.shouldComponentUpdate = reactiveMixin.shouldComponentUpdate;
}
}
// TODO: support injection somehow as well?
export const Observer = observer(({ children }) => children())
Observer.propTypes = {
children: React.PropTypes.func.isRequired
}