Skip to content

Commit

Permalink
Not using dirty node internally anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
arqex committed Oct 29, 2015
1 parent a0837af commit 1c17fe3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 137 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,8 @@
# Freezer Changelog
###v0.9.4
* Not working internally with dirty nodes anymore.
* Fixed some nodes getting out of sync when having nested duplicate nodes.

###v0.9.3
* Using messages for nextTick implementation only in browsers. Needed to make freezer work with react native.

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -38,6 +38,7 @@ Do you want to know more?
* [A JSON editor with undo and redo](http://jsbin.com/hugusi/1/edit?js,output), and [here the blog article](http://arqex.com/991/json-editor-react-immutable-data) explaining it .
* [The flux comparison project](https://github.com/voronianski/flux-comparison).
* [Freezer receiving data from websockets in the Flux Challenge.](https://github.com/staltz/flux-challenge/tree/master/submissions/arqex).
* [Use freezer with redux-devtools](https://github.com/arqex/freezer-redux-devtools).

## Installation
Freezer is available as a npm package.
Expand Down
67 changes: 7 additions & 60 deletions build/freezer.js
@@ -1,4 +1,4 @@
/* freezer-js v0.9.3 (26-10-2015)
/* freezer-js v0.9.4 (29-10-2015)
* https://github.com/arqex/freezer
* By arqex
* License: MIT
Expand Down Expand Up @@ -259,11 +259,10 @@ var createNE = function( attrs ){
var commonMethods = {
set: function( attr, value ){
var attrs = attr,
update = this.__.trans,
attrType = typeof attr
update = this.__.trans
;

if( attrType == 'string' || attrType == 'number' ){
if( typeof attr != 'object' ){
attrs = {};
attrs[ attr ] = value;
}
Expand Down Expand Up @@ -414,7 +413,6 @@ var Frozen = {
listener: false,
parents: [],
notify: notify,
dirty: false,
freezeFn: freezeFn,
live: live || false
}});
Expand Down Expand Up @@ -570,7 +568,7 @@ var Frozen = {
}
for (var i = _.parents.length - 1; i >= 0; i--) {
if( i == 0 ){
this.refresh( _.parents[i], node, frozen, false );
this.refresh( _.parents[i], node, frozen );
}
else{

Expand Down Expand Up @@ -732,7 +730,7 @@ var Frozen = {
});
},

refresh: function( node, oldChild, newChild, returnUpdated ){
refresh: function( node, oldChild, newChild ){
var me = this,
trans = node.__.trans,
found = 0
Expand All @@ -757,33 +755,15 @@ var Frozen = {
}

var frozen = this.copyMeta( node ),
dirty = node.__.dirty,
dirt, replacement, __
replacement, __
;

if( dirty ){
dirt = dirty[0],
replacement = dirty[1]
}

Utils.each( node, function( child, key ){
if( child === oldChild ){
child = newChild;
}
else if( child === dirt ){
child = replacement;
}

if( child && (__ = child.__) ){

// If there is a trans happening we
// don't update a dirty node now. The update
// will occur on run.
if( !__.trans && __.dirty ){
child = me.refresh( child, __.dirty[0], __.dirty[1], true );
}


me.removeParent( child, node );
me.addParent( child, frozen );
}
Expand All @@ -793,15 +773,6 @@ var Frozen = {

node.__.freezeFn( frozen );

// If the node was dirty, clean it
if( dirty ){
node.__.dirty = false;
this.trigger( frozen, 'update', frozen, frozen.__.live );
}

if( returnUpdated )
return frozen;

this.refreshParents( node, frozen );
},

Expand Down Expand Up @@ -848,7 +819,6 @@ var Frozen = {
listener: _.listener,
parents: _.parents.slice( 0 ),
trans: _.trans,
dirty: false,
freezeFn: _.freezeFn,
pivot: _.pivot,
live: _.live
Expand All @@ -874,34 +844,11 @@ var Frozen = {
}
else {
for (i = _.parents.length - 1; i >= 0; i--) {
// If there is more than one parent, mark everyone as dirty
// but the last in the iteration, and when the last is refreshed
// it will update the dirty nodes.
if( i == 0 )
this.refresh( _.parents[i], oldChild, newChild, false );
else{
this.markDirty( _.parents[i], [oldChild, newChild] );
}
this.refresh( _.parents[i], oldChild, newChild );
}
}
},

markDirty: function( node, dirt ){
var _ = node.__,
i
;
_.dirty = dirt;

// If there is a transaction happening in the node
// update the transaction data immediately
if( _.trans )
this.refresh( node, dirt[0], dirt[1] );

for ( i = _.parents.length - 1; i >= 0; i-- ) {
this.markDirty( _.parents[i], dirt );
}
},

removeParent: function( node, parent ){
var parents = node.__.parents,
index = parents.indexOf( parent )
Expand Down
4 changes: 2 additions & 2 deletions build/freezer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "freezer-js",
"version": "0.9.3",
"version": "0.9.4",
"description": "A tree data structure that is always updated from the root, making easier to think in a reactive way.",
"main": "freezer.js",
"homepage": "https://github.com/arqex/freezer",
Expand Down
60 changes: 4 additions & 56 deletions src/frozen.js
Expand Up @@ -27,7 +27,6 @@ var Frozen = {
listener: false,
parents: [],
notify: notify,
dirty: false,
freezeFn: freezeFn,
live: live || false
}});
Expand Down Expand Up @@ -183,7 +182,7 @@ var Frozen = {
}
for (var i = _.parents.length - 1; i >= 0; i--) {
if( i == 0 ){
this.refresh( _.parents[i], node, frozen, false );
this.refresh( _.parents[i], node, frozen );
}
else{

Expand Down Expand Up @@ -345,7 +344,7 @@ var Frozen = {
});
},

refresh: function( node, oldChild, newChild, returnUpdated ){
refresh: function( node, oldChild, newChild ){
var me = this,
trans = node.__.trans,
found = 0
Expand All @@ -370,33 +369,15 @@ var Frozen = {
}

var frozen = this.copyMeta( node ),
dirty = node.__.dirty,
dirt, replacement, __
replacement, __
;

if( dirty ){
dirt = dirty[0],
replacement = dirty[1]
}

Utils.each( node, function( child, key ){
if( child === oldChild ){
child = newChild;
}
else if( child === dirt ){
child = replacement;
}

if( child && (__ = child.__) ){

// If there is a trans happening we
// don't update a dirty node now. The update
// will occur on run.
if( !__.trans && __.dirty ){
child = me.refresh( child, __.dirty[0], __.dirty[1], true );
}


me.removeParent( child, node );
me.addParent( child, frozen );
}
Expand All @@ -406,15 +387,6 @@ var Frozen = {

node.__.freezeFn( frozen );

// If the node was dirty, clean it
if( dirty ){
node.__.dirty = false;
this.trigger( frozen, 'update', frozen, frozen.__.live );
}

if( returnUpdated )
return frozen;

this.refreshParents( node, frozen );
},

Expand Down Expand Up @@ -461,7 +433,6 @@ var Frozen = {
listener: _.listener,
parents: _.parents.slice( 0 ),
trans: _.trans,
dirty: false,
freezeFn: _.freezeFn,
pivot: _.pivot,
live: _.live
Expand All @@ -487,34 +458,11 @@ var Frozen = {
}
else {
for (i = _.parents.length - 1; i >= 0; i--) {
// If there is more than one parent, mark everyone as dirty
// but the last in the iteration, and when the last is refreshed
// it will update the dirty nodes.
if( i == 0 )
this.refresh( _.parents[i], oldChild, newChild, false );
else{
this.markDirty( _.parents[i], [oldChild, newChild] );
}
this.refresh( _.parents[i], oldChild, newChild );
}
}
},

markDirty: function( node, dirt ){
var _ = node.__,
i
;
_.dirty = dirt;

// If there is a transaction happening in the node
// update the transaction data immediately
if( _.trans )
this.refresh( node, dirt[0], dirt[1] );

for ( i = _.parents.length - 1; i >= 0; i-- ) {
this.markDirty( _.parents[i], dirt );
}
},

removeParent: function( node, parent ){
var parents = node.__.parents,
index = parents.indexOf( parent )
Expand Down
5 changes: 2 additions & 3 deletions src/mixins.js
Expand Up @@ -27,11 +27,10 @@ var createNE = function( attrs ){
var commonMethods = {
set: function( attr, value ){
var attrs = attr,
update = this.__.trans,
attrType = typeof attr
update = this.__.trans
;

if( attrType == 'string' || attrType == 'number' ){
if( typeof attr != 'object' ){
attrs = {};
attrs[ attr ] = value;
}
Expand Down
57 changes: 42 additions & 15 deletions tests/freezer-spec.js
Expand Up @@ -172,27 +172,52 @@ describe("Freezer test", function(){
assert.equal( updated.d.z, 2 );
});

it( "All duplicated node parents should be updated at the same time", function(){
data.c[2].set( {y: data.b.x } );
it( "Duplicated nodes should be updated at the same time.", function(){
data = data.set({selected: data.b.x});

var updated = freezer.get();
data.selected.push( data.c[2] );

data = freezer.get();

assert.equal( updated.b.x.__.parents.length, 2 );
assert.equal( updated.b.x.__.parents[1], updated.c[2] );
assert.equal( updated.b.x.__.parents[0], updated.b );
assert.equal( data.selected, data.b.x );
assert.equal( data.selected, data.c[2].__.parents[1] );

updated.b.x.push( 'C' );
data.selected[2].set({u: 4});

var second = freezer.get();
data = freezer.get();

assert.notEqual( updated, second );
assert.notEqual( updated.c, second.c );
assert.notEqual( updated.c[2], second.c[2] );
assert.notEqual( updated.b, second.b );
assert.equal( second.b.x.__.parents.length, 2 );
assert.equal( second.b.x.__.parents[1], second.c[2] );
assert.equal( second.b.x.__.parents[0], second.b );
assert.equal( data.selected, data.b.x );
});

it( "Duplicated nodes should be updated at the same time 2.", function( done ){
var freezer = new Freezer({a:[], b:[{z:1}]});

freezer.get().a.push( freezer.get().b[0] );
freezer.get().set( {c: freezer.get().b[0] } );

var count = 0;
freezer.on('update', function(){
count++;
});

var cCount = 0;
freezer.get().c.getListener().on( 'update', function(){
cCount++;
});

var c = freezer.get().c.set({y:2});

var data = freezer.get();
assert.equal( data.a[0], data.b[0] );
assert.equal( data.c, data.b[0] );
assert.equal( data.c, c );
assert.equal( data.b[0], c );

setTimeout( function(){
assert.equal( count, 1 );
assert.equal( cCount, 1 );
done();
}, 200);
});

it( "Restore a previous state", function(){
Expand Down Expand Up @@ -312,4 +337,6 @@ describe("Freezer test", function(){
assert.equal( triggered, '432143214321' );
});



});

1 comment on commit 1c17fe3

@kuraga
Copy link
Contributor

@kuraga kuraga commented on 1c17fe3 Jan 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arqex , tag this as v0.9.4, please!

Please sign in to comment.