reactive-replica
Advanced tools
Comparing version 1.0.5 to 1.1.0
@@ -216,15 +216,10 @@ 'use strict'; | ||
//... and for absent replica as well since application supposed to replicate the present model's shape: | ||
if (place.replica === undefined) { | ||
issueRemove(listener, place, place.removedReplica); | ||
} | ||
//handle already existing replica: | ||
if (isFunction(listener.create)) { | ||
if (place.replica !== undefined) { | ||
listener.create(place.replica); | ||
else { | ||
issueCreate(listener, place, place.replica); | ||
} | ||
} | ||
//... and for absent replica as well since application supposed to replicate the present model's shape: | ||
if (isFunction(listener.remove)) { | ||
if (place.replica === undefined) { | ||
listener.remove(place.removedReplica); | ||
} | ||
} | ||
} | ||
@@ -248,3 +243,5 @@ | ||
if (place.replica !== undefined) { | ||
onRemove(place, place.replica); | ||
place.removedReplica = place.replica; | ||
delete place.replica; | ||
onRemove(place, place.removedReplica); | ||
@@ -255,6 +252,2 @@ for (var i = 0; i < place.children.length; ++i) { | ||
} | ||
place.removedReplica = place.replica; | ||
delete place.replica; | ||
} | ||
@@ -264,3 +257,6 @@ } | ||
else { | ||
if (place.replica === undefined) { | ||
var old = place.replica; | ||
place.replica = value; | ||
if (old === undefined) { | ||
onCreate(place, value); | ||
@@ -270,5 +266,5 @@ } | ||
//be careful with null values (since those are 'object's) - don't let them sneak into model and/or server-side: | ||
else if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' || _typeof(place.replica) !== 'object') { | ||
if (value !== place.replica) { | ||
onChange(place, place.replica, value); | ||
else if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' || (typeof old === 'undefined' ? 'undefined' : _typeof(old)) !== 'object') { | ||
if (value !== old) { | ||
onChange(place, value, old); | ||
} | ||
@@ -279,10 +275,27 @@ } | ||
//we don't care about model's fields which has no attached Places against them: | ||
for (var i = 0; i < place.children.length; ++i) { | ||
var child = place.children[i]; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
//I suppose we shouldn't care if replicating value isn't of Object type since [] operator should just return undefined (is what we need) in that case: | ||
child.replicate(value[child.name]); | ||
try { | ||
for (var _iterator2 = place.children[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _child = _step2.value; | ||
//I suppose we shouldn't care if replicating value isn't of Object type since [] operator should just return undefined (is what we need) in that case: | ||
_child.replicate(value[_child.name]); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
place.replica = value; | ||
} | ||
@@ -324,26 +337,102 @@ | ||
function issueCreate(listener, place, created) { | ||
if (listener.create === true) { | ||
if (isFunction(listener.change)) { | ||
listener.change(created, undefined, place); | ||
} | ||
} else if (isFunction(listener.create)) { | ||
listener.create(created, place); | ||
} | ||
} | ||
function onCreate(place, created) { | ||
for (var i in place.listeners) { | ||
var listener = place.listeners[i]; | ||
if (isFunction(listener.create)) { | ||
listener.create(created, place); | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = place.listeners[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var listener = _step3.value; | ||
issueCreate(listener, place, created); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
} | ||
function onChange(place, before, after) { | ||
for (var i in place.listeners) { | ||
var listener = place.listeners[i]; | ||
if (isFunction(listener.change)) { | ||
listener.change(before, after, place); | ||
function onChange(place, after, before) { | ||
var _iteratorNormalCompletion4 = true; | ||
var _didIteratorError4 = false; | ||
var _iteratorError4 = undefined; | ||
try { | ||
for (var _iterator4 = place.listeners[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var listener = _step4.value; | ||
if (listener.change === true) { | ||
if (isFunction(listener.create)) { | ||
listener.create(after, place); | ||
} | ||
} else if (isFunction(listener.change)) { | ||
listener.change(after, before, place); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError4 = true; | ||
_iteratorError4 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError4) { | ||
throw _iteratorError4; | ||
} | ||
} | ||
} | ||
} | ||
function issueRemove(listener, place, old) { | ||
if (isFunction(listener.remove)) { | ||
listener.remove(old, place); | ||
} | ||
} | ||
function onRemove(place, old) { | ||
for (var i in place.listeners) { | ||
var listener = place.listeners[i]; | ||
if (isFunction(listener.remove)) { | ||
listener.remove(old, place); | ||
var _iteratorNormalCompletion5 = true; | ||
var _didIteratorError5 = false; | ||
var _iteratorError5 = undefined; | ||
try { | ||
for (var _iterator5 = place.listeners[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
var listener = _step5.value; | ||
issueRemove(listener, place, old); | ||
} | ||
} catch (err) { | ||
_didIteratorError5 = true; | ||
_iteratorError5 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion5 && _iterator5.return) { | ||
_iterator5.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError5) { | ||
throw _iteratorError5; | ||
} | ||
} | ||
} | ||
@@ -350,0 +439,0 @@ } |
97
index.js
@@ -214,18 +214,11 @@ | ||
//handle already existing replica: | ||
if ( isFunction( listener.create ) ) | ||
//... and for absent replica as well since application supposed to replicate the present model's shape: | ||
if ( place.replica === undefined ) | ||
{ | ||
if ( place.replica !== undefined ) | ||
{ | ||
listener.create( place.replica ) | ||
} | ||
issueRemove( listener, place, place.removedReplica ) | ||
} | ||
//... and for absent replica as well since application supposed to replicate the present model's shape: | ||
if ( isFunction( listener.remove ) ) | ||
//handle already existing replica: | ||
else | ||
{ | ||
if ( place.replica === undefined ) | ||
{ | ||
listener.remove( place.removedReplica ) | ||
} | ||
issueCreate( listener, place, place.replica ) | ||
} | ||
@@ -262,3 +255,5 @@ } | ||
{ | ||
onRemove( place, place.replica ) | ||
place.removedReplica = place.replica | ||
delete place.replica | ||
onRemove( place, place.removedReplica ) | ||
@@ -270,6 +265,2 @@ for ( var i = 0; i < place.children.length; ++ i ) | ||
} | ||
place.removedReplica = place.replica | ||
delete place.replica | ||
} | ||
@@ -280,3 +271,6 @@ } | ||
{ | ||
if ( place.replica === undefined ) | ||
let old = place.replica | ||
place.replica = value | ||
if ( old === undefined ) | ||
{ | ||
@@ -287,7 +281,7 @@ onCreate( place, value ) | ||
//be careful with null values (since those are 'object's) - don't let them sneak into model and/or server-side: | ||
else if ( typeof value !== 'object' || typeof place.replica !== 'object' ) | ||
else if ( typeof value !== 'object' || typeof old !== 'object' ) | ||
{ | ||
if ( value !== place.replica ) | ||
if ( value !== old ) | ||
{ | ||
onChange( place, place.replica, value ) | ||
onChange( place, value, old ) | ||
} | ||
@@ -298,11 +292,7 @@ } | ||
//we don't care about model's fields which has no attached Places against them: | ||
for ( var i = 0; i < place.children.length; ++ i ) | ||
for ( let child of place.children ) | ||
{ | ||
var child = place.children[ i ] | ||
//I suppose we shouldn't care if replicating value isn't of Object type since [] operator should just return undefined (is what we need) in that case: | ||
child.replicate( value[ child.name ] ) | ||
} | ||
place.replica = value | ||
} | ||
@@ -347,35 +337,56 @@ | ||
function onCreate( place, created ) | ||
function issueCreate( listener, place, created ) | ||
{ | ||
for ( var i in place.listeners ) | ||
if ( listener.create === true ) | ||
{ | ||
var listener = place.listeners[ i ] | ||
if ( isFunction( listener.create ) ) | ||
if ( isFunction( listener.change ) ) | ||
{ | ||
listener.create( created, place ) | ||
listener.change( created, undefined, place ) | ||
} | ||
} | ||
else if ( isFunction( listener.create ) ) | ||
{ | ||
listener.create( created, place ) | ||
} | ||
} | ||
function onChange( place, before, after ) | ||
function onCreate( place, created ) | ||
{ | ||
for ( var i in place.listeners ) | ||
for ( let listener of place.listeners ) | ||
{ | ||
var listener = place.listeners[ i ] | ||
if ( isFunction( listener.change ) ) | ||
issueCreate( listener, place, created ) | ||
} | ||
} | ||
function onChange( place, after, before ) | ||
{ | ||
for ( let listener of place.listeners ) | ||
{ | ||
if ( listener.change === true ) | ||
{ | ||
listener.change( before, after, place ) | ||
if ( isFunction( listener.create ) ) | ||
{ | ||
listener.create( after, place ) | ||
} | ||
} | ||
else if ( isFunction( listener.change ) ) | ||
{ | ||
listener.change( after, before, place ) | ||
} | ||
} | ||
} | ||
function issueRemove( listener, place, old ) | ||
{ | ||
if ( isFunction( listener.remove ) ) | ||
{ | ||
listener.remove( old, place ) | ||
} | ||
} | ||
function onRemove( place, old ) | ||
{ | ||
for ( var i in place.listeners ) | ||
for ( let listener of place.listeners ) | ||
{ | ||
var listener = place.listeners[ i ] | ||
if ( isFunction( listener.remove ) ) | ||
{ | ||
listener.remove( old, place ) | ||
} | ||
issueRemove( listener, place, old ) | ||
} | ||
@@ -382,0 +393,0 @@ } |
{ | ||
"name": "reactive-replica", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Helps structure logic around model changes, i.e. utilize reactive style.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -224,7 +224,7 @@ import 'babel-polyfill' | ||
}, | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
++ changeCalled | ||
expect( after ).equal( 2 ) | ||
expect( before ).equal( 1 ) | ||
expect( after ).equal( 2 ) | ||
}, | ||
@@ -334,6 +334,6 @@ remove : function( removed ) | ||
}, | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
expect( after ).equal( 2 ) | ||
expect( before ).equal( 1 ) | ||
expect( after ).equal( 2 ) | ||
++ firstChanged | ||
@@ -350,6 +350,6 @@ } | ||
}, | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
expect( after ).equal( 2 ) | ||
expect( before ).equal( 1 ) | ||
expect( after ).equal( 2 ) | ||
++ secondChanged | ||
@@ -383,3 +383,3 @@ } | ||
place.listen( { | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
@@ -389,14 +389,14 @@ switch ( changed ) | ||
case 0: | ||
expect( after ).equal( 1 ) | ||
expect( before ).equal( 0 ) | ||
expect( after ).equal( 1 ) | ||
break | ||
case 1: | ||
expect( after ).equal( 2 ) | ||
expect( before ).equal( 1 ) | ||
expect( after ).equal( 2 ) | ||
break | ||
case 2: | ||
expect( after ).equal( 3 ) | ||
expect( before ).equal( 2 ) | ||
expect( after ).equal( 3 ) | ||
break | ||
@@ -537,6 +537,6 @@ } | ||
}, | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
expect( after ).equal( 2 ) | ||
expect( before ).equal( 1 ) | ||
expect( after ).equal( 2 ) | ||
++ changeCalled | ||
@@ -611,6 +611,6 @@ }, | ||
}, | ||
change( before, after ) | ||
change( after, before ) | ||
{ | ||
expect( after, 'after' ).equal( 1 ) | ||
expect( before, 'before' ).equal( 2 ) | ||
expect( after, 'after' ).equal( 1 ) | ||
++ changeCalled | ||
@@ -641,3 +641,3 @@ } | ||
}, | ||
change : function( before, after ) | ||
change : function( after, before ) | ||
{ | ||
@@ -775,2 +775,61 @@ throw 'Must NOT be called' | ||
} ) | ||
it ( 'Create redirection', function( done ) | ||
{ | ||
let root = new Place | ||
root.replicate( { | ||
create : 1 | ||
} ) | ||
let changeCalled = 0 | ||
root.listen( { | ||
create : true, | ||
change : function( after, before ) | ||
{ | ||
expect( after, 'after' ).equal( 1 ) | ||
expect( before, 'before' ).equal( undefined ) | ||
++ changeCalled | ||
} | ||
}, 'create' ) | ||
expect( changeCalled, 'change called' ).equal( 1 ) | ||
done() | ||
} ) | ||
it ( 'Change redirection', function( done ) | ||
{ | ||
let root = new Place | ||
root.replicate( { | ||
change : 1 | ||
} ) | ||
let createCalled = 0 | ||
root.listen( { | ||
change : true, | ||
create : function( created ) | ||
{ | ||
switch ( createCalled ) | ||
{ | ||
case 0: | ||
expect( created ).equal( 1 ) | ||
break | ||
case 1: | ||
expect( created ).equal( 2 ) | ||
break | ||
} | ||
++ createCalled | ||
} | ||
}, 'change' ) | ||
root.replicate( { | ||
change : 2 | ||
} ) | ||
expect( createCalled ).equal( 2 ) | ||
done() | ||
} ) | ||
} ) | ||
@@ -777,0 +836,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54942
1606