Comparing version 1.0.0-rc1 to 1.0.0
{ | ||
"name": "baobab", | ||
"main": "build/baobab.min.js", | ||
"version": "1.0.0-rc1", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/Yomguithereal/baobab", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -12,3 +12,3 @@ /** | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '1.0.0-rc1' | ||
value: '1.0.0' | ||
}); | ||
@@ -15,0 +15,0 @@ |
{ | ||
"name": "baobab", | ||
"version": "1.0.0-rc1", | ||
"version": "1.0.0", | ||
"description": "JavaScript data tree with cursors.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,2 @@ /** | ||
EventEmitter = require('emmett'), | ||
Watcher = require('./watcher.js'), | ||
Facet = require('./facet.js'), | ||
@@ -191,12 +190,2 @@ helpers = require('./helpers.js'), | ||
Baobab.prototype.watch = function(paths) { | ||
if (!type.Array(paths) || | ||
paths.some(function(p) { | ||
return !type.Path(p); | ||
})) | ||
throw Error('Baobab.watch: invalid paths.'); | ||
return new Watcher(this, [].concat(paths)); | ||
}; | ||
Baobab.prototype.release = function() { | ||
@@ -203,0 +192,0 @@ var k; |
154
src/facet.js
@@ -7,32 +7,98 @@ /** | ||
*/ | ||
var Watcher = require('./watcher.js'), | ||
helpers = require('./helpers.js'); | ||
var EventEmitter = require('emmett'), | ||
Cursor = require('./cursor.js'), | ||
helpers = require('./helpers.js'), | ||
type = require('./type.js'); | ||
function identity(v) { | ||
return v; | ||
} | ||
function Facet(tree, definition) { | ||
function Facet(tree, definition, scope) { | ||
var self = this; | ||
// Private | ||
var data = null, | ||
var firstTime = true, | ||
solved = false, | ||
solver = definition.get || identity, | ||
map = definition.cursors; | ||
getter = definition.get, | ||
data = null; | ||
var paths = Object.keys(map).map(function(k) { | ||
return map[k]; | ||
}); | ||
// Extending event emitter | ||
EventEmitter.call(this); | ||
// Watcher | ||
var watcher = new Watcher(tree, paths); | ||
// Properties | ||
this.tree = tree; | ||
this.cursors = {}; | ||
this.facets = {}; | ||
function bind(name) { | ||
self[name] = watcher[name].bind(watcher); | ||
var cursorsMapping = definition.cursors, | ||
facetsMapping = definition.facets, | ||
complexCursors = typeof definition.cursors === 'function', | ||
complexFacets = typeof definition.facets === 'function'; | ||
// Refreshing the internal mapping | ||
function refresh(complexity, targetMapping, targetProperty, mappingType) { | ||
if (!complexity && !firstTime) | ||
return; | ||
solved = false; | ||
var solvedMapping = targetMapping; | ||
if (complexity) | ||
solvedMapping = targetMapping.call(scope); | ||
if (!mappingType(solvedMapping)) | ||
throw Error('baobab.Facet: incorrect ' + targetProperty + ' mapping.'); | ||
self[targetProperty] = {}; | ||
Object.keys(solvedMapping).forEach(function(k) { | ||
if (targetProperty === 'cursors') { | ||
if (solvedMapping[k] instanceof Cursor) { | ||
self.cursors[k] = solvedMapping[k]; | ||
return; | ||
} | ||
if (type.Path(solvedMapping[k])) { | ||
self.cursors[k] = tree.select(solvedMapping[k]); | ||
return; | ||
} | ||
} | ||
else { | ||
if (solvedMapping[k] instanceof Facet) { | ||
self.facets[k] = solvedMapping[k]; | ||
return; | ||
} | ||
if (typeof solvedMapping[k] === 'string') { | ||
self.facets[k] = tree.facets[solvedMapping[k]]; | ||
if (!self.facets) | ||
throw Error('baobab.Facet: unkown "' + solvedMapping[k] + '" facet in facets mapping.'); | ||
return; | ||
} | ||
} | ||
throw Error('baobab.Facet: invalid value returned by function in ' + targetProperty + ' mapping.'); | ||
}); | ||
} | ||
['on', 'once', 'release'].forEach(bind); | ||
this.refresh = function() { | ||
// Getting facet data | ||
if (cursorsMapping) | ||
refresh( | ||
complexCursors, | ||
cursorsMapping, | ||
'cursors', | ||
type.FacetCursors | ||
); | ||
if (facetsMapping) | ||
refresh( | ||
complexFacets, | ||
facetsMapping, | ||
'facets', | ||
type.FacetFacets | ||
); | ||
}; | ||
// Data solving | ||
this.get = function() { | ||
@@ -43,8 +109,16 @@ if (solved) | ||
// Solving | ||
var cursorsData = {}; | ||
var data = {}, | ||
k; | ||
for (var k in map) | ||
cursorsData[k] = tree.get(map[k]); | ||
for (k in self.facets) | ||
data[k] = self.facets[k].get(); | ||
data = solver.call(null, cursorsData); | ||
for (k in self.cursors) | ||
data[k] = self.cursors[k].get(); | ||
// Applying getter | ||
data = typeof getter === 'function' ? | ||
getter.call(null, data) : | ||
data; | ||
solved = true; | ||
@@ -55,8 +129,32 @@ | ||
// Resetting flag on cursor update | ||
this.on('update', function() { | ||
solved = false; | ||
}); | ||
// Tracking the tree's updates | ||
this.updateHandler = function(e) { | ||
var paths = Object.keys(self.cursors).map(function(k) { | ||
return self.cursors[k].solvedPath; | ||
}) | ||
if (helpers.solveUpdate(e.data.log, paths)) { | ||
solved = false; | ||
self.emit('update'); | ||
} | ||
}; | ||
// Init routine | ||
this.refresh(); | ||
this.tree.on('update', this.updateHandler); | ||
firstTime = false; | ||
} | ||
helpers.inherits(Facet, EventEmitter); | ||
Facet.prototype.release = function() { | ||
this.tree.off('update', this.updateHandler); | ||
this.tree = null; | ||
this.cursors = null; | ||
this.facets = null; | ||
this.kill(); | ||
}; | ||
module.exports = Facet; |
@@ -52,3 +52,5 @@ /** | ||
type.Primitive = function(value) { | ||
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'; | ||
return typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean'; | ||
}; | ||
@@ -85,10 +87,2 @@ | ||
type.MixinCursor = function(value) { | ||
return anyOf(value, ['String', 'Number', 'Array', 'Function', 'Cursor']); | ||
}; | ||
type.MixinCursors = function(value) { | ||
return anyOf(value, ['Object', 'Array', 'Function']); | ||
}; | ||
type.ComplexPath = function(value) { | ||
@@ -100,2 +94,26 @@ return value.some(function(step) { | ||
type.FacetCursors = function(value) { | ||
if (!type.Object(value)) | ||
return false; | ||
return Object.keys(value).every(function(k) { | ||
var v = value[k]; | ||
return type.Path(v) || | ||
v instanceof require('./cursor.js'); | ||
}); | ||
}; | ||
type.FacetFacets = function(value) { | ||
if (!type.Object(value)) | ||
return false; | ||
return Object.keys(value).every(function(k) { | ||
var v = value[k]; | ||
return typeof v === 'string' || | ||
v instanceof require('./facet.js'); | ||
}); | ||
} | ||
module.exports = type; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
57912
1225
0
0
14