Skip to content

Commit

Permalink
Performance and bugfixes. 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Jul 14, 2014
1 parent 86160f5 commit c303cb2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion example/b.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var React = require('react');
var B = React.createClass({
render: function() {
return (
<div style={{background: 'purple',color: 'white'}}>
<div style={{background: 'purple', color: 'white'}}>
<p>I am <code>example/b.jsx</code>, feel free to edit me.</p>
<img src='http://facebook.github.io/react/img/logo_og.png' width='200' />
</div>
Expand Down
90 changes: 48 additions & 42 deletions hot.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'use strict';

var setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
/* jshint proto:true */
obj.__proto__ = proto;
return obj;
};

module.exports = function (React) {
var mounted = [];
var Mixin = {
Expand All @@ -19,63 +13,75 @@ module.exports = function (React) {
};

var assimilatePrototype = (function () {
var storedPrototype;

function assimilateProperty(freshPrototype, key) {
function get() {
if (typeof storedPrototype[key] !== 'function' ||
key === 'type' ||
key === 'constructor') {
var storedPrototype,
knownPrototypes = [];

return storedPrototype[key];
function wrapFunction(key) {
return function () {
if (storedPrototype[key]) {
return storedPrototype[key].apply(this, arguments);
}
};
}

return function () {
var value = storedPrototype[key];
if (typeof value === 'function') {
return value.apply(this, arguments);
} else {
console.warn('A call to ' + key + ' was made after it was deleted. Acting as no-op.');
}
};
}
function patchProperty(proto, key) {
proto[key] = storedPrototype[key];

function set(value) {
storedPrototype[key] = value;
if (typeof proto[key] !== 'function' ||
key === 'type' ||
key === 'constructor') {
return;
}

storedPrototype[key] = freshPrototype[key];
Object.defineProperty(freshPrototype, key, {
configurable: false,
enumerable: true,
get: get,
set: set
});
proto[key] = wrapFunction(key);

if (proto.__reactAutoBindMap[key]) {
proto.__reactAutoBindMap[key] = proto[key];
}
}

return function assimilatePrototype(freshPrototype) {
function updateStoredPrototype(freshPrototype) {
storedPrototype = {};

for (var key in freshPrototype) {
assimilateProperty(freshPrototype, key);
if (freshPrototype.hasOwnProperty(key)) {
storedPrototype[key] = freshPrototype[key];
}
}
}

function reconcileWithStoredPrototypes(freshPrototype) {
knownPrototypes.push(freshPrototype);
knownPrototypes.forEach(function (proto) {
for (var key in storedPrototype) {
patchProperty(proto, key);
}
});
}

return function (freshPrototype) {
updateStoredPrototype(freshPrototype);
reconcileWithStoredPrototypes(freshPrototype);
};
})();

function injectMixinAndAssimilatePrototype(spec) {
spec.mixins = spec.mixins || [];
spec.mixins.push(Mixin);
var Component = React.createClass(spec);
assimilatePrototype(Component.type.prototype);
return Component;
}

var Component;
return {
createClass: function (spec) {
spec.mixins = spec.mixins || [];
spec.mixins.push(Mixin);

Component = React.createClass(spec);
assimilatePrototype(Component.componentConstructor.prototype);

Component = injectMixinAndAssimilatePrototype(spec);
return Component;
},

updateClass: function (spec) {
var UpdatedComponent = React.createClass(spec);
assimilatePrototype(UpdatedComponent.componentConstructor.prototype);
injectMixinAndAssimilatePrototype(spec);

mounted.forEach(function (instance) {
instance._bindAutoBindMethods();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hot-loader",
"version": "0.1.5",
"version": "0.1.6",
"description": "Webpack loader that enables live-editing React components without unmounting or losing their state",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit c303cb2

Please sign in to comment.