Comparing version 1.1.0-immutable to 1.1.0-syncwrite
{ | ||
"name": "baobab", | ||
"main": "build/baobab.min.js", | ||
"version": "1.1.0-immutable", | ||
"version": "1.1.0-syncwrite", | ||
"homepage": "https://github.com/Yomguithereal/baobab", | ||
@@ -6,0 +6,0 @@ "author": { |
# Changelog | ||
## v1.0.3 | ||
* Exposing `Cursor` and `Facet` classes for type checking (**@charlieschwabacher**). | ||
* Fixing `type.Object`. | ||
* Fixing root updates. | ||
## v1.0.2 | ||
@@ -4,0 +10,0 @@ |
@@ -17,5 +17,2 @@ /** | ||
// Should the tree's data be immutable? | ||
immutable: false, | ||
// Validation specifications | ||
@@ -22,0 +19,0 @@ validate: null, |
@@ -14,3 +14,3 @@ /** | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '1.1.0-immutable' | ||
value: '1.1.0-syncwrite' | ||
}); | ||
@@ -17,0 +17,0 @@ |
{ | ||
"name": "baobab", | ||
"version": "1.1.0-immutable", | ||
"version": "1.1.0-syncwrite", | ||
"description": "JavaScript persistent data tree with cursors.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,10 +48,8 @@ /** | ||
// Properties | ||
this.data = initialData; | ||
this.log = []; | ||
this.previousData = null; | ||
this.data = helpers.deepClone(initialData); | ||
this.root = this.select([]); | ||
this.facets = {}; | ||
// Immutable tree? | ||
if (this.options.immutable) | ||
helpers.deepFreeze(this.data); | ||
// Boostrapping root cursor's methods | ||
@@ -127,2 +125,5 @@ function bootstrap(name) { | ||
// TODO: if syncwrite wins: drop skipMerge, this._transaction etc. | ||
// TODO: uniq'ing the log through path hashing | ||
// TODO: fix failed tested behaviors | ||
Baobab.prototype.stack = function(spec, skipMerge) { | ||
@@ -134,6 +135,11 @@ var self = this; | ||
this._transaction = (skipMerge && !Object.keys(this._transaction).length) ? | ||
spec : | ||
merge(this._transaction, spec); | ||
if (!this.previousData) | ||
this.previousData = this.data; | ||
// Applying modifications | ||
var result = update(this.data, spec, this.options); | ||
this.data = result.data; | ||
this.log = [].concat(this.log).concat(result.log); | ||
// Should we let the user commit? | ||
@@ -155,12 +161,3 @@ if (!this.options.autoCommit) | ||
Baobab.prototype.commit = function() { | ||
var self = this; | ||
// Applying modifications | ||
var result = update(this.data, this._transaction, this.options); | ||
var oldData = this.data; | ||
// Resetting | ||
this._transaction = {}; | ||
if (this._future) | ||
@@ -174,3 +171,3 @@ this._future = clearTimeout(this._future); | ||
if (typeof validate === 'function') { | ||
var error = validate.call(this, oldData, result.data, result.log); | ||
var error = validate.call(this, this.previousData, this.data, this.log); | ||
@@ -180,16 +177,18 @@ if (error instanceof Error) { | ||
if (behavior === 'rollback') | ||
if (behavior === 'rollback') { | ||
this.data = this.previousData; | ||
return this; | ||
} | ||
} | ||
} | ||
// Switching tree's data | ||
this.data = result.data; | ||
// Baobab-level update event | ||
this.emit('update', { | ||
log: result.log, | ||
previousState: oldData | ||
log: this.log, | ||
previousState: this.previousData | ||
}); | ||
this.log = []; | ||
this.previousData = null; | ||
return this; | ||
@@ -196,0 +195,0 @@ }; |
@@ -58,3 +58,3 @@ /** | ||
// Cloning function | ||
function cloner(deep, item) { | ||
function clone(deep, item) { | ||
if (!item || | ||
@@ -104,46 +104,29 @@ typeof item !== 'object' || | ||
// Shallow & deep cloning functions | ||
var shallowClone = cloner.bind(null, false), | ||
deepClone = cloner.bind(null, true); | ||
var shallowClone = clone.bind(null, false), | ||
deepClone = clone.bind(null, true); | ||
// Freezing function | ||
function freezer(deep, o) { | ||
var freeze = Object.freeze || Function.prototype; | ||
function deepFreeze(o) { | ||
if (typeof o !== 'object') | ||
return; | ||
Object.freeze(o); | ||
var p, | ||
k; | ||
if (!deep) | ||
return; | ||
freeze(o); | ||
if (Array.isArray(o)) { | ||
for (k in o) { | ||
p = o[k]; | ||
// Iterating through the elements | ||
var i, | ||
l; | ||
if (!o.hasOwnProperty(k) || | ||
typeof p !== 'object' || | ||
Object.isFrozen(p)) | ||
continue; | ||
for (i = 0, l = o.length; i < l; i++) | ||
deepFreeze(o[i]); | ||
deepFreeze(p); | ||
} | ||
else { | ||
var p, | ||
k; | ||
for (k in o) { | ||
p = o[k]; | ||
if (!p || | ||
!o.hasOwnProperty(k) || | ||
typeof p !== 'object' || | ||
Object.isFrozen(p)) | ||
continue; | ||
deepFreeze(p); | ||
} | ||
} | ||
} | ||
// Shallow & deep freezing function | ||
var freeze = Object.freeze ? freezer.bind(null, false) : Function.prototype, | ||
deepFreeze = Object.freeze ? freezer.bind(null, true) : Function.prototype; | ||
// Simplistic composition | ||
@@ -402,3 +385,2 @@ function compose(fn1, fn2) { | ||
before: before, | ||
freeze: freeze, | ||
deepClone: deepClone, | ||
@@ -405,0 +387,0 @@ deepFreeze: deepFreeze, |
@@ -41,2 +41,8 @@ /** | ||
// If nested object does not exist, we create it | ||
if (type.Primitive(o[lastKey])) | ||
o[lastKey] = {}; | ||
else | ||
o[lastKey] = helpers.shallowClone(o[lastKey]); | ||
// Are we at leaf level? | ||
@@ -63,4 +69,2 @@ var leafLevel = Object.keys(spec).some(function(k) { | ||
if (opts.immutable) | ||
helpers.freeze(parent[olderKey]); | ||
break; | ||
@@ -126,20 +130,5 @@ } | ||
} | ||
// Deep freezing the new value? | ||
if (opts.immutable) | ||
helpers.deepFreeze(o); | ||
} | ||
} | ||
else { | ||
// If nested object does not exist, we create it | ||
if (type.Primitive(o[lastKey])) | ||
o[lastKey] = {}; | ||
else | ||
o[lastKey] = helpers.shallowClone(o[lastKey]); | ||
// Should we freeze the parent? | ||
if (opts.immutable) | ||
helpers.freeze(o); | ||
for (k in spec) { | ||
@@ -146,0 +135,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
60893
1277