Skip to content

Commit c303cb2

Browse files
committed
Performance and bugfixes. 0.1.6
1 parent 86160f5 commit c303cb2

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

example/b.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var React = require('react');
55
var B = React.createClass({
66
render: function() {
77
return (
8-
<div style={{background: 'purple',color: 'white'}}>
8+
<div style={{background: 'purple', color: 'white'}}>
99
<p>I am <code>example/b.jsx</code>, feel free to edit me.</p>
1010
<img src='http://facebook.github.io/react/img/logo_og.png' width='200' />
1111
</div>

hot.js

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict';
22

3-
var setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
4-
/* jshint proto:true */
5-
obj.__proto__ = proto;
6-
return obj;
7-
};
8-
93
module.exports = function (React) {
104
var mounted = [];
115
var Mixin = {
@@ -19,63 +13,75 @@ module.exports = function (React) {
1913
};
2014

2115
var assimilatePrototype = (function () {
22-
var storedPrototype;
23-
24-
function assimilateProperty(freshPrototype, key) {
25-
function get() {
26-
if (typeof storedPrototype[key] !== 'function' ||
27-
key === 'type' ||
28-
key === 'constructor') {
16+
var storedPrototype,
17+
knownPrototypes = [];
2918

30-
return storedPrototype[key];
19+
function wrapFunction(key) {
20+
return function () {
21+
if (storedPrototype[key]) {
22+
return storedPrototype[key].apply(this, arguments);
3123
}
24+
};
25+
}
3226

33-
return function () {
34-
var value = storedPrototype[key];
35-
if (typeof value === 'function') {
36-
return value.apply(this, arguments);
37-
} else {
38-
console.warn('A call to ' + key + ' was made after it was deleted. Acting as no-op.');
39-
}
40-
};
41-
}
27+
function patchProperty(proto, key) {
28+
proto[key] = storedPrototype[key];
4229

43-
function set(value) {
44-
storedPrototype[key] = value;
30+
if (typeof proto[key] !== 'function' ||
31+
key === 'type' ||
32+
key === 'constructor') {
33+
return;
4534
}
4635

47-
storedPrototype[key] = freshPrototype[key];
48-
Object.defineProperty(freshPrototype, key, {
49-
configurable: false,
50-
enumerable: true,
51-
get: get,
52-
set: set
53-
});
36+
proto[key] = wrapFunction(key);
37+
38+
if (proto.__reactAutoBindMap[key]) {
39+
proto.__reactAutoBindMap[key] = proto[key];
40+
}
5441
}
5542

56-
return function assimilatePrototype(freshPrototype) {
43+
function updateStoredPrototype(freshPrototype) {
5744
storedPrototype = {};
45+
5846
for (var key in freshPrototype) {
59-
assimilateProperty(freshPrototype, key);
47+
if (freshPrototype.hasOwnProperty(key)) {
48+
storedPrototype[key] = freshPrototype[key];
49+
}
6050
}
51+
}
52+
53+
function reconcileWithStoredPrototypes(freshPrototype) {
54+
knownPrototypes.push(freshPrototype);
55+
knownPrototypes.forEach(function (proto) {
56+
for (var key in storedPrototype) {
57+
patchProperty(proto, key);
58+
}
59+
});
60+
}
61+
62+
return function (freshPrototype) {
63+
updateStoredPrototype(freshPrototype);
64+
reconcileWithStoredPrototypes(freshPrototype);
6165
};
6266
})();
6367

68+
function injectMixinAndAssimilatePrototype(spec) {
69+
spec.mixins = spec.mixins || [];
70+
spec.mixins.push(Mixin);
71+
var Component = React.createClass(spec);
72+
assimilatePrototype(Component.type.prototype);
73+
return Component;
74+
}
75+
6476
var Component;
6577
return {
6678
createClass: function (spec) {
67-
spec.mixins = spec.mixins || [];
68-
spec.mixins.push(Mixin);
69-
70-
Component = React.createClass(spec);
71-
assimilatePrototype(Component.componentConstructor.prototype);
72-
79+
Component = injectMixinAndAssimilatePrototype(spec);
7380
return Component;
7481
},
7582

7683
updateClass: function (spec) {
77-
var UpdatedComponent = React.createClass(spec);
78-
assimilatePrototype(UpdatedComponent.componentConstructor.prototype);
84+
injectMixinAndAssimilatePrototype(spec);
7985

8086
mounted.forEach(function (instance) {
8187
instance._bindAutoBindMethods();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-hot-loader",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Webpack loader that enables live-editing React components without unmounting or losing their state",
55
"main": "index.js",
66
"directories": {

0 commit comments

Comments
 (0)