Comparing version 2.0.0-dev2 to 2.0.0-dev3
{ | ||
"name": "baobab", | ||
"main": "build/baobab.min.js", | ||
"version": "2.0.0-dev2", | ||
"version": "2.0.0-dev3", | ||
"homepage": "https://github.com/Yomguithereal/baobab", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -232,3 +232,3 @@ /** | ||
if (!cursor) { | ||
cursor = new _cursor2['default'](this, path, hash); | ||
cursor = new _cursor2['default'](this, path, { hash: hash }); | ||
this._cursors[hash] = cursor; | ||
@@ -364,2 +364,17 @@ } | ||
}, { | ||
key: 'watch', | ||
/** | ||
* Method used to watch a collection of paths within the tree. Very useful | ||
* to bind UI components and such to the tree. | ||
* | ||
* @param {object|array} paths - Paths to listen. | ||
* @return {Cursor} - A special cursor that can be listened. | ||
*/ | ||
value: function watch(paths) { | ||
if (!_type2['default'].object(paths) && !_type2['default'].array(paths)) throw Error('Baobab.watch: wrong argument.'); | ||
return new _cursor2['default'](this, null, { watch: paths }); | ||
} | ||
}, { | ||
key: 'release', | ||
@@ -417,3 +432,3 @@ | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '2.0.0-dev2' | ||
value: '2.0.0-dev3' | ||
}); | ||
@@ -420,0 +435,0 @@ |
@@ -15,3 +15,3 @@ /** | ||
var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x4, _x5, _x6) { var _again = true; _function: while (_again) { var object = _x4, property = _x5, receiver = _x6; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x4 = parent; _x5 = property; _x6 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -38,11 +38,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
* @constructor | ||
* @param {Baobab} tree - The cursor's root. | ||
* @param {array} path - The cursor's path in the tree. | ||
* @param {string} hash - The path's hash computed ahead by the tree. | ||
* @param {Baobab} tree - The cursor's root. | ||
* @param {array} path - The cursor's path in the tree. | ||
* @param {object} [opts] - Options | ||
* @param {string} [opts.hash] - The path's hash computed ahead by the tree. | ||
* @param {array} [opts.watch] - Paths the cursor is meant to watch. | ||
*/ | ||
var Cursor = (function (_Emitter) { | ||
function Cursor(tree, path, hash) { | ||
function Cursor(tree, path) { | ||
var _this = this; | ||
var opts = arguments[2] === undefined ? {} : arguments[2]; | ||
_classCallCheck(this, Cursor); | ||
@@ -62,3 +66,3 @@ | ||
this.path = path; | ||
this.hash = hash; | ||
this.hash = opts.hash; | ||
@@ -71,2 +75,10 @@ // State | ||
// Checking whether the cursor is a watcher | ||
this._watch = opts.watch; | ||
this._watchedPaths = opts.watch && (!_type2['default'].array(opts.watch) ? Object.keys(opts.watch).map(function (k) { | ||
return opts.watch[k]; | ||
}) : opts.watch); | ||
if (this._watch) this.get = this.tree.project.bind(this.tree, this._watch); | ||
// Checking whether the given path is dynamic or not | ||
@@ -87,2 +99,5 @@ this._dynamicPath = _type2['default'].dynamicPath(this.path); | ||
var fireUpdate = function fireUpdate(previousData) { | ||
if (_this._watch) return _this.emit('update'); | ||
var record = (0, _helpers.getIn)(previousData, _this.solvedPath); | ||
@@ -116,17 +131,32 @@ | ||
// Checking whether we should keep track of some dependencies | ||
// TODO: some operations here might be merged for perfs | ||
var additionalPaths = _this._facetPath ? (0, _helpers.getIn)(_this.tree._computedDataIndex, _this._facetPath).relatedPaths() : []; | ||
var comparedPaths = undefined; | ||
// If this is the root selector, we fire already | ||
if (_this.isRoot()) return update(); | ||
// Standard cursor | ||
if (!_this._watch) { | ||
// If the cursor's path is dynamic, we need to recompute it | ||
if (_this._dynamicPath) _this.solvedPath = (0, _helpers.solvePath)(_this.tree.data, _this.path); | ||
// Checking whether we should keep track of some dependencies | ||
var additionalPaths = _this._facetPath ? (0, _helpers.getIn)(_this.tree._computedDataIndex, _this._facetPath).relatedPaths() : []; | ||
var shouldFire = false; | ||
// If the cursor's path is dynamic, we need to recompute it | ||
if (_this._dynamicPath) _this.solvedPath = (0, _helpers.solvePath)(_this.tree.data, _this.path); | ||
if (_this.solvedPath) shouldFire = (0, _helpers.solveUpdate)(paths, [_this.solvedPath].concat(additionalPaths)); | ||
comparedPaths = [_this.solvedPath].concat(additionalPaths); | ||
} | ||
if (shouldFire) return update(); | ||
// Watcher cursor | ||
else { | ||
comparedPaths = _this._watchedPaths.reduce(function (cp, p) { | ||
if (_type2['default'].dynamicPath(p)) p = (0, _helpers.solvePath)(_this.tree.data, p); | ||
if (!p) return cp; | ||
var facetPath = _type2['default'].facetPath(p); | ||
if (facetPath) return cp.concat((0, _helpers.getIn)(_this.tree._computedDataIndex, p).relatedPaths()); | ||
return cp.concat([p]); | ||
}, []); | ||
} | ||
if ((0, _helpers.solveUpdate)(paths, comparedPaths)) return update(); | ||
}; | ||
@@ -566,3 +596,3 @@ | ||
// Unsubscribe from the parent | ||
delete this.tree._cursors[this.hash]; | ||
if (this.hash) delete this.tree._cursors[this.hash]; | ||
@@ -569,0 +599,0 @@ // Dereferencing |
@@ -30,3 +30,3 @@ /** | ||
* @param {Baobab} tree - The tree. | ||
* @param {array} path - Path where the facets stands in its tree. | ||
* @param {array} pathInTree - Path where the facets stands in its tree. | ||
* @param {array|object} definition - The facet's definition. | ||
@@ -36,3 +36,3 @@ */ | ||
var Facet = (function () { | ||
function Facet(tree, path, definition) { | ||
function Facet(tree, pathInTree, definition) { | ||
var _this = this; | ||
@@ -46,3 +46,3 @@ | ||
// If the definition type is not valid, we cry | ||
if (!definitionType) throw (0, _helpers.makeError)('Baobab.Facet: attempting to create a computed data node with a ' + ('wrong definition (path: /' + path.join('/') + ').'), { path: path, definition: definition }); | ||
if (!definitionType) throw (0, _helpers.makeError)('Baobab.Facet: attempting to create a computed data node with a ' + ('wrong definition (path: /' + pathInTree.join('/') + ').'), { path: pathInTree, definition: definition }); | ||
@@ -68,5 +68,5 @@ // Properties | ||
// Is the facet recursive? | ||
this.isRecursive = !!this.paths.filter(function (p) { | ||
return _type2['default'].facetPath(p); | ||
}).length; | ||
this.isRecursive = this.paths.some(function (p) { | ||
return !!_type2['default'].facetPath(p); | ||
}); | ||
@@ -113,6 +113,7 @@ // Internal state | ||
if (!this.isRecursive) return this.paths;else return this.paths.reduce(function (paths, path) { | ||
if (!_type2['default'].facetPath(path)) return paths.concat(path); | ||
var facetPath = _type2['default'].facetPath(path); | ||
if (!facetPath) return paths.concat(path); | ||
// Solving recursive path | ||
var relatedFacet = (0, _helpers.getIn)(_this2.tree._computedDataIndex, path); | ||
var relatedFacet = (0, _helpers.getIn)(_this2.tree._computedDataIndex, facetPath); | ||
@@ -119,0 +120,0 @@ return paths.concat(relatedFacet.relatedPaths()); |
@@ -591,3 +591,3 @@ /** | ||
if (!c.length) return true; | ||
if (!c || !c.length) return true; | ||
@@ -594,0 +594,0 @@ // Looping through steps |
{ | ||
"name": "baobab", | ||
"version": "2.0.0-dev2", | ||
"version": "2.0.0-dev3", | ||
"description": "JavaScript persistent data tree with cursors.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/baobab.js", |
@@ -218,3 +218,3 @@ /** | ||
if (!cursor) { | ||
cursor = new Cursor(this, path, hash); | ||
cursor = new Cursor(this, path, {hash}); | ||
this._cursors[hash] = cursor; | ||
@@ -359,2 +359,16 @@ } | ||
/** | ||
* Method used to watch a collection of paths within the tree. Very useful | ||
* to bind UI components and such to the tree. | ||
* | ||
* @param {object|array} paths - Paths to listen. | ||
* @return {Cursor} - A special cursor that can be listened. | ||
*/ | ||
watch(paths) { | ||
if (!type.object(paths) && !type.array(paths)) | ||
throw Error('Baobab.watch: wrong argument.'); | ||
return new Cursor(this, null, {watch: paths}); | ||
} | ||
/** | ||
* Method releasing the tree and its attached data from memory. | ||
@@ -401,3 +415,3 @@ */ | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '2.0.0-dev2' | ||
value: '2.0.0-dev3' | ||
}); | ||
@@ -404,0 +418,0 @@ |
@@ -24,8 +24,10 @@ /** | ||
* @constructor | ||
* @param {Baobab} tree - The cursor's root. | ||
* @param {array} path - The cursor's path in the tree. | ||
* @param {string} hash - The path's hash computed ahead by the tree. | ||
* @param {Baobab} tree - The cursor's root. | ||
* @param {array} path - The cursor's path in the tree. | ||
* @param {object} [opts] - Options | ||
* @param {string} [opts.hash] - The path's hash computed ahead by the tree. | ||
* @param {array} [opts.watch] - Paths the cursor is meant to watch. | ||
*/ | ||
export default class Cursor extends Emitter { | ||
constructor(tree, path, hash) { | ||
constructor(tree, path, opts={}) { | ||
super(); | ||
@@ -43,3 +45,3 @@ | ||
this.path = path; | ||
this.hash = hash; | ||
this.hash = opts.hash; | ||
@@ -52,2 +54,11 @@ // State | ||
// Checking whether the cursor is a watcher | ||
this._watch = opts.watch; | ||
this._watchedPaths = opts.watch && (!type.array(opts.watch) ? | ||
Object.keys(opts.watch).map(k => opts.watch[k]) : | ||
opts.watch); | ||
if (this._watch) | ||
this.get = this.tree.project.bind(this.tree, this._watch); | ||
// Checking whether the given path is dynamic or not | ||
@@ -71,2 +82,6 @@ this._dynamicPath = type.dynamicPath(this.path); | ||
const fireUpdate = (previousData) => { | ||
if (this._watch) | ||
return this.emit('update'); | ||
const record = getIn(previousData, this.solvedPath); | ||
@@ -99,25 +114,39 @@ | ||
// Checking whether we should keep track of some dependencies | ||
// TODO: some operations here might be merged for perfs | ||
const additionalPaths = this._facetPath ? | ||
getIn(this.tree._computedDataIndex, this._facetPath).relatedPaths() : | ||
[]; | ||
let comparedPaths; | ||
// If this is the root selector, we fire already | ||
if (this.isRoot()) | ||
return update(); | ||
// Standard cursor | ||
if (!this._watch) { | ||
// If the cursor's path is dynamic, we need to recompute it | ||
if (this._dynamicPath) | ||
this.solvedPath = solvePath(this.tree.data, this.path); | ||
// Checking whether we should keep track of some dependencies | ||
const additionalPaths = this._facetPath ? | ||
getIn(this.tree._computedDataIndex, this._facetPath).relatedPaths() : | ||
[]; | ||
let shouldFire = false; | ||
// If the cursor's path is dynamic, we need to recompute it | ||
if (this._dynamicPath) | ||
this.solvedPath = solvePath(this.tree.data, this.path); | ||
if (this.solvedPath) | ||
shouldFire = solveUpdate( | ||
paths, | ||
[this.solvedPath].concat(additionalPaths) | ||
); | ||
comparedPaths = [this.solvedPath].concat(additionalPaths); | ||
} | ||
if (shouldFire) | ||
// Watcher cursor | ||
else { | ||
comparedPaths = this._watchedPaths.reduce((cp, p) => { | ||
if (type.dynamicPath(p)) | ||
p = solvePath(this.tree.data, p); | ||
if (!p) | ||
return cp; | ||
const facetPath = type.facetPath(p); | ||
if (facetPath) | ||
return cp.concat( | ||
getIn(this.tree._computedDataIndex, p).relatedPaths()); | ||
return cp.concat([p]); | ||
}, []); | ||
} | ||
if (solveUpdate(paths, comparedPaths)) | ||
return update(); | ||
@@ -539,3 +568,4 @@ }; | ||
// Unsubscribe from the parent | ||
delete this.tree._cursors[this.hash]; | ||
if (this.hash) | ||
delete this.tree._cursors[this.hash]; | ||
@@ -542,0 +572,0 @@ // Dereferencing |
@@ -20,7 +20,7 @@ /** | ||
* @param {Baobab} tree - The tree. | ||
* @param {array} path - Path where the facets stands in its tree. | ||
* @param {array} pathInTree - Path where the facets stands in its tree. | ||
* @param {array|object} definition - The facet's definition. | ||
*/ | ||
export default class Facet { | ||
constructor(tree, path, definition) { | ||
constructor(tree, pathInTree, definition) { | ||
@@ -34,4 +34,4 @@ // Checking definition's type | ||
'Baobab.Facet: attempting to create a computed data node with a ' + | ||
`wrong definition (path: /${path.join('/')}).`, | ||
{path, definition} | ||
`wrong definition (path: /${pathInTree.join('/')}).`, | ||
{path: pathInTree, definition} | ||
); | ||
@@ -58,3 +58,3 @@ | ||
// Is the facet recursive? | ||
this.isRecursive = !!this.paths.filter(p => type.facetPath(p)).length; | ||
this.isRecursive = this.paths.some(p => !!type.facetPath(p)); | ||
@@ -98,7 +98,8 @@ // Internal state | ||
return this.paths.reduce((paths, path) => { | ||
if (!type.facetPath(path)) | ||
const facetPath = type.facetPath(path); | ||
if (!facetPath) | ||
return paths.concat(path); | ||
// Solving recursive path | ||
const relatedFacet = getIn(this.tree._computedDataIndex, path); | ||
const relatedFacet = getIn(this.tree._computedDataIndex, facetPath); | ||
@@ -105,0 +106,0 @@ return paths.concat(relatedFacet.relatedPaths()); |
@@ -561,3 +561,3 @@ /** | ||
if (!c.length) | ||
if (!c || !c.length) | ||
return true; | ||
@@ -564,0 +564,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
3828
149792
18