From 4833e50a6cd64dc2c8bff655a319ede7041df836 Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Tue, 28 Feb 2017 19:51:33 +0100 Subject: [PATCH] Bump 2.4.3 --- CHANGELOG.md | 4 ++++ bower.json | 2 +- build/baobab.js | 17 ++++++++--------- build/baobab.min.js | 6 +++--- package.json | 2 +- src/baobab.js | 2 +- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41b1313..bfc9c6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v2.4.3 + +* Better `tree/cursor.splice` ([@jrust](https://github.com/jrust)). + ## v2.4.2 * Fixing monkey-related memory leak ([@jrust](https://github.com/jrust)). diff --git a/bower.json b/bower.json index 7bf56a2..f3f5c99 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "baobab", "main": "build/baobab.min.js", - "version": "2.4.2", + "version": "2.4.3", "homepage": "https://github.com/Yomguithereal/baobab", "author": { "name": "Guillaume Plique", diff --git a/build/baobab.js b/build/baobab.js index 5da35f1..1927345 100644 --- a/build/baobab.js +++ b/build/baobab.js @@ -2,7 +2,7 @@ * Baobab * * Homepage: https://github.com/Yomguithereal/baobab - * Version: 2.4.2 + * Version: 2.4.3 * Author: Yomguithereal (Guillaume Plique) * License: MIT */ @@ -1146,7 +1146,7 @@ Baobab.helpers = helpers; /** * Version */ -Baobab.VERSION = '2.4.2'; +Baobab.VERSION = '2.4.3'; module.exports = exports['default']; },{"./cursor":3,"./helpers":4,"./monkey":5,"./type":6,"./update":7,"./watcher":8,"emmett":1}],3:[function(require,module,exports){ @@ -2581,7 +2581,11 @@ function solveUpdate(affectedPaths, comparedPaths) { */ function splice(array, startIndex, nb) { - if (nb === undefined) nb = array.length - startIndex;else if (nb === null) nb = 0;else if (Number.isNaN(Number.parseInt(nb, 10))) throw new Error('argument nb ' + nb + ' can not be parsed into a number!'); + for (var _len2 = arguments.length, elements = Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) { + elements[_key2 - 3] = arguments[_key2]; + } + + if (nb === undefined && arguments.length === 2) nb = array.length - startIndex;else if (nb === null || nb === undefined) nb = 0;else if (isNaN(+nb)) throw new Error('argument nb ' + nb + ' can not be parsed into a number!'); nb = Math.max(0, nb); // Solving startIndex @@ -2591,11 +2595,6 @@ function splice(array, startIndex, nb) { }); // Positive index - - for (var _len2 = arguments.length, elements = Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) { - elements[_key2 - 3] = arguments[_key2]; - } - if (startIndex >= 0) return array.slice(0, startIndex).concat(elements).concat(array.slice(startIndex + nb)); // Negative index @@ -3017,7 +3016,7 @@ type.primitive = function (target) { */ type.splicer = function (target) { if (!type.array(target) || target.length < 1) return false; - if (target.length > 1 && Number.isNaN(Number.parseInt(target[1], 10))) return false; + if (target.length > 1 && isNaN(+target[1])) return false; return anyOf(target[0], ['number', 'function', 'object']); }; diff --git a/build/baobab.min.js b/build/baobab.min.js index 0135f91..8e68393 100644 --- a/build/baobab.min.js +++ b/build/baobab.min.js @@ -2,9 +2,9 @@ * Baobab * * Homepage: https://github.com/Yomguithereal/baobab - * Version: 2.4.2 + * Version: 2.4.3 * Author: Yomguithereal (Guillaume Plique) * License: MIT */ -!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Baobab=t()}}(function(){var t;return function t(e,r,n){function i(a,s){if(!r[a]){if(!e[a]){var h="function"==typeof require&&require;if(!s&&h)return h(a,!0);if(o)return o(a,!0);var u=new Error("Cannot find module '"+a+"'");throw u.code="MODULE_NOT_FOUND",u}var l=r[a]={exports:{}};e[a][0].call(l.exports,function(t){var r=e[a][1][t];return i(r?r:t)},l,l.exports,t,e,r,n)}return r[a].exports}for(var o="function"==typeof require&&require,a=0;a1?t[r]=e(t[r],{once:!0}):t.push({once:!0}),this.on.apply(this,t)},u.prototype.off=function(t,e){var r,n,s,h;if(1===arguments.length&&"function"==typeof t){e=arguments[0];for(s in this._handlers)this._handlers[s]=a(this._handlers[s],e),0===this._handlers[s].length&&delete this._handlers[s];this._handlersAll=a(this._handlersAll,e),this._handlersComplex=a(this._handlersComplex,e)}else if(1!==arguments.length||"string"!=typeof t&&"symbol"!=typeof t)if(2===arguments.length){var u=[].concat(t);for(r=0,n=u.length;r1&&(r.data=e),s.fn.call("scope"in s?s.scope:this,r),s.once&&p.push(s);for(u=p.length-1;u>=0;u--)n=p[u].type?this._handlers[p[u].type]:p[u].pattern?this._handlersComplex:this._handlersAll,n.splice(n.indexOf(p[u]),1)}return this},u.prototype.kill=function(){this.unbindAll(),this._handlers=null,this._handlersAll=null,this._handlersComplex=null,this._enabled=!1,this.unbindAll=this.on=this.once=this.off=this.emit=this.listeners=Function.prototype},u.prototype.disable=function(){return this._enabled=!1,this},u.prototype.enable=function(){return this._enabled=!0,this},u.version="3.1.1","undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=u),n.Emitter=u):"function"==typeof t&&t.amd?t("emmett",[],function(){return u}):this.Emitter=u}).call(this)},{}],2:[function(t,e,r){"use strict";function n(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e}function i(t){return t&&t.__esModule?t:{default:t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function s(t){return"λ"+t.map(function(t){return v.default.function(t)||v.default.object(t)?"#"+B()+"#":t}).join("λ")}r.__esModule=!0;var h=t("emmett"),u=i(h),l=t("./cursor"),f=i(l),c=t("./monkey"),p=t("./watcher"),d=i(p),y=t("./type"),v=i(y),m=t("./update"),g=i(m),b=t("./helpers"),_=n(b),w=_.arrayFrom,k=_.coercePath,P=_.deepFreeze,j=_.getIn,M=_.makeError,O=_.deepClone,C=_.deepMerge,E=_.shallowClone,x=_.shallowMerge,B=_.uniqid,D={autoCommit:!0,asynchronous:!0,immutable:!0,lazyMonkeys:!0,persistent:!0,pure:!0,validate:null,validationBehavior:"rollback"},z=function(t){function e(r,n){var i=this;if(o(this,e),t.call(this),arguments.length<1&&(r={}),!v.default.object(r)&&!v.default.array(r))throw M("Baobab: invalid data.",{data:r});this.options=x({},D,n),this.options.persistent||(this.options.immutable=!1,this.options.pure=!1),this._identity="[object Baobab]",this._cursors={},this._future=null,this._transaction=[],this._affectedPathsIndex={},this._monkeys={},this._previousData=null,this._data=r,this.root=new f.default(this,[],"λ"),delete this.root.release,this.options.immutable&&P(this._data);var a=function(t){i[t]=function(){var e=this.root[t].apply(this.root,arguments);return e instanceof f.default?this:e}};["apply","clone","concat","deepClone","deepMerge","exists","get","push","merge","pop","project","serialize","set","shift","splice","unset","unshift"].forEach(a),this._refreshMonkeys();var s=this.validate();if(s)throw Error("Baobab: invalid data.",{error:s})}return a(e,t),e.prototype._refreshMonkeys=function(t,e,r){var n=this,i=function t(e){var r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];if(e instanceof c.Monkey)return e.release(),void g.default(n._monkeys,r,{type:"unset"},{immutable:!1,persistent:!1,pure:!1});if(v.default.object(e))for(var i in e)t(e[i],r.concat(i))},o=function t(e){var r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];if(e instanceof c.MonkeyDefinition||e instanceof c.Monkey){var i=new c.Monkey(n,r,e instanceof c.Monkey?e.definition:e);return void g.default(n._monkeys,r,{type:"set",value:i},{immutable:!1,persistent:!1,pure:!1})}if(v.default.object(e))for(var o in e)t(e[o],r.concat(o))};if(arguments.length){var a=j(this._monkeys,e).data;a&&i(a,e),"unset"!==r&&o(t,e)}else o(this._data);return this},e.prototype.validate=function t(e){var r=this.options,t=r.validate,n=r.validationBehavior;if("function"!=typeof t)return null;var i=t.call(this,this._previousData,this._data,e||[[]]);return i instanceof Error?("rollback"===n&&(this._data=this._previousData,this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data),this.emit("invalid",{error:i}),i):null},e.prototype.select=function(t){if(t=t||[],arguments.length>1&&(t=w(arguments)),!v.default.path(t))throw M("Baobab.select: invalid path.",{path:t});t=[].concat(t);var e=s(t),r=this._cursors[e];return r||(r=new f.default(this,t,e),this._cursors[e]=r),this.emit("select",{path:t,cursor:r}),r},e.prototype.update=function(t,e){var r=this;if(t=k(t),!v.default.operationType(e.type))throw M('Baobab.update: unknown operation type "'+e.type+'".',{operation:e});var n=j(this._data,t),i=n.solvedPath,o=n.exists;if(!i)throw M("Baobab.update: could not solve the given path.",{path:i});var a=v.default.monkeyPath(this._monkeys,i);if(a&&i.length>a.length)throw M("Baobab.update: attempting to update a read-only path.",{path:i});if("unset"!==e.type||o){var h=e;if(/merge/i.test(e.type)){var u=j(this._monkeys,i).data;if(v.default.object(u)){h=E(h);var l=j(this._data,i).data;/deep/i.test(h.type)?h.value=C({},C({},l,O(u)),h.value):h.value=x({},C({},l,O(u)),h.value)}}this._transaction.length||(this._previousData=this._data);var f=g.default(this._data,i,h,this.options),c=f.data,p=f.node;if(!("data"in f))return p;var d=i.concat("push"===e.type?p.length-1:[]),y=s(d);return this._data=c,this._affectedPathsIndex[y]=!0,this._transaction.push(x({},e,{path:d})),this._refreshMonkeys(p,i,e.type),this.emit("write",{path:d}),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout(function(){return r.commit()},0)),p):(this.commit(),p):p}},e.prototype.commit=function(){if(!this._transaction.length)return this;this._future&&(this._future=clearTimeout(this._future));var t=Object.keys(this._affectedPathsIndex).map(function(t){return"λ"!==t?t.split("λ").slice(1):[]}),e=this.validate(t);if(e)return this;var r=this._transaction,n=this._previousData;return this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data,this.emit("update",{paths:t,currentData:this._data,transaction:r,previousData:n}),this},e.prototype.getMonkey=function(t){t=k(t);var e=j(this._monkeys,[].concat(t)).data;return e instanceof c.Monkey?e:null},e.prototype.watch=function(t){return new d.default(this,t)},e.prototype.release=function(){var t=void 0;this.emit("release"),delete this.root,delete this._data,delete this._previousData,delete this._transaction,delete this._affectedPathsIndex,delete this._monkeys;for(t in this._cursors)this._cursors[t].release();delete this._cursors,this.kill()},e.prototype.toJSON=function(){return this.serialize()},e.prototype.toString=function(){return this._identity},e}(u.default);r.default=z,z.monkey=function(){for(var t=arguments.length,e=Array(t),r=0;r2)throw p.makeError("Baobab.Cursor."+t+": too many arguments.");if(1!==arguments.length||y[t]||(n=r,r=[]),r=p.coercePath(r),!c.default.path(r))throw p.makeError("Baobab.Cursor."+t+": invalid path.",{path:r});if(e&&!e(n))throw p.makeError("Baobab.Cursor."+t+": invalid value.",{path:r,value:n});if(!this.solvedPath)throw p.makeError("Baobab.Cursor."+t+": the dynamic path of the cursor cannot be solved.",{path:this.path});var i=this.solvedPath.concat(r);return this.tree.update(i,{type:t,value:n})}}r.__esModule=!0;var h=t("emmett"),u=n(h),l=t("./monkey"),f=t("./type"),c=n(f),p=t("./helpers"),d=function(t){function e(r,n,o){var a=this;i(this,e),t.call(this),n=n||[],this._identity="[object Cursor]",this._archive=null,this.tree=r,this.path=n,this.hash=o,this.state={killed:!1,recording:!1,undoing:!1},this._dynamicPath=c.default.dynamicPath(this.path),this._monkeyPath=c.default.monkeyPath(this.tree._monkeys,this.path),this._dynamicPath?this.solvedPath=p.getIn(this.tree._data,this.path).solvedPath:this.solvedPath=this.path,this._writeHandler=function(t){var e=t.data;!a.state.killed&&p.solveUpdate([e.path],a._getComparedPaths())&&(a.solvedPath=p.getIn(a.tree._data,a.path).solvedPath)};var s=function(t){var e=a,r=Object.defineProperties({},{previousData:{get:function(){return p.getIn(t,e.solvedPath).data},configurable:!0,enumerable:!0},currentData:{get:function(){return e.get()},configurable:!0,enumerable:!0}});return a.state.recording&&!a.state.undoing&&a.archive.add(r.previousData),a.state.undoing=!1,a.emit("update",r)};this._updateHandler=function(t){if(!a.state.killed){var e=t.data,r=e.paths,n=e.previousData,i=s.bind(a,n),o=a._getComparedPaths();return p.solveUpdate(r,o)?i():void 0}};var h=!1;this._lazyBind=function(){if(!h)return h=!0,a._dynamicPath&&a.tree.on("write",a._writeHandler),a.tree.on("update",a._updateHandler)},this._dynamicPath?this._lazyBind():(this.on=p.before(this._lazyBind,this.on.bind(this)),this.once=p.before(this._lazyBind,this.once.bind(this)))}return o(e,t),e.prototype._getComparedPaths=function(){var t=this._monkeyPath?p.getIn(this.tree._monkeys,this._monkeyPath).data.relatedPaths():[];return[this.solvedPath].concat(t)},e.prototype.isRoot=function(){return!this.path.length},e.prototype.isLeaf=function(){return c.default.primitive(this._get().data)},e.prototype.isBranch=function(){return!this.isRoot()&&!this.isLeaf()},e.prototype.root=function(){return this.tree.select()},e.prototype.select=function(t){return arguments.length>1&&(t=p.arrayFrom(arguments)),this.tree.select(this.path.concat(t))},e.prototype.up=function(){return this.isRoot()?null:this.tree.select(this.path.slice(0,-1))},e.prototype.down=function(){if(a("down",this.solvedPath),!(this._get().data instanceof Array))throw Error("Baobab.Cursor.down: cannot go down on a non-list type.");return this.tree.select(this.solvedPath.concat(0))},e.prototype.left=function(){a("left",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.left: cannot go left on a non-list type.");return t?this.tree.select(this.solvedPath.slice(0,-1).concat(t-1)):null},e.prototype.right=function(){a("right",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.right: cannot go right on a non-list type.");return t+1===this.up()._get().data.length?null:this.tree.select(this.solvedPath.slice(0,-1).concat(t+1))},e.prototype.leftmost=function(){a("leftmost",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.leftmost: cannot go left on a non-list type.");return this.tree.select(this.solvedPath.slice(0,-1).concat(0))},e.prototype.rightmost=function(){a("rightmost",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.rightmost: cannot go right on a non-list type.");var e=this.up()._get().data;return this.tree.select(this.solvedPath.slice(0,-1).concat(e.length-1))},e.prototype.map=function(t,e){a("map",this.solvedPath);var r=this._get().data,n=arguments.length;if(!c.default.array(r))throw Error("baobab.Cursor.map: cannot map a non-list type.");return r.map(function(i,o){return t.call(n>1?e:this,this.select(o),o,r)},this)},e.prototype._get=function(){var t=arguments.length<=0||void 0===arguments[0]?[]:arguments[0];if(!c.default.path(t))throw p.makeError("Baobab.Cursor.getters: invalid path.",{path:t});return this.solvedPath?p.getIn(this.tree._data,this.solvedPath.concat(t)):{data:void 0,solvedPath:null,exists:!1}},e.prototype.exists=function(t){return t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments)),this._get(t).exists},e.prototype.get=function(t){t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments));var e=this._get(t),r=e.data,n=e.solvedPath;return this.tree.emit("get",{data:r,solvedPath:n,path:this.path.concat(t)}),r},e.prototype.clone=function(){var t=this.get.apply(this,arguments);return p.shallowClone(t)},e.prototype.deepClone=function(){var t=this.get.apply(this,arguments);return p.deepClone(t)},e.prototype.serialize=function(t){if(t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments)),!c.default.path(t))throw p.makeError("Baobab.Cursor.getters: invalid path.",{path:t});if(this.solvedPath){var e=this.solvedPath.concat(t),r=p.deepClone(p.getIn(this.tree._data,e).data),n=p.getIn(this.tree._monkeys,e).data,i=function t(e,r){if(c.default.object(r)&&c.default.object(e))for(var n in r)r[n]instanceof l.Monkey?delete e[n]:t(e[n],r[n])};return i(r,n),r}},e.prototype.project=function(t){if(c.default.object(t)){var e={};for(var r in t)e[r]=this.get(t[r]);return e}if(c.default.array(t)){for(var e=[],n=0,i=t.length;n1?e-1:0),n=1;n3?n-3:0),a=3;a=0?t.slice(0,e).concat(i).concat(t.slice(e+r)):t.slice(0,t.length+e).concat(i).concat(t.slice(t.length+e+r))}r.__esModule=!0,r.arrayFrom=s,r.before=h,r.coercePath=f,r.getIn=d,r.makeError=y,r.solveRelativePath=m,r.solveUpdate=g,r.splice=b;var _=t("./monkey"),w=t("./type"),k=n(w),P=Function.prototype,j={}.hasOwnProperty,M=function(){function t(e){i(this,t),this.size=e,this.records=[]}return t.prototype.get=function(){return this.records},t.prototype.add=function(t){return this.records.unshift(t),this.records.length>this.size&&(this.records.length=this.size),this},t.prototype.clear=function(){return this.records=[],this},t.prototype.back=function(t){var e=this.records[t-1];return e&&(this.records=this.records.slice(t)),e},t}();r.Archive=M;var O=l.bind(null,!1),C=l.bind(null,!0);r.shallowClone=O,r.deepClone=C;var E="function"==typeof Object.freeze,x=E?p.bind(null,!1):P,B=E?p.bind(null,!0):P;r.freeze=x,r.deepFreeze=B;var D={data:void 0,solvedPath:null,exists:!1},z=v.bind(null,!1),A=v.bind(null,!0);r.shallowMerge=z,r.deepMerge=A;var N=function(){var t=0;return function(){return t++}}();r.uniqid=N}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./monkey":5,"./type":6}],5:[function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}r.__esModule=!0;var o=t("./type"),a=n(o),s=t("./update"),h=n(s),u=t("./helpers"),l=function t(e){var r=this;i(this,t);var n=a.default.monkeyDefinition(e);if(!n)throw u.makeError("Baobab.monkey: invalid definition.",{definition:e});if(this.type=n,"object"===this.type)this.getter=e.get,this.projection=e.cursors||{},this.paths=Object.keys(this.projection).map(function(t){return r.projection[t]}),this.options=e.options||{};else{var o=1,s={};a.default.object(e[e.length-1])&&(o++,s=e[e.length-1]),this.getter=e[e.length-o],this.projection=e.slice(0,-o),this.paths=this.projection,this.options=s}this.paths=this.paths.map(function(t){return[].concat(t)}),this.hasDynamicPaths=this.paths.some(a.default.dynamicPath)};r.MonkeyDefinition=l;var f=function(){function t(e,r,n){var o=this;i(this,t),this.tree=e,this.path=r,this.definition=n;var a=n.projection,s=u.solveRelativePath.bind(null,r.slice(0,-1));"object"===n.type?(this.projection=Object.keys(a).reduce(function(t,e){return t[e]=s(a[e]),t},{}),this.depPaths=Object.keys(this.projection).map(function(t){return o.projection[t]})):(this.projection=a.map(s),this.depPaths=this.projection),this.state={killed:!1},this.writeListener=function(t){var e=t.data.path;if(!o.state.killed){var r=u.solveUpdate([e],o.relatedPaths());r&&o.update()}},this.recursiveListener=function(t){var e=t.data,r=e.monkey,n=e.path;if(!o.state.killed&&o!==r){var i=u.solveUpdate([n],o.relatedPaths(!1));i&&o.update()}},this.tree.on("write",this.writeListener),this.tree.on("_monkey",this.recursiveListener),this.update()}return t.prototype.relatedPaths=function(){var t=this,e=arguments.length<=0||void 0===arguments[0]||arguments[0],r=void 0;r=this.definition.hasDynamicPaths?this.depPaths.map(function(e){return u.getIn(t.tree._data,e).solvedPath}):this.depPaths;var n=e&&this.depPaths.some(function(e){return!!a.default.monkeyPath(t.tree._monkeys,e)});return n?r.reduce(function(e,r){var n=a.default.monkeyPath(t.tree._monkeys,r);if(!n)return e.concat([r]);var i=u.getIn(t.tree._monkeys,n).data;return e.concat(i.relatedPaths())},[]):r},t.prototype.update=function(){var t=this.tree.project(this.projection),e=function(t,e,r){var n=null,i=!1;return function(){return i||(n=e.getter.apply(t,"object"===e.type?[r]:r),t.options.immutable&&e.options.immutable!==!1&&u.deepFreeze(n),i=!0),n}}(this.tree,this.definition,t);if(e.isLazyGetter=!0,this.tree.options.lazyMonkeys)this.tree._data=h.default(this.tree._data,this.path,{type:"monkey",value:e},this.tree.options).data;else{var r=h.default(this.tree._data,this.path,{type:"set",value:e(),options:{mutableLeaf:!this.definition.options.immutable}},this.tree.options);"data"in r&&(this.tree._data=r.data)}return this.tree.emit("_monkey",{monkey:this,path:this.path}),this},t.prototype.release=function(){this.tree.off("write",this.writeListener),this.tree.off("_monkey",this.recursiveListener),this.state.killed=!0,delete this.projection,delete this.depPaths,delete this.tree},t}();r.Monkey=f},{"./helpers":4,"./type":6,"./update":7}],6:[function(t,e,r){"use strict";function n(t,e){return e.some(function(e){return o[e](t)})}r.__esModule=!0;var i=t("./monkey"),o={};o.array=function(t){return Array.isArray(t)},o.object=function(t){return t&&"object"==typeof t&&!Array.isArray(t)&&!(t instanceof Date)&&!(t instanceof RegExp)&&!("function"==typeof Map&&t instanceof Map)&&!("function"==typeof Set&&t instanceof Set)},o.string=function(t){return"string"==typeof t},o.number=function(t){return"number"==typeof t},o.function=function(t){return"function"==typeof t},o.primitive=function(t){return t!==Object(t)},o.splicer=function(t){return!(!o.array(t)||t.length<1)&&(!(t.length>1&&Number.isNaN(Number.parseInt(t[1],10)))&&n(t[0],["number","function","object"]))};var a=["string","number","function","object"];o.path=function(t){return!(!t&&0!==t&&""!==t)&&[].concat(t).every(function(t){return n(t,a)})},o.dynamicPath=function(t){return t.some(function(t){return o.function(t)||o.object(t)})},o.monkeyPath=function(t,e){var r=[],n=t,o=void 0,a=void 0;for(o=0,a=e.length;o0&&p.push(m),y===v-1){if("set"===o){if(n.pure&&d[m]===a)return{node:d[m]};s.default.lazyGetter(d,m)?Object.defineProperty(d,m,{value:a,enumerable:!0,configurable:!0}):n.persistent&&!l.mutableLeaf?d[m]=h.shallowClone(a):d[m]=a}else if("monkey"===o)Object.defineProperty(d,m,{get:a,enumerable:!0,configurable:!0});else if("apply"===o){var g=a(d[m]);if(n.pure&&d[m]===g)return{node:d[m]};s.default.lazyGetter(d,m)?Object.defineProperty(d,m,{value:g,enumerable:!0,configurable:!0}):n.persistent?d[m]=h.shallowClone(g):d[m]=g}else if("push"===o){if(!s.default.array(d[m]))throw i("push","array",p);n.persistent?d[m]=d[m].concat([a]):d[m].push(a)}else if("unshift"===o){if(!s.default.array(d[m]))throw i("unshift","array",p);n.persistent?d[m]=[a].concat(d[m]):d[m].unshift(a)}else if("concat"===o){if(!s.default.array(d[m]))throw i("concat","array",p);n.persistent?d[m]=d[m].concat(a):d[m].push.apply(d[m],a)}else if("splice"===o){if(!s.default.array(d[m]))throw i("splice","array",p);n.persistent?d[m]=h.splice.apply(null,[d[m]].concat(a)):d[m].splice.apply(d[m],a)}else if("pop"===o){if(!s.default.array(d[m]))throw i("pop","array",p);n.persistent?d[m]=h.splice(d[m],-1,1):d[m].pop()}else if("shift"===o){if(!s.default.array(d[m]))throw i("shift","array",p);n.persistent?d[m]=h.splice(d[m],0,1):d[m].shift()}else if("unset"===o)s.default.object(d)?delete d[m]:s.default.array(d)&&d.splice(m,1);else if("merge"===o){if(!s.default.object(d[m]))throw i("merge","object",p);n.persistent?d[m]=h.shallowMerge({},d[m],a):d[m]=h.shallowMerge(d[m],a)}else if("deepMerge"===o){if(!s.default.object(d[m]))throw i("deepMerge","object",p);n.persistent?d[m]=h.deepMerge({},d[m],a):d[m]=h.deepMerge(d[m],a)}n.immutable&&!l.mutableLeaf&&h.deepFreeze(d);break}s.default.primitive(d[m])?d[m]={}:n.persistent&&(d[m]=h.shallowClone(d[m])),n.immutable&&v>0&&h.freeze(d),d=d[m]}return s.default.lazyGetter(d,m)?{data:f.root}:{data:f.root,node:d[m]}}r.__esModule=!0,r.default=o;var a=t("./type"),s=n(a),h=t("./helpers");e.exports=r.default},{"./helpers":4,"./type":6}],8:[function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0, -configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}r.__esModule=!0;var a=t("emmett"),s=n(a),h=t("./cursor"),u=n(h),l=t("./type"),f=n(l),c=t("./helpers"),p=function(t){function e(r,n){var o=this;i(this,e),t.call(this),this.tree=r,this.mapping=null,this.state={killed:!1},this.refresh(n),this.handler=function(t){if(!o.state.killed){var e=o.getWatchedPaths();return c.solveUpdate(t.data.paths,e)?o.emit("update"):void 0}},this.tree.on("update",this.handler)}return o(e,t),e.prototype.getWatchedPaths=function(){var t=this,e=Object.keys(this.mapping).map(function(e){var r=t.mapping[e];return r instanceof u.default?r.solvedPath:t.mapping[e]});return e.reduce(function(e,r){if(r=[].concat(r),f.default.dynamicPath(r)&&(r=c.getIn(t.tree._data,r).solvedPath),!r)return e;var n=f.default.monkeyPath(t.tree._monkeys,r);return n?e.concat(c.getIn(t.tree._monkeys,n).data.relatedPaths()):e.concat([r])},[])},e.prototype.getCursors=function(){var t=this,e={};return Object.keys(this.mapping).forEach(function(r){var n=t.mapping[r];n instanceof u.default?e[r]=n:e[r]=t.tree.select(n)}),e},e.prototype.refresh=function(t){if(!f.default.watcherMapping(t))throw c.makeError("Baobab.watch: invalid mapping.",{mapping:t});this.mapping=t;var e={};for(var r in t)e[r]=t[r]instanceof u.default?t[r].path:t[r];this.get=this.tree.project.bind(this.tree,e)},e.prototype.release=function(){this.tree.off("update",this.handler),this.state.killed=!0,this.kill()},e}(s.default);r.default=p,e.exports=r.default},{"./cursor":3,"./helpers":4,"./type":6,emmett:1}]},{},[2])(2)}); \ No newline at end of file +!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Baobab=t()}}(function(){var t;return function t(e,r,n){function i(a,s){if(!r[a]){if(!e[a]){var h="function"==typeof require&&require;if(!s&&h)return h(a,!0);if(o)return o(a,!0);var u=new Error("Cannot find module '"+a+"'");throw u.code="MODULE_NOT_FOUND",u}var l=r[a]={exports:{}};e[a][0].call(l.exports,function(t){var r=e[a][1][t];return i(r?r:t)},l,l.exports,t,e,r,n)}return r[a].exports}for(var o="function"==typeof require&&require,a=0;a1?t[r]=e(t[r],{once:!0}):t.push({once:!0}),this.on.apply(this,t)},u.prototype.off=function(t,e){var r,n,s,h;if(1===arguments.length&&"function"==typeof t){e=arguments[0];for(s in this._handlers)this._handlers[s]=a(this._handlers[s],e),0===this._handlers[s].length&&delete this._handlers[s];this._handlersAll=a(this._handlersAll,e),this._handlersComplex=a(this._handlersComplex,e)}else if(1!==arguments.length||"string"!=typeof t&&"symbol"!=typeof t)if(2===arguments.length){var u=[].concat(t);for(r=0,n=u.length;r1&&(r.data=e),s.fn.call("scope"in s?s.scope:this,r),s.once&&p.push(s);for(u=p.length-1;u>=0;u--)n=p[u].type?this._handlers[p[u].type]:p[u].pattern?this._handlersComplex:this._handlersAll,n.splice(n.indexOf(p[u]),1)}return this},u.prototype.kill=function(){this.unbindAll(),this._handlers=null,this._handlersAll=null,this._handlersComplex=null,this._enabled=!1,this.unbindAll=this.on=this.once=this.off=this.emit=this.listeners=Function.prototype},u.prototype.disable=function(){return this._enabled=!1,this},u.prototype.enable=function(){return this._enabled=!0,this},u.version="3.1.1",void 0!==n?(void 0!==r&&r.exports&&(n=r.exports=u),n.Emitter=u):"function"==typeof t&&t.amd?t("emmett",[],function(){return u}):this.Emitter=u}).call(this)},{}],2:[function(t,e,r){"use strict";function n(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e}function i(t){return t&&t.__esModule?t:{default:t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function s(t){return"λ"+t.map(function(t){return v.default.function(t)||v.default.object(t)?"#"+B()+"#":t}).join("λ")}r.__esModule=!0;var h=t("emmett"),u=i(h),l=t("./cursor"),f=i(l),c=t("./monkey"),p=t("./watcher"),d=i(p),y=t("./type"),v=i(y),g=t("./update"),m=i(g),b=t("./helpers"),_=n(b),w=_.arrayFrom,k=_.coercePath,P=_.deepFreeze,j=_.getIn,M=_.makeError,O=_.deepClone,C=_.deepMerge,E=_.shallowClone,x=_.shallowMerge,B=_.uniqid,D={autoCommit:!0,asynchronous:!0,immutable:!0,lazyMonkeys:!0,persistent:!0,pure:!0,validate:null,validationBehavior:"rollback"},z=function(t){function e(r,n){var i=this;if(o(this,e),t.call(this),arguments.length<1&&(r={}),!v.default.object(r)&&!v.default.array(r))throw M("Baobab: invalid data.",{data:r});this.options=x({},D,n),this.options.persistent||(this.options.immutable=!1,this.options.pure=!1),this._identity="[object Baobab]",this._cursors={},this._future=null,this._transaction=[],this._affectedPathsIndex={},this._monkeys={},this._previousData=null,this._data=r,this.root=new f.default(this,[],"λ"),delete this.root.release,this.options.immutable&&P(this._data);var a=function(t){i[t]=function(){var e=this.root[t].apply(this.root,arguments);return e instanceof f.default?this:e}};["apply","clone","concat","deepClone","deepMerge","exists","get","push","merge","pop","project","serialize","set","shift","splice","unset","unshift"].forEach(a),this._refreshMonkeys();var s=this.validate();if(s)throw Error("Baobab: invalid data.",{error:s})}return a(e,t),e.prototype._refreshMonkeys=function(t,e,r){var n=this,i=function t(e){var r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];if(e instanceof c.Monkey)return e.release(),void m.default(n._monkeys,r,{type:"unset"},{immutable:!1,persistent:!1,pure:!1});if(v.default.object(e))for(var i in e)t(e[i],r.concat(i))},o=function t(e){var r=arguments.length<=1||void 0===arguments[1]?[]:arguments[1];if(e instanceof c.MonkeyDefinition||e instanceof c.Monkey){var i=new c.Monkey(n,r,e instanceof c.Monkey?e.definition:e);return void m.default(n._monkeys,r,{type:"set",value:i},{immutable:!1,persistent:!1,pure:!1})}if(v.default.object(e))for(var o in e)t(e[o],r.concat(o))};if(arguments.length){var a=j(this._monkeys,e).data;a&&i(a,e),"unset"!==r&&o(t,e)}else o(this._data);return this},e.prototype.validate=function t(e){var r=this.options,t=r.validate,n=r.validationBehavior;if("function"!=typeof t)return null;var i=t.call(this,this._previousData,this._data,e||[[]]);return i instanceof Error?("rollback"===n&&(this._data=this._previousData,this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data),this.emit("invalid",{error:i}),i):null},e.prototype.select=function(t){if(t=t||[],arguments.length>1&&(t=w(arguments)),!v.default.path(t))throw M("Baobab.select: invalid path.",{path:t});t=[].concat(t);var e=s(t),r=this._cursors[e];return r||(r=new f.default(this,t,e),this._cursors[e]=r),this.emit("select",{path:t,cursor:r}),r},e.prototype.update=function(t,e){var r=this;if(t=k(t),!v.default.operationType(e.type))throw M('Baobab.update: unknown operation type "'+e.type+'".',{operation:e});var n=j(this._data,t),i=n.solvedPath,o=n.exists;if(!i)throw M("Baobab.update: could not solve the given path.",{path:i});var a=v.default.monkeyPath(this._monkeys,i);if(a&&i.length>a.length)throw M("Baobab.update: attempting to update a read-only path.",{path:i});if("unset"!==e.type||o){var h=e;if(/merge/i.test(e.type)){var u=j(this._monkeys,i).data;if(v.default.object(u)){h=E(h);var l=j(this._data,i).data;/deep/i.test(h.type)?h.value=C({},C({},l,O(u)),h.value):h.value=x({},C({},l,O(u)),h.value)}}this._transaction.length||(this._previousData=this._data);var f=m.default(this._data,i,h,this.options),c=f.data,p=f.node;if(!("data"in f))return p;var d=i.concat("push"===e.type?p.length-1:[]),y=s(d);return this._data=c,this._affectedPathsIndex[y]=!0,this._transaction.push(x({},e,{path:d})),this._refreshMonkeys(p,i,e.type),this.emit("write",{path:d}),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout(function(){return r.commit()},0)),p):(this.commit(),p):p}},e.prototype.commit=function(){if(!this._transaction.length)return this;this._future&&(this._future=clearTimeout(this._future));var t=Object.keys(this._affectedPathsIndex).map(function(t){return"λ"!==t?t.split("λ").slice(1):[]});if(this.validate(t))return this;var e=this._transaction,r=this._previousData;return this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data,this.emit("update",{paths:t,currentData:this._data,transaction:e,previousData:r}),this},e.prototype.getMonkey=function(t){t=k(t);var e=j(this._monkeys,[].concat(t)).data;return e instanceof c.Monkey?e:null},e.prototype.watch=function(t){return new d.default(this,t)},e.prototype.release=function(){var t=void 0;this.emit("release"),delete this.root,delete this._data,delete this._previousData,delete this._transaction,delete this._affectedPathsIndex,delete this._monkeys;for(t in this._cursors)this._cursors[t].release();delete this._cursors,this.kill()},e.prototype.toJSON=function(){return this.serialize()},e.prototype.toString=function(){return this._identity},e}(u.default);r.default=z,z.monkey=function(){for(var t=arguments.length,e=Array(t),r=0;r2)throw p.makeError("Baobab.Cursor."+t+": too many arguments.");if(1!==arguments.length||y[t]||(n=r,r=[]),r=p.coercePath(r),!c.default.path(r))throw p.makeError("Baobab.Cursor."+t+": invalid path.",{path:r});if(e&&!e(n))throw p.makeError("Baobab.Cursor."+t+": invalid value.",{path:r,value:n});if(!this.solvedPath)throw p.makeError("Baobab.Cursor."+t+": the dynamic path of the cursor cannot be solved.",{path:this.path});var i=this.solvedPath.concat(r);return this.tree.update(i,{type:t,value:n})}}r.__esModule=!0;var h=t("emmett"),u=n(h),l=t("./monkey"),f=t("./type"),c=n(f),p=t("./helpers"),d=function(t){function e(r,n,o){var a=this;i(this,e),t.call(this),n=n||[],this._identity="[object Cursor]",this._archive=null,this.tree=r,this.path=n,this.hash=o,this.state={killed:!1,recording:!1,undoing:!1},this._dynamicPath=c.default.dynamicPath(this.path),this._monkeyPath=c.default.monkeyPath(this.tree._monkeys,this.path),this._dynamicPath?this.solvedPath=p.getIn(this.tree._data,this.path).solvedPath:this.solvedPath=this.path,this._writeHandler=function(t){var e=t.data;!a.state.killed&&p.solveUpdate([e.path],a._getComparedPaths())&&(a.solvedPath=p.getIn(a.tree._data,a.path).solvedPath)};var s=function(t){var e=a,r=Object.defineProperties({},{previousData:{get:function(){return p.getIn(t,e.solvedPath).data},configurable:!0,enumerable:!0},currentData:{get:function(){return e.get()},configurable:!0,enumerable:!0}});return a.state.recording&&!a.state.undoing&&a.archive.add(r.previousData),a.state.undoing=!1,a.emit("update",r)};this._updateHandler=function(t){if(!a.state.killed){var e=t.data,r=e.paths,n=e.previousData,i=s.bind(a,n),o=a._getComparedPaths();return p.solveUpdate(r,o)?i():void 0}};var h=!1;this._lazyBind=function(){if(!h)return h=!0,a._dynamicPath&&a.tree.on("write",a._writeHandler),a.tree.on("update",a._updateHandler)},this._dynamicPath?this._lazyBind():(this.on=p.before(this._lazyBind,this.on.bind(this)),this.once=p.before(this._lazyBind,this.once.bind(this)))}return o(e,t),e.prototype._getComparedPaths=function(){var t=this._monkeyPath?p.getIn(this.tree._monkeys,this._monkeyPath).data.relatedPaths():[];return[this.solvedPath].concat(t)},e.prototype.isRoot=function(){return!this.path.length},e.prototype.isLeaf=function(){return c.default.primitive(this._get().data)},e.prototype.isBranch=function(){return!this.isRoot()&&!this.isLeaf()},e.prototype.root=function(){return this.tree.select()},e.prototype.select=function(t){return arguments.length>1&&(t=p.arrayFrom(arguments)),this.tree.select(this.path.concat(t))},e.prototype.up=function(){return this.isRoot()?null:this.tree.select(this.path.slice(0,-1))},e.prototype.down=function(){if(a("down",this.solvedPath),!(this._get().data instanceof Array))throw Error("Baobab.Cursor.down: cannot go down on a non-list type.");return this.tree.select(this.solvedPath.concat(0))},e.prototype.left=function(){a("left",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.left: cannot go left on a non-list type.");return t?this.tree.select(this.solvedPath.slice(0,-1).concat(t-1)):null},e.prototype.right=function(){a("right",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.right: cannot go right on a non-list type.");return t+1===this.up()._get().data.length?null:this.tree.select(this.solvedPath.slice(0,-1).concat(t+1))},e.prototype.leftmost=function(){a("leftmost",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.leftmost: cannot go left on a non-list type.");return this.tree.select(this.solvedPath.slice(0,-1).concat(0))},e.prototype.rightmost=function(){a("rightmost",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.rightmost: cannot go right on a non-list type.");var e=this.up()._get().data;return this.tree.select(this.solvedPath.slice(0,-1).concat(e.length-1))},e.prototype.map=function(t,e){a("map",this.solvedPath);var r=this._get().data,n=arguments.length;if(!c.default.array(r))throw Error("baobab.Cursor.map: cannot map a non-list type.");return r.map(function(i,o){return t.call(n>1?e:this,this.select(o),o,r)},this)},e.prototype._get=function(){var t=arguments.length<=0||void 0===arguments[0]?[]:arguments[0];if(!c.default.path(t))throw p.makeError("Baobab.Cursor.getters: invalid path.",{path:t});return this.solvedPath?p.getIn(this.tree._data,this.solvedPath.concat(t)):{data:void 0,solvedPath:null,exists:!1}},e.prototype.exists=function(t){return t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments)),this._get(t).exists},e.prototype.get=function(t){t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments));var e=this._get(t),r=e.data,n=e.solvedPath;return this.tree.emit("get",{data:r,solvedPath:n,path:this.path.concat(t)}),r},e.prototype.clone=function(){var t=this.get.apply(this,arguments);return p.shallowClone(t)},e.prototype.deepClone=function(){var t=this.get.apply(this,arguments);return p.deepClone(t)},e.prototype.serialize=function(t){if(t=p.coercePath(t),arguments.length>1&&(t=p.arrayFrom(arguments)),!c.default.path(t))throw p.makeError("Baobab.Cursor.getters: invalid path.",{path:t});if(this.solvedPath){var e=this.solvedPath.concat(t),r=p.deepClone(p.getIn(this.tree._data,e).data),n=p.getIn(this.tree._monkeys,e).data;return function t(e,r){if(c.default.object(r)&&c.default.object(e))for(var n in r)r[n]instanceof l.Monkey?delete e[n]:t(e[n],r[n])}(r,n),r}},e.prototype.project=function(t){if(c.default.object(t)){var e={};for(var r in t)e[r]=this.get(t[r]);return e}if(c.default.array(t)){for(var e=[],n=0,i=t.length;n1?e-1:0),n=1;n3?n-3:0),a=3;a=0?t.slice(0,e).concat(i).concat(t.slice(e+r)):t.slice(0,t.length+e).concat(i).concat(t.slice(t.length+e+r))}r.__esModule=!0,r.arrayFrom=s,r.before=h,r.coercePath=f,r.getIn=d,r.makeError=y,r.solveRelativePath=g,r.solveUpdate=m,r.splice=b;var _=t("./monkey"),w=t("./type"),k=n(w),P=Function.prototype,j={}.hasOwnProperty,M=function(){function t(e){i(this,t),this.size=e,this.records=[]}return t.prototype.get=function(){return this.records},t.prototype.add=function(t){return this.records.unshift(t),this.records.length>this.size&&(this.records.length=this.size),this},t.prototype.clear=function(){return this.records=[],this},t.prototype.back=function(t){var e=this.records[t-1];return e&&(this.records=this.records.slice(t)),e},t}();r.Archive=M;var O=l.bind(null,!1),C=l.bind(null,!0);r.shallowClone=O,r.deepClone=C;var E="function"==typeof Object.freeze,x=E?p.bind(null,!1):P,B=E?p.bind(null,!0):P;r.freeze=x,r.deepFreeze=B;var D={data:void 0,solvedPath:null,exists:!1},z=v.bind(null,!1),A=v.bind(null,!0);r.shallowMerge=z,r.deepMerge=A;var I=function(){var t=0;return function(){return t++}}();r.uniqid=I}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./monkey":5,"./type":6}],5:[function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}r.__esModule=!0;var o=t("./type"),a=n(o),s=t("./update"),h=n(s),u=t("./helpers"),l=function t(e){var r=this;i(this,t);var n=a.default.monkeyDefinition(e);if(!n)throw u.makeError("Baobab.monkey: invalid definition.",{definition:e});if(this.type=n,"object"===this.type)this.getter=e.get,this.projection=e.cursors||{},this.paths=Object.keys(this.projection).map(function(t){return r.projection[t]}),this.options=e.options||{};else{var o=1,s={};a.default.object(e[e.length-1])&&(o++,s=e[e.length-1]),this.getter=e[e.length-o],this.projection=e.slice(0,-o),this.paths=this.projection,this.options=s}this.paths=this.paths.map(function(t){return[].concat(t)}),this.hasDynamicPaths=this.paths.some(a.default.dynamicPath)};r.MonkeyDefinition=l;var f=function(){function t(e,r,n){var o=this;i(this,t),this.tree=e,this.path=r,this.definition=n;var a=n.projection,s=u.solveRelativePath.bind(null,r.slice(0,-1));"object"===n.type?(this.projection=Object.keys(a).reduce(function(t,e){return t[e]=s(a[e]),t},{}),this.depPaths=Object.keys(this.projection).map(function(t){return o.projection[t]})):(this.projection=a.map(s),this.depPaths=this.projection),this.state={killed:!1},this.writeListener=function(t){var e=t.data.path;if(!o.state.killed){u.solveUpdate([e],o.relatedPaths())&&o.update()}},this.recursiveListener=function(t){var e=t.data,r=e.monkey,n=e.path;if(!o.state.killed&&o!==r){u.solveUpdate([n],o.relatedPaths(!1))&&o.update()}},this.tree.on("write",this.writeListener),this.tree.on("_monkey",this.recursiveListener),this.update()}return t.prototype.relatedPaths=function(){var t=this,e=arguments.length<=0||void 0===arguments[0]||arguments[0],r=void 0;return r=this.definition.hasDynamicPaths?this.depPaths.map(function(e){return u.getIn(t.tree._data,e).solvedPath}):this.depPaths,e&&this.depPaths.some(function(e){return!!a.default.monkeyPath(t.tree._monkeys,e)})?r.reduce(function(e,r){var n=a.default.monkeyPath(t.tree._monkeys,r);if(!n)return e.concat([r]);var i=u.getIn(t.tree._monkeys,n).data;return e.concat(i.relatedPaths())},[]):r},t.prototype.update=function(){var t=this.tree.project(this.projection),e=function(t,e,r){var n=null,i=!1;return function(){return i||(n=e.getter.apply(t,"object"===e.type?[r]:r),t.options.immutable&&e.options.immutable!==!1&&u.deepFreeze(n),i=!0),n}}(this.tree,this.definition,t);if(e.isLazyGetter=!0,this.tree.options.lazyMonkeys)this.tree._data=h.default(this.tree._data,this.path,{type:"monkey",value:e},this.tree.options).data;else{var r=h.default(this.tree._data,this.path,{type:"set",value:e(),options:{mutableLeaf:!this.definition.options.immutable}},this.tree.options);"data"in r&&(this.tree._data=r.data)}return this.tree.emit("_monkey",{monkey:this,path:this.path}),this},t.prototype.release=function(){this.tree.off("write",this.writeListener),this.tree.off("_monkey",this.recursiveListener),this.state.killed=!0,delete this.projection,delete this.depPaths,delete this.tree},t}();r.Monkey=f},{"./helpers":4,"./type":6,"./update":7}],6:[function(t,e,r){"use strict";function n(t,e){return e.some(function(e){return o[e](t)})}r.__esModule=!0;var i=t("./monkey"),o={};o.array=function(t){return Array.isArray(t)},o.object=function(t){return t&&"object"==typeof t&&!Array.isArray(t)&&!(t instanceof Date)&&!(t instanceof RegExp)&&!("function"==typeof Map&&t instanceof Map)&&!("function"==typeof Set&&t instanceof Set)},o.string=function(t){return"string"==typeof t},o.number=function(t){return"number"==typeof t},o.function=function(t){return"function"==typeof t},o.primitive=function(t){return t!==Object(t)},o.splicer=function(t){return!(!o.array(t)||t.length<1)&&(!(t.length>1&&isNaN(+t[1]))&&n(t[0],["number","function","object"]))};var a=["string","number","function","object"];o.path=function(t){return!(!t&&0!==t&&""!==t)&&[].concat(t).every(function(t){return n(t,a)})},o.dynamicPath=function(t){return t.some(function(t){return o.function(t)||o.object(t)})},o.monkeyPath=function(t,e){var r=[],n=t,o=void 0,a=void 0;for(o=0,a=e.length;o0&&p.push(g),y===v-1){if("set"===o){if(n.pure&&d[g]===a)return{node:d[g]};s.default.lazyGetter(d,g)?Object.defineProperty(d,g,{value:a,enumerable:!0,configurable:!0}):n.persistent&&!l.mutableLeaf?d[g]=h.shallowClone(a):d[g]=a}else if("monkey"===o)Object.defineProperty(d,g,{get:a,enumerable:!0,configurable:!0});else if("apply"===o){var m=a(d[g]);if(n.pure&&d[g]===m)return{node:d[g]};s.default.lazyGetter(d,g)?Object.defineProperty(d,g,{value:m,enumerable:!0,configurable:!0}):n.persistent?d[g]=h.shallowClone(m):d[g]=m}else if("push"===o){if(!s.default.array(d[g]))throw i("push","array",p);n.persistent?d[g]=d[g].concat([a]):d[g].push(a)}else if("unshift"===o){if(!s.default.array(d[g]))throw i("unshift","array",p);n.persistent?d[g]=[a].concat(d[g]):d[g].unshift(a)}else if("concat"===o){if(!s.default.array(d[g]))throw i("concat","array",p);n.persistent?d[g]=d[g].concat(a):d[g].push.apply(d[g],a)}else if("splice"===o){if(!s.default.array(d[g]))throw i("splice","array",p);n.persistent?d[g]=h.splice.apply(null,[d[g]].concat(a)):d[g].splice.apply(d[g],a)}else if("pop"===o){if(!s.default.array(d[g]))throw i("pop","array",p);n.persistent?d[g]=h.splice(d[g],-1,1):d[g].pop()}else if("shift"===o){if(!s.default.array(d[g]))throw i("shift","array",p);n.persistent?d[g]=h.splice(d[g],0,1):d[g].shift()}else if("unset"===o)s.default.object(d)?delete d[g]:s.default.array(d)&&d.splice(g,1);else if("merge"===o){if(!s.default.object(d[g]))throw i("merge","object",p);n.persistent?d[g]=h.shallowMerge({},d[g],a):d[g]=h.shallowMerge(d[g],a)}else if("deepMerge"===o){if(!s.default.object(d[g]))throw i("deepMerge","object",p);n.persistent?d[g]=h.deepMerge({},d[g],a):d[g]=h.deepMerge(d[g],a)}n.immutable&&!l.mutableLeaf&&h.deepFreeze(d);break}s.default.primitive(d[g])?d[g]={}:n.persistent&&(d[g]=h.shallowClone(d[g])),n.immutable&&v>0&&h.freeze(d),d=d[g]}return s.default.lazyGetter(d,g)?{data:f.root}:{data:f.root,node:d[g]}}r.__esModule=!0,r.default=o;var a=t("./type"),s=n(a),h=t("./helpers");e.exports=r.default},{"./helpers":4,"./type":6}],8:[function(t,e,r){"use strict";function n(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)} +r.__esModule=!0;var a=t("emmett"),s=n(a),h=t("./cursor"),u=n(h),l=t("./type"),f=n(l),c=t("./helpers"),p=function(t){function e(r,n){var o=this;i(this,e),t.call(this),this.tree=r,this.mapping=null,this.state={killed:!1},this.refresh(n),this.handler=function(t){if(!o.state.killed){var e=o.getWatchedPaths();return c.solveUpdate(t.data.paths,e)?o.emit("update"):void 0}},this.tree.on("update",this.handler)}return o(e,t),e.prototype.getWatchedPaths=function(){var t=this;return Object.keys(this.mapping).map(function(e){var r=t.mapping[e];return r instanceof u.default?r.solvedPath:t.mapping[e]}).reduce(function(e,r){if(r=[].concat(r),f.default.dynamicPath(r)&&(r=c.getIn(t.tree._data,r).solvedPath),!r)return e;var n=f.default.monkeyPath(t.tree._monkeys,r);return n?e.concat(c.getIn(t.tree._monkeys,n).data.relatedPaths()):e.concat([r])},[])},e.prototype.getCursors=function(){var t=this,e={};return Object.keys(this.mapping).forEach(function(r){var n=t.mapping[r];n instanceof u.default?e[r]=n:e[r]=t.tree.select(n)}),e},e.prototype.refresh=function(t){if(!f.default.watcherMapping(t))throw c.makeError("Baobab.watch: invalid mapping.",{mapping:t});this.mapping=t;var e={};for(var r in t)e[r]=t[r]instanceof u.default?t[r].path:t[r];this.get=this.tree.project.bind(this.tree,e)},e.prototype.release=function(){this.tree.off("update",this.handler),this.state.killed=!0,this.kill()},e}(s.default);r.default=p,e.exports=r.default},{"./cursor":3,"./helpers":4,"./type":6,emmett:1}]},{},[2])(2)}); \ No newline at end of file diff --git a/package.json b/package.json index d15cd4b..1795fee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "baobab", - "version": "2.4.2", + "version": "2.4.3", "description": "JavaScript persistent data tree with cursors.", "main": "./dist/baobab.js", "dependencies": { diff --git a/src/baobab.js b/src/baobab.js index 26ca905..0605515 100644 --- a/src/baobab.js +++ b/src/baobab.js @@ -591,4 +591,4 @@ Baobab.helpers = helpers; /** * Version */ -Baobab.VERSION = '2.4.2'; +Baobab.VERSION = '2.4.3';