forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobx-react.js
More file actions
executable file
·213 lines (189 loc) · 8.43 KB
/
mobx-react.js
File metadata and controls
executable file
·213 lines (189 loc) · 8.43 KB
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
;(function() {
function mrFactory(mobx, React, ReactDOM) {
if (!mobx) throw new Error("mobx-react requires the MobX package")
if (!React) throw new Error("mobx-react requires React to be available")
var isDevtoolsEnabled = false
// WeakMap<Node, Object>;
var componentByNodeRegistery = typeof WeakMap !== "undefined" ? new WeakMap() : undefined
var renderReporter = new mobx.SimpleEventEmitter()
function findDOMNode(component) {
if (ReactDOM) return ReactDOM.findDOMNode(component)
return null
}
function reportRendering(component) {
var node = findDOMNode(component)
if (node) componentByNodeRegistery.set(node, component)
renderReporter.emit({
event: "render",
renderTime: component.__$mobRenderEnd - component.__$mobRenderStart,
totalTime: Date.now() - component.__$mobRenderStart,
component: component,
node: node
})
}
var reactiveMixin = {
componentWillMount: function() {
// Generate friendly name for debugging
var name = [
this.displayName ||
this.name ||
(this.constructor && this.constructor.name) ||
"<component>",
"#",
this._reactInternalInstance && this._reactInternalInstance._rootNodeID,
".render()"
].join("")
var baseRender = this.render.bind(this)
var self = this
var reaction = null
var isRenderingPending = false
function initialRender() {
reaction = new mobx.Reaction(name, function() {
if (!isRenderingPending) {
isRenderingPending = true
React.Component.prototype.forceUpdate.call(self)
}
})
reactiveRender.$mobx = reaction
self.render = reactiveRender
return reactiveRender()
}
function reactiveRender() {
isRenderingPending = false
var rendering
reaction.track(function() {
if (isDevtoolsEnabled) self.__$mobRenderStart = Date.now()
rendering = mobx.extras.allowStateChanges(false, baseRender)
if (isDevtoolsEnabled) self.__$mobRenderEnd = Date.now()
})
return rendering
}
this.render = initialRender
},
componentWillUnmount: function() {
this.render.$mobx && this.render.$mobx.dispose()
if (isDevtoolsEnabled) {
var node = findDOMNode(this)
if (node) {
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) {
// TODO: if context changed, return true.., see #18
// if props or state did change, but a render was scheduled already, no additional render needs to be scheduled
if (this.render.$mobx && this.render.$mobx.isScheduled() === true) return false
// 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
var keys = Object.keys(this.props)
var key
if (keys.length !== Object.keys(nextProps).length) return true
for (var i = keys.length - 1; i >= 0, (key = keys[i]); i--) {
var newValue = nextProps[key]
if (newValue !== this.props[key]) {
return true
} else if (
newValue &&
typeof newValue === "object" &&
!mobx.isObservable(newValue)
) {
/**
* If the newValue is still the same object, but that object is not observable,
* fallback to the default React behavior: update, because the object *might* have changed.
* If you need the non default behavior, just use the React pure render mixin, as that one
* will work fine with mobx as well, instead of the default implementation of
* observer.
*/
return true
}
}
return false
}
}
function patch(target, funcName) {
var base = target[funcName]
var mixinFunc = reactiveMixin[funcName]
target[funcName] = function() {
base && base.apply(this, arguments)
mixinFunc.apply(this, arguments)
}
}
function observer(componentClass) {
// 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.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'")
var target = componentClass.prototype || componentClass
;[
"componentWillMount",
"componentWillUnmount",
"componentDidMount",
"componentDidUpdate"
].forEach(function(funcName) {
patch(target, funcName)
})
if (!target.shouldComponentUpdate)
target.shouldComponentUpdate = reactiveMixin.shouldComponentUpdate
componentClass.isMobXReactObserver = true
return componentClass
}
function trackComponents() {
if (typeof WeakMap === "undefined")
throw new Error(
"[mobx-react] tracking components is not supported in this browser."
)
if (!isDevtoolsEnabled) isDevtoolsEnabled = true
}
return {
observer: observer,
reactiveComponent: function() {
console.warn(
"[mobx-react] `reactiveComponent` has been renamed to `observer` and will be removed in 1.1."
)
return observer.apply(null, arguments)
},
renderReporter: renderReporter,
componentByNodeRegistery: componentByNodeRegistery,
trackComponents: trackComponents
}
}
// UMD
if (typeof define === "function" && define.amd) {
define("mobx-react", ["mobx", "react", "react-dom"], mrFactory)
} else if (typeof exports === "object") {
module.exports = mrFactory(require("mobx"), require("react"), require("react-dom"))
} else {
this.mobxReact = mrFactory(this["mobx"], this["React"], this["ReactDOM"])
}
})()