Skip to content

Commit

Permalink
Faster item cloning, faster serialisation. Breaks API
Browse files Browse the repository at this point in the history
  • Loading branch information
KoryNunn committed Feb 24, 2015
1 parent 4fa0ca2 commit d694c46
Show file tree
Hide file tree
Showing 13 changed files with 10,904 additions and 37 deletions.
7 changes: 6 additions & 1 deletion action.js
Expand Up @@ -5,7 +5,12 @@ var createSpec = require('spec-js'),

function triggerAction(action, parent, scope, event) {
// clone
action = parent.gaffa.initialiseAction(statham.revive(JSON.parse(statham.stringify(action))));

if(action instanceof ViewItem){
action = action._clone();
}

action = parent.gaffa.initialiseAction(action);

action.on('bind', function(parent, scope){
action.path = action.getPath();
Expand Down
27 changes: 26 additions & 1 deletion bindable.js
@@ -1,8 +1,30 @@
var createSpec = require('spec-js'),
EventEmitter = require('events').EventEmitter,
merge = require('./flatMerge'),
statham = require('statham'),
jsonConverter = require('./jsonConverter');

function clone(object){
var copy = jsonConverter(object, object.__serialiseExclude__, object.__serialiseInclude__);

for(var key in copy){
var item = copy[key];
if(!item || typeof item !== 'object'){
continue;
}else if(typeof copy[key]._clone === 'function'){
copy[key] = item._clone();
}else{
copy[key] = clone(item);
}

if(copy[key] === undefined){
delete copy[key];
}
}

return copy;
}

var stack = [];
function eventually(fn){
stack.push(fn);
Expand Down Expand Up @@ -67,6 +89,9 @@ Bindable.prototype.toJSON = function(){

return tempObject;
};
Bindable.prototype._clone = function(){
return new this.constructor(clone(this));
};

function setupCleanup(bindable, parent){
var onDebind = bindable.debind.bind(bindable);
Expand All @@ -93,7 +118,7 @@ Bindable.prototype.bind = function(parent, scope){
}
if(this._bound){
this.debind();
console.warn('Attempted to bind an already bound item.');
// console.warn('Attempted to bind an already bound item.');
}

if(parent){
Expand Down
14 changes: 13 additions & 1 deletion excludeProps.js
@@ -1 +1,13 @@
module.exports = ["_trackedListeners", "__iuid", "gaffa", "parent", "parentContainer", "renderedElement", "_removeHandlers", "gediCallbacks", "__super__", "_events", "consuela"];
module.exports = {
"_trackedListeners":null,
"__iuid":null,
"gaffa":null,
"parent":null,
"parentContainer":null,
"renderedElement":null,
"_removeHandlers":null,
"gediCallbacks":null,
"__super__":null,
"_events":null,
"consuela":null
};
5 changes: 4 additions & 1 deletion includeProps.js
@@ -1 +1,4 @@
module.exports = ["type", "_type"];
module.exports = {
"type":null,
"_type":null
};
43 changes: 22 additions & 21 deletions jsonConverter.js
Expand Up @@ -2,31 +2,32 @@ var deepEqual = require('deep-equal'),
excludeProps = require('./excludeProps'),
includeProps = require('./includeProps');

function jsonConverter(object, exclude, include){
var plainInstance = new object.constructor(),
tempObject = (Array.isArray(object) || object instanceof Array) ? [] : {};

//console.log(object.constructor.name);

if(exclude){
excludeProps = excludeProps.concat(exclude);
function addProp(plainInstance, object, key, extraExclude, extraInclude){
var item = object[key]
if(typeof item === 'function'){
return;
}

if(include){
includeProps = includeProps.concat(include);
if(
item !== undefined &&
(includeProps && (key in includeProps)) ||
(extraInclude && ~extraInclude.indexOf(key)) ||
object.hasOwnProperty(key) &&
(excludeProps ? !(key in excludeProps) : true) &&
(extraExclude ? !~extraExclude.indexOf(key) : true) &&
!deepEqual(plainInstance[key], item)
){
return item;
}
}

function jsonConverter(object, extraExclude, extraInclude){
var plainInstance = new object.constructor(),
tempObject = (Array.isArray(object) || object instanceof Array) ? [] : {};

for(var key in object){
if(typeof object[key] === 'function'){
continue;
}
if(
includeProps.indexOf(key)>=0 ||
object.hasOwnProperty(key) &&
excludeProps.indexOf(key)<0 &&
!deepEqual(plainInstance[key], object[key])
){
tempObject[key] = object[key];
var item = addProp(plainInstance, object, key, extraExclude, extraInclude);
if(item !== undefined){
tempObject[key] = item;
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,8 @@
"mockery": "^1.0.0"
},
"scripts": {
"test": "node ./test/test.js"
"test": "node ./test/index.js",
"buildTest": "browserify ./test/index.js > ./test/index.b.js"
},
"testling": {
"files": "test/*.js",
Expand Down
7 changes: 3 additions & 4 deletions property.js
Expand Up @@ -9,8 +9,7 @@ var createSpec = require('spec-js'),
requestAnimationFrame = animationFrame.requestAnimationFrame,
cancelAnimationFrame = animationFrame.cancelAnimationFrame,
resolvePath = require('./resolvePath'),
excludeProps = require('./excludeProps'),
includeProps = require('./includeProps');
excludeProps = require('./excludeProps');

var nextFrame;

Expand Down Expand Up @@ -143,8 +142,8 @@ function inflateProperty(property, propertyDescription){

if(
isProperty && (
~propertyDescription.__serialiseExclude__.indexOf(key) ||
~excludeProps.indexOf(key)
~propertyDescription.__serialiseExclude__.indexOf(key) ||
key in excludeProps
)
){
continue;
Expand Down
15 changes: 10 additions & 5 deletions templaterProperty.js
@@ -1,4 +1,5 @@
var Property = require('./property'),
View = require('./view'),
statham = require('statham'),
createSpec = require('spec-js');

Expand Down Expand Up @@ -61,8 +62,6 @@ TemplaterProperty.prototype.update =function (view, value) {
if(!this.template){
return;
}
this._templateCache || (this._templateCache = this.template && JSON.parse(statham.stringify(this.template)));
this._emptyTemplateCache || (this._emptyTemplateCache = this.emptyTemplate && JSON.parse(statham.stringify(this.emptyTemplate)));
var property = this,
gaffa = this.gaffa,
paths = gaffa.gedi.paths,
Expand All @@ -73,6 +72,12 @@ TemplaterProperty.prototype.update =function (view, value) {
isEmpty = true,
subPaths = sourcePathInfo && sourcePathInfo.subPaths;

this.template = gaffa.initialiseView(this.template);

if(this.emptyTemplate){
this.emptyTemplate = gaffa.initialiseView(this.emptyTemplate);
}

childViews.abortDeferredAdd();

if (value && typeof value === "object" && sourcePathInfo){
Expand Down Expand Up @@ -126,7 +131,7 @@ TemplaterProperty.prototype.update =function (view, value) {
}

if(!existingChild){
newView = statham.revive(this._templateCache);
newView = this.template._clone();
newView._item = value[key];
newView.scope = {item: newView._item, key: key};
newView.sourcePath = property.ignorePaths ? null : sourcePath;
Expand Down Expand Up @@ -155,8 +160,8 @@ TemplaterProperty.prototype.update =function (view, value) {
i--;
}
}
if(this._emptyTemplateCache){
newView = gaffa.initialiseView(statham.revive(this._emptyTemplateCache));
if(this.emptyTemplate){
newView = this.emptyTemplate._clone();
newView.containerName = viewsName;
childViews.add(newView, itemIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion test/browser.html
@@ -1,2 +1,2 @@
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height">
<script src="event-debind.browser.js"></script>
<script src="index.b.js"></script>

0 comments on commit d694c46

Please sign in to comment.