UNPKG

arbor-store

Version:

Seamless state management made with <3

1 lines 12 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["arbor-store"]=e():t["arbor-store"]=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var u=n[r]={i:r,l:!1,exports:{}};return t[r].call(u.exports,u,u.exports,e),u.l=!0,u.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=9)}([function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];r(this,t);var n=e.join("/");return t.cache[n]||(this.props=e,t.cache[n]=this),t.cache[n]}return u(t,null,[{key:"parse",value:function(e){return new t(e.split("/").filter(function(t){return""!==t}))}},{key:"root",get:function(){return new t}}]),u(t,[{key:"child",value:function(e){return new t(this.props.concat(e))}},{key:"match",value:function(t){return new RegExp("^"+t.toString().replace(/:index/g,"\\d+")+"$").test(this.toString())}},{key:"complement",value:function(e){var n=this;if(0===this.props.length)return e;var r=e.props.findIndex(function(t,e){return t!==n.props[e]});switch(r){case 0:case-1:return new t;default:return new t(e.props.slice(r))}}},{key:"walk",value:function(t){return this.props.reduce(function(t,e){var n=t[e].$copy;return t.$value[e]=n.$value,n},t)}},{key:"toString",value:function(){return"/"+this.props.join("/")}}]),t}();o.cache={},e.default=o},function(t,e,n){"use strict";function r(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);e<t.length;e++)n[e]=t[e];return n}return Array.from(t)}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function i(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)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=n(2),c=function(t){return t&&t.__esModule?t:{default:t}}(s),f=function(t){function e(){return u(this,e),o(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return i(e,t),a(e,[{key:"copyWithin",value:function(t,e,n){return this.$tree.mutate(this.$path,function(r){return r.$value.copyWithin(t,e,n),r})}},{key:"push",value:function(t){return this.$tree.mutate(this.$path,function(e){return e.$value.push(t)})}},{key:"pop",value:function(){return this.$tree.mutate(this.$path,function(t){return t.$value.pop()})}},{key:"reverse",value:function(){return this.$tree.mutate(this.$path,function(t){return t.$value.reverse(),t})}},{key:"shift",value:function(){return this.$tree.mutate(this.$path,function(t){return t.$value.shift()})}},{key:"sort",value:function(t){return this.$tree.mutate(this.$path,function(e){return e.$value.sort(t),e})}},{key:"splice",value:function(t,e){for(var n=arguments.length,r=Array(n>2?n-2:0),u=2;u<n;u++)r[u-2]=arguments[u];return this.$tree.mutate(this.$path,function(n){var u;return(u=n.$value).splice.apply(u,[t,e].concat(r))})}},{key:"unshift",value:function(){for(var t=arguments.length,e=Array(t),n=0;n<t;n++)e[n]=arguments[n];return this.$tree.mutate(this.$path,function(t){var n;return(n=t.$value).unshift.apply(n,e)})}},{key:"$copy",get:function(){return this.$tree.add(this.$path,[].concat(r(this.$value)))}}]),e}(c.default);e.default=f},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=n(0),i=(function(t){t&&t.__esModule}(o),function(){function t(e,n,u){r(this,t),this.$tree=e,this.$path=n,this.$value=u}return u(t,[{key:"$mutate",value:function(t){return this.$tree.mutate(this.$path,t)}},{key:"get",value:function(t,e,n){var r=Reflect.get(this,e,n);if(r)return r;this.$tree.transactions.empty()||(t=this.$refreshed.$value);var u=Reflect.get(t,e,n);if(!this.$tree.canProxify(u))return u;var o=this.$path.child(e);if(!this.$tree.nodes.byValue.has(u))return this.$tree.add(o,u);var i=this.$tree.nodes.byValue.get(u);return i.$path=o,i}},{key:"set",value:function(t,e,n,r){return"$path"===e?(this.$path=n,!0):(this.$tree.mutate(this.$path,function(t){t.$value[e]=n}),!0)}},{key:"$copy",get:function(){throw"Abstract method: Must be implemented by subclasses"}},{key:"$refreshed",get:function(){return this.$tree.nodes.byPath.get(this.$path)}}]),t}());e.default=i},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=n(1),a=r(i),s=n(4),c=r(s),f=function(){function t(e){u(this,t),this.tree=e}return o(t,[{key:"create",value:function(t,e){var n=Array.isArray(e)?a.default:c.default;return new Proxy(e,new n(this.tree,t,e))}}]),t}();e.default=f},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function u(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}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)}Object.defineProperty(e,"__esModule",{value:!0});var i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=n(2),c=function(t){return t&&t.__esModule?t:{default:t}}(s),f=function(t){function e(){return r(this,e),u(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return o(e,t),a(e,[{key:"$copy",get:function(){return this.$tree.add(this.$path,i({},this.$value))}}]),e}(c.default);e.default=f},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(e,"__esModule",{value:!0}),e.NodeFactory=e.ObjectNode=e.ArrayNode=e.Path=void 0;var u=n(6),o=r(u),i=n(0),a=r(i),s=n(1),c=r(s),f=n(4),l=r(f),h=n(3),p=r(h);e.default=o.default,e.Path=a.default,e.ArrayNode=c.default,e.ObjectNode=l.default,e.NodeFactory=p.default},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=n(0),a=r(i),s=n(7),c=r(s),f=n(3),l=r(f),h=n(8),p=r(h),y=function(){function t(){u(this,t),this.byPath=new WeakMap,this.byValue=new WeakMap}return o(t,[{key:"set",value:function(t,e){this.byPath.set(e.$path,e),this.byValue.set(t,e)}}]),t}(),v=function(){function t(){u(this,t),this.mutationPoints=[]}return o(t,[{key:"start",value:function(t){this.mutationPoints.push(t)}},{key:"commit",value:function(){return this.mutationPoints.pop()}},{key:"current",value:function(){return this.mutationPoints[this.mutationPoints.length-1]}},{key:"empty",value:function(){return 0===this.mutationPoints.length}}]),t}(),b=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};u(this,t),this.nodes=new y,this.types=new p.default,this.subscriptions=new c.default,this.transactions=new v,this.nodeFactory=new l.default(this),this.root=this.add(a.default.root,e)}return o(t,[{key:"add",value:function(t,e){var n=this.types.fetch(t),r=n?new n(e):e,u=this.nodeFactory.create(t,r);return this.nodes.set(e,u),u}},{key:"bind",value:function(t,e){this.types.register(t,e)}},{key:"mutate",value:function(t,e){var n=this.transactions.current()||this.root.$copy,r=n.$path.complement(t),u=r.walk(n);this.transactions.start(u);var o=e(u);if(this.transactions.commit(),this.transactions.empty()){var i=this.root;this.root=n,this.subscriptions.notify(n,i)}return o}},{key:"canProxify",value:function(t){var e=t&&t.constructor;return this.types.has(e)||e===Object||e===Array}},{key:"subscribe",value:function(t){var e=this;return this.subscriptions.subscribe(t),function(){e.subscriptions.unsubscribe(t)}}},{key:"state",get:function(){return this.root}}]),t}();e.default=b},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=function(){function t(){r(this,t),this.subscribers=[]}return u(t,[{key:"subscribe",value:function(t){this.subscribers.push(t)}},{key:"unsubscribe",value:function(t){this.subscribers=this.subscribers.filter(function(e){return e!==t})}},{key:"notify",value:function(t,e){this.subscribers.forEach(function(n){n(t,e)})}}]),t}();e.default=o},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),o=function(){function t(){r(this,t),this.types={}}return u(t,[{key:"fetch",value:function(t){var e=Object.keys(this.types),n=e.find(function(e){return t.match(e)});return this.types[n]}},{key:"register",value:function(t,e){this.types[t.toString()]=e}},{key:"has",value:function(t){return Object.values(this.types).includes(t)}}]),t}();e.default=o},function(t,e,n){t.exports=n(5)}])});