Skip to content

Commit

Permalink
Merge branch 'nathanial-single-parent'
Browse files Browse the repository at this point in the history
  • Loading branch information
arqex committed Feb 19, 2018
2 parents 750e38a + 98cb0cd commit b47a667
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/freezer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var Freezer = function( initialValue, options ) {
ops = options || {},
store = {
live: ops.live || false,
freezeInstances: ops.freezeInstances || false
freezeInstances: ops.freezeInstances || false,
singleParent: ops.singleParent || false
}
;

Expand Down
3 changes: 3 additions & 0 deletions src/frozen.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ var Frozen = {
;

if( index === -1 ){
if(node.__.store.singleParent && parents.length >= 1){
throw new Error('Node already has a parent');
}
parents[ parents.length ] = parent;
}
},
Expand Down
13 changes: 13 additions & 0 deletions tests/freezer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,17 @@ describe("Freezer test", function(){
assert.equal( freezer.get().b.z, 0 );
assert.equal( freezer.get().b.x[0], 'z' );
});

it('singleParent should limit a node to having one parent', function () {
var freezer = new Freezer({
a: {
b: {}
},
c: {}
}, {singleParent: true, live: true});
const oldB = freezer.get().a.b;
assert.throws(function() {
freezer.get().c.set({b: oldB});
}, Error, "Node already has a parent");
});
});

0 comments on commit b47a667

Please sign in to comment.