UNPKG

mobx-state-tree

Version:

Opinionated, transactional, MobX powered state container

1 lines 75.4 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("mobx")):"function"==typeof define&&define.amd?define(["exports","mobx"],e):e((t=t||self).mobxStateTree={},t.mobx)}(this,function(t,e){"use strict";var n,r="warn";function i(t){r=t}function o(){return r}!function(t){t.afterCreate="afterCreate",t.afterAttach="afterAttach",t.afterCreationFinalization="afterCreationFinalization",t.beforeDetach="beforeDetach",t.beforeDestroy="beforeDestroy"}(n||(n={}));var a=function(t,e){return(a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function s(t,e){function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var u=function(){return(u=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function p(t,e,n,r){var i,o=arguments.length,a=o<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,n,r);else for(var s=t.length-1;s>=0;s--)(i=t[s])&&(a=(o<3?i(a):o>3?i(e,n,a):i(e,n))||a);return o>3&&a&&Object.defineProperty(e,n,a),a}function c(t){var e="function"==typeof Symbol&&t[Symbol.iterator],n=0;return e?e.call(t):{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}}}function h(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)a.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return a}function f(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(h(arguments[e]));return t}function l(t){return ht(t).type}function d(t,e){return ht(t).onPatch(e)}function y(t,e){ht(t).applyPatches(Ct(e))}function v(t,e){return ht(t).applySnapshot(e)}function b(t,e){void 0===e&&(e=!0);var n=ht(t);return e?n.snapshot:Nt(n.type.getSnapshot(n,!1))}function g(t){return ht(t).root.storedValue}function m(t){return ht(t).path}function w(t){return ht(t).identifier}function _(t,e){var n=vt(ht(t),e,!1);if(void 0!==n)try{return n.value}catch(t){return}}function P(t){return ht(t).observableIsAlive}function S(t){var e;return{name:(e=ct(t)?l(t):t).name,properties:u({},e.properties)}}var A,I=function(){function t(t,e,n,r){this.type=t,this.environment=r,this._state=ot.INITIALIZING,this.environment=r,this.baseSetParent(e,n)}return Object.defineProperty(t.prototype,"subpath",{get:function(){return this._subpath},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"subpathUponDeath",{get:function(){return this._subpathUponDeath},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pathUponDeath",{get:function(){return this._pathUponDeath},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"value",{get:function(){return this.type.getValue(this)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"state",{get:function(){return this._state},set:function(t){var e=this.isAlive;this._state=t;var n=this.isAlive;this.aliveAtom&&e!==n&&this.aliveAtom.reportChanged()},enumerable:!0,configurable:!0}),t.prototype.fireInternalHook=function(t){this._hookSubscribers&&this._hookSubscribers.emit(t,this,t)},t.prototype.registerHook=function(t,e){return this._hookSubscribers||(this._hookSubscribers=new xt),this._hookSubscribers.register(t,e)},Object.defineProperty(t.prototype,"parent",{get:function(){return this._parent},enumerable:!0,configurable:!0}),t.prototype.baseSetParent=function(t,e){this._parent=t,this._subpath=e,this._escapedSubpath=void 0,this.pathAtom&&this.pathAtom.reportChanged()},Object.defineProperty(t.prototype,"path",{get:function(){return this.getEscapedPath(!0)},enumerable:!0,configurable:!0}),t.prototype.getEscapedPath=function(t){return t&&(this.pathAtom||(this.pathAtom=e.createAtom("path")),this.pathAtom.reportObserved()),this.parent?(void 0===this._escapedSubpath&&(this._escapedSubpath=this._subpath?Ut(this._subpath):""),this.parent.getEscapedPath(t)+"/"+this._escapedSubpath):""},Object.defineProperty(t.prototype,"isRoot",{get:function(){return null===this.parent},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isAlive",{get:function(){return this.state!==ot.DEAD},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isDetaching",{get:function(){return this.state===ot.DETACHING},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"observableIsAlive",{get:function(){return this.aliveAtom||(this.aliveAtom=e.createAtom("alive")),this.aliveAtom.reportObserved(),this.isAlive},enumerable:!0,configurable:!0}),t.prototype.baseFinalizeCreation=function(t){if(this.state===ot.CREATED){if(this.parent){if(this.parent.state!==ot.FINALIZED)return;this.fireHook(n.afterAttach)}this.state=ot.FINALIZED,t&&t()}},t.prototype.baseFinalizeDeath=function(){this._hookSubscribers&&this._hookSubscribers.clearAll(),this._subpathUponDeath=this._subpath,this._pathUponDeath=this.getEscapedPath(!1),this.baseSetParent(null,""),this.state=ot.DEAD},t.prototype.baseAboutToDie=function(){this.fireHook(n.beforeDestroy)},t}(),T=function(t){function n(e,n,r,i,o){var a=t.call(this,e,n,r,i)||this;try{a.storedValue=e.createNewInstance(o)}catch(t){throw a.state=ot.DEAD,t}return a.state=ot.CREATED,a.finalizeCreation(),a}return s(n,t),Object.defineProperty(n.prototype,"root",{get:function(){if(!this.parent)throw St("This scalar node is not part of a tree");return this.parent.root},enumerable:!0,configurable:!0}),n.prototype.setParent=function(t,e){var n=this.parent!==t,r=this.subpath!==e;(n||r)&&(this.environment=void 0,this.baseSetParent(this.parent,e))},Object.defineProperty(n.prototype,"snapshot",{get:function(){return Nt(this.getSnapshot())},enumerable:!0,configurable:!0}),n.prototype.getSnapshot=function(){return this.type.getSnapshot(this)},n.prototype.toString=function(){var t=(this.isAlive?this.path:this.pathUponDeath)||"<root>";return this.type.name+"@"+t+(this.isAlive?"":" [dead]")},n.prototype.die=function(){this.isAlive&&this.state!==ot.DETACHING&&(this.aboutToDie(),this.finalizeDeath())},n.prototype.finalizeCreation=function(){this.baseFinalizeCreation()},n.prototype.aboutToDie=function(){this.baseAboutToDie()},n.prototype.finalizeDeath=function(){this.baseFinalizeDeath()},n.prototype.fireHook=function(t){this.fireInternalHook(t)},p([e.action],n.prototype,"die",null),n}(I),C=1,E={onError:function(t){throw t}},V=function(t){function r(e,n,r,i,o){var a=t.call(this,e,n,r,i)||this;if(a.nodeId=++C,a.isProtectionEnabled=!0,a._autoUnbox=!0,a._isRunningAction=!1,a._hasSnapshotReaction=!1,a._observableInstanceState=0,a._cachedInitialSnapshotCreated=!1,a.unbox=a.unbox.bind(a),a._initialSnapshot=Nt(o),a.identifierAttribute=e.identifierAttribute,n||(a.identifierCache=new st),a._childNodes=e.initializeChildNodes(a,a._initialSnapshot),a.identifier=null,a.unnormalizedIdentifier=null,a.identifierAttribute&&a._initialSnapshot){var s=a._initialSnapshot[a.identifierAttribute];if(void 0===s){var u=a._childNodes[a.identifierAttribute];u&&(s=u.value)}if("string"!=typeof s&&"number"!=typeof s)throw St("Instance identifier '"+a.identifierAttribute+"' for type '"+a.type.name+"' must be a string or a number");a.identifier=Le(s),a.unnormalizedIdentifier=s}return n?n.root.identifierCache.addNodeToCache(a):a.identifierCache.addNodeToCache(a),a}return s(r,t),r.prototype.applyPatches=function(t){this.createObservableInstanceIfNeeded(),this._applyPatches(t)},r.prototype.applySnapshot=function(t){this.createObservableInstanceIfNeeded(),this._applySnapshot(t)},r.prototype.createObservableInstanceIfNeeded=function(){var t,r;if(0===this._observableInstanceState){this._observableInstanceState=1;for(var i=[],o=this.parent;o&&0===o._observableInstanceState;)i.unshift(o),o=o.parent;try{for(var a=c(i),s=a.next();!s.done;s=a.next()){s.value.createObservableInstanceIfNeeded()}}catch(e){t={error:e}}finally{try{s&&!s.done&&(r=a.return)&&r.call(a)}finally{if(t)throw t.error}}var u,p,h=this.type;try{this.storedValue=h.createNewInstance(this._childNodes),this.preboot(),this._isRunningAction=!0,h.finalizeNewInstance(this,this.storedValue)}catch(t){throw this.state=ot.DEAD,t}finally{this._isRunningAction=!1}this._observableInstanceState=2,u=this,p="snapshot",e.getAtom(u,p).trackAndCompute(),this.isRoot&&this._addSnapshotReaction(),this._childNodes=_t,this.state=ot.CREATED,this.fireHook(n.afterCreate),this.finalizeCreation()}},Object.defineProperty(r.prototype,"root",{get:function(){var t=this.parent;return t?t.root:this},enumerable:!0,configurable:!0}),r.prototype.clearParent=function(){if(this.parent){this.fireHook(n.beforeDetach);var t=this.state;this.state=ot.DETACHING;var e=this.root,r=e.environment,i=e.identifierCache.splitCache(this);try{this.parent.removeChild(this.subpath),this.baseSetParent(null,""),this.environment=r,this.identifierCache=i}finally{this.state=t}}},r.prototype.setParent=function(t,e){var r=t!==this.parent,i=e!==this.subpath;(r||i)&&(r?(this.environment=void 0,t.root.identifierCache.mergeCache(this),this.baseSetParent(t,e),this.fireHook(n.afterAttach)):i&&this.baseSetParent(this.parent,e))},r.prototype.fireHook=function(t){var n=this;this.fireInternalHook(t);var r=this.storedValue&&"object"==typeof this.storedValue&&this.storedValue[t];"function"==typeof r&&(e._allowStateChangesInsideComputed?e._allowStateChangesInsideComputed(function(){r.apply(n.storedValue)}):r.apply(this.storedValue))},Object.defineProperty(r.prototype,"snapshot",{get:function(){return Nt(this.getSnapshot())},enumerable:!0,configurable:!0}),r.prototype.getSnapshot=function(){return this.isAlive?2===this._observableInstanceState?this._getActualSnapshot():this._getCachedInitialSnapshot():this._snapshotUponDeath},r.prototype._getActualSnapshot=function(){return this.type.getSnapshot(this)},r.prototype._getCachedInitialSnapshot=function(){if(!this._cachedInitialSnapshotCreated){var t=this.type,e=this._childNodes,n=this._initialSnapshot;this._cachedInitialSnapshot=t.processInitialSnapshot(e,n),this._cachedInitialSnapshotCreated=!0}return this._cachedInitialSnapshot},r.prototype.isRunningAction=function(){return!!this._isRunningAction||!this.isRoot&&this.parent.isRunningAction()},r.prototype.assertAlive=function(t){var e=o();if(!this.isAlive&&"ignore"!==e){var n=this._getAssertAliveError(t);switch(e){case"error":throw St(n);case"warn":Ft(n)}}},r.prototype._getAssertAliveError=function(t){var e=this.getEscapedPath(!1)||this.pathUponDeath||"",n=t.subpath&&Ut(t.subpath)||"",r=t.actionContext||U();r&&"action"!==r.type&&r.parentActionEvent&&(r=r.parentActionEvent);var i="";r&&null!=r.name&&(i=(r&&r.context&&m(r.context)||e)+"."+r.name+"()");return"You are trying to read or write to an object that is no longer part of a state tree. (Object type: '"+this.type.name+"', Path upon death: '"+e+"', Subpath: '"+n+"', Action: '"+i+"'). Either detach nodes first, or don't use objects after removing / replacing them in the tree."},r.prototype.getChildNode=function(t){this.assertAlive({subpath:t}),this._autoUnbox=!1;try{return 2===this._observableInstanceState?this.type.getChildNode(this,t):this._childNodes[t]}finally{this._autoUnbox=!0}},r.prototype.getChildren=function(){this.assertAlive(_t),this._autoUnbox=!1;try{return 2===this._observableInstanceState?this.type.getChildren(this):gt(this._childNodes)}finally{this._autoUnbox=!0}},r.prototype.getChildType=function(t){return this.type.getChildType(t)},Object.defineProperty(r.prototype,"isProtected",{get:function(){return this.root.isProtectionEnabled},enumerable:!0,configurable:!0}),r.prototype.assertWritable=function(t){if(this.assertAlive(t),!this.isRunningAction()&&this.isProtected)throw St("Cannot modify '"+this+"', the object is protected and can only be modified by using an action.")},r.prototype.removeChild=function(t){this.type.removeChild(this,t)},r.prototype.unbox=function(t){return t?(this.assertAlive({subpath:t.subpath||t.subpathUponDeath}),this._autoUnbox?t.value:t):t},r.prototype.toString=function(){var t=(this.isAlive?this.path:this.pathUponDeath)||"<root>",e=this.identifier?"(id: "+this.identifier+")":"";return this.type.name+"@"+t+e+(this.isAlive?"":" [dead]")},r.prototype.finalizeCreation=function(){var t=this;this.baseFinalizeCreation(function(){var e,r;try{for(var i=c(t.getChildren()),o=i.next();!o.done;o=i.next()){o.value.finalizeCreation()}}catch(t){e={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}t.fireInternalHook(n.afterCreationFinalization)})},r.prototype.detach=function(){if(!this.isAlive)throw St("Error while detaching, node is not alive.");this.clearParent()},r.prototype.preboot=function(){var t=this;this._applyPatches=G(this.storedValue,"@APPLY_PATCHES",function(e){e.forEach(function(e){var n=Yt(e.path);bt(t,n.slice(0,-1)).applyPatchLocally(n[n.length-1],e)})}),this._applySnapshot=G(this.storedValue,"@APPLY_SNAPSHOT",function(e){if(e!==t.snapshot)return t.type.applySnapshot(t,e)}),jt(this.storedValue,"$treenode",this),jt(this.storedValue,"toJSON",lt)},r.prototype.die=function(){this.isAlive&&this.state!==ot.DETACHING&&(this.aboutToDie(),this.finalizeDeath())},r.prototype.aboutToDie=function(){0!==this._observableInstanceState&&(this.getChildren().forEach(function(t){t.aboutToDie()}),this.baseAboutToDie(),this._internalEventsEmit("dispose"),this._internalEventsClear("dispose"))},r.prototype.finalizeDeath=function(){this.getChildren().forEach(function(t){t.finalizeDeath()}),this.root.identifierCache.notifyDied(this);var t=this.snapshot;this._snapshotUponDeath=t,this._internalEventsClearAll(),this.baseFinalizeDeath()},r.prototype.onSnapshot=function(t){return this._addSnapshotReaction(),this._internalEventsRegister("snapshot",t)},r.prototype.emitSnapshot=function(t){this._internalEventsEmit("snapshot",t)},r.prototype.onPatch=function(t){return this._internalEventsRegister("patch",t)},r.prototype.emitPatch=function(t,e){if(this._internalEventsHasSubscribers("patch")){var n=h(function(t){if(!("oldValue"in t))throw St("Patches without `oldValue` field cannot be inversed");return[Ht(t),Lt(t)]}(function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];for(var r=0;r<e.length;r++){var i=e[r];for(var o in i)t[o]=i[o]}return t}({},t,{path:e.path.substr(this.path.length)+"/"+t.path})),2),r=n[0],i=n[1];this._internalEventsEmit("patch",r,i)}this.parent&&this.parent.emitPatch(t,e)},r.prototype.hasDisposer=function(t){return this._internalEventsHas("dispose",t)},r.prototype.addDisposer=function(t){if(this.hasDisposer(t))throw St("cannot add a disposer when it is already registered for execution");this._internalEventsRegister("dispose",t,!0)},r.prototype.removeDisposer=function(t){if(!this._internalEventsHas("dispose",t))throw St("cannot remove a disposer which was never registered for execution");this._internalEventsUnregister("dispose",t)},r.prototype.removeMiddleware=function(t){if(this.middlewares){var e=this.middlewares.indexOf(t);e>=0&&this.middlewares.splice(e,1)}},r.prototype.addMiddleWare=function(t,e){var n=this;void 0===e&&(e=!0);var r={handler:t,includeHooks:e};return this.middlewares?this.middlewares.push(r):this.middlewares=[r],function(){n.removeMiddleware(r)}},r.prototype.applyPatchLocally=function(t,e){this.assertWritable({subpath:t}),this.createObservableInstanceIfNeeded(),this.type.applyPatchLocally(this,t,e)},r.prototype._addSnapshotReaction=function(){var t=this;if(!this._hasSnapshotReaction){var n=e.reaction(function(){return t.snapshot},function(e){return t.emitSnapshot(e)},E);this.addDisposer(n),this._hasSnapshotReaction=!0}},r.prototype._internalEventsHasSubscribers=function(t){return!!this._internalEvents&&this._internalEvents.hasSubscribers(t)},r.prototype._internalEventsRegister=function(t,e,n){return void 0===n&&(n=!1),this._internalEvents||(this._internalEvents=new xt),this._internalEvents.register(t,e,n)},r.prototype._internalEventsHas=function(t,e){return!!this._internalEvents&&this._internalEvents.has(t,e)},r.prototype._internalEventsUnregister=function(t,e){this._internalEvents&&this._internalEvents.unregister(t,e)},r.prototype._internalEventsEmit=function(t){for(var e,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];this._internalEvents&&(e=this._internalEvents).emit.apply(e,f([t],n))},r.prototype._internalEventsClear=function(t){this._internalEvents&&this._internalEvents.clear(t)},r.prototype._internalEventsClearAll=function(){this._internalEvents&&this._internalEvents.clearAll()},p([e.action],r.prototype,"createObservableInstanceIfNeeded",null),p([e.computed],r.prototype,"snapshot",null),p([e.action],r.prototype,"detach",null),p([e.action],r.prototype,"die",null),r}(I);!function(t){t[t.String=1]="String",t[t.Number=2]="Number",t[t.Boolean=4]="Boolean",t[t.Date=8]="Date",t[t.Literal=16]="Literal",t[t.Array=32]="Array",t[t.Map=64]="Map",t[t.Object=128]="Object",t[t.Frozen=256]="Frozen",t[t.Optional=512]="Optional",t[t.Reference=1024]="Reference",t[t.Identifier=2048]="Identifier",t[t.Late=4096]="Late",t[t.Refinement=8192]="Refinement",t[t.Union=16384]="Union",t[t.Null=32768]="Null",t[t.Undefined=65536]="Undefined",t[t.Integer=131072]="Integer",t[t.Custom=262144]="Custom",t[t.SnapshotProcessor=524288]="SnapshotProcessor"}(A||(A={}));var O="cannotDetermine",N=function(){function t(t){this.isType=!0,this.name=t}return t.prototype.create=function(t,e){return rt(this,t),this.instantiate(null,"",e,t).value},t.prototype.getSnapshot=function(t,e){throw St("unimplemented method")},t.prototype.isAssignableFrom=function(t){return t===this},t.prototype.validate=function(t,e){if(ft(t)){var n=l(t);return this.isAssignableFrom(n)?tt():et(e,t)}return this.isValidSnapshot(t,e)},t.prototype.is=function(t){return 0===this.validate(t,[{path:"",type:this}]).length},Object.defineProperty(t.prototype,"Type",{get:function(){throw St("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`")},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"TypeWithoutSTN",{get:function(){throw St("Factory.TypeWithoutSTN should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.TypeWithoutSTN`")},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"SnapshotType",{get:function(){throw St("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`")},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"CreationType",{get:function(){throw St("Factory.CreationType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.CreationType`")},enumerable:!0,configurable:!0}),p([e.action],t.prototype,"create",null),t}(),j=function(t){function n(e){return t.call(this,e)||this}return s(n,t),n.prototype.create=function(e,n){return void 0===e&&(e=this.getDefaultSnapshot()),t.prototype.create.call(this,e,n)},n.prototype.getValue=function(t){return t.createObservableInstanceIfNeeded(),t.storedValue},n.prototype.tryToReconcileNode=function(t,e){return!t.isDetaching&&(t.snapshot===e||(!(!ct(e)||ht(e)!==t)||!(t.type!==this||!Vt(e)||ct(e)||t.identifierAttribute&&t.identifier!==Le(e[t.identifierAttribute]))&&(t.applySnapshot(e),!0)))},n.prototype.reconcile=function(t,e,n,r){if(this.tryToReconcileNode(t,e))return t.setParent(n,r),t;if(t.die(),ct(e)&&this.isAssignableFrom(l(e))){var i=ht(e);return i.setParent(n,r),i}return this.instantiate(n,r,void 0,e)},n.prototype.getSubTypes=function(){return null},p([e.action],n.prototype,"create",null),n}(N),D=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return s(e,t),e.prototype.createNewInstance=function(t){return t},e.prototype.getValue=function(t){return t.storedValue},e.prototype.getSnapshot=function(t){return t.storedValue},e.prototype.reconcile=function(t,e,n,r){if(!t.isDetaching&&t.type===this&&t.storedValue===e)return t;var i=this.instantiate(n,r,void 0,e);return t.die(),i},e.prototype.getSubTypes=function(){return null},e}(N);function x(t){return"object"==typeof t&&t&&!0===t.isType}var R=new Map;var k=function(){function t(t,e){this.hooks=t,this.call=e,this.flowsPending=0,this.running=!0,t&&t.onStart(e)}return t.prototype.finish=function(t){this.running&&(this.running=!1,this.hooks&&this.hooks.onFinish(this.call,t))},t.prototype.incFlowsPending=function(){this.flowsPending++},t.prototype.decFlowsPending=function(){this.flowsPending--},Object.defineProperty(t.prototype,"hasFlowsPending",{get:function(){return this.flowsPending>0},enumerable:!0,configurable:!0}),t}();function z(t){return{$MST_UNSERIALIZABLE:!0,type:t}}function F(t,n){e.runInAction(function(){Ct(n).forEach(function(e){return function(t,e){var n=_(t,e.path||"");if(!n)throw St("Invalid action path: "+(e.path||""));var r=ht(n);if("@APPLY_PATCHES"===e.name)return y.call(null,n,e.args[0]);if("@APPLY_SNAPSHOT"===e.name)return v.call(null,n,e.args[0]);if("function"!=typeof n[e.name])throw St("Action '"+e.name+"' does not exist in '"+r.path+"'");return n[e.name].apply(n,e.args?e.args.map(function(t){return(e=t)&&"object"==typeof e&&"$MST_DATE"in e?new Date(e.$MST_DATE):e;var e}):[])}(t,e)})})}function M(t,e,n){return void 0===n&&(n=!1),J(t,function(r,i){if("action"===r.type&&r.id===r.rootId){var o=ht(r.context),a={name:r.name,path:yt(ht(t),o),args:r.args.map(function(t,e){return function(t,e,n,r){if(r instanceof Date)return{$MST_DATE:r.getTime()};if(Ot(r))return r;if(ct(r))return z("[MSTNode: "+l(r).name+"]");if("function"==typeof r)return z("[function]");if("object"==typeof r&&!Et(r)&&!Tt(r))return z("[object "+(r&&r.constructor&&r.constructor.name||"Complex Object")+"]");try{return JSON.stringify(r),r}catch(t){return z(""+t)}}(0,r.name,0,t)})};if(n){var s=i(r);return e(a),s}return e(a),i(r)}return i(r)})}var H,L=1;function U(){return H}function $(){return L++}function W(t,r){var i=ht(t.context);"action"===t.type&&i.assertAlive({actionContext:t});var o=i._isRunningAction;i._isRunningAction=!0;var a=H;H=t;try{return function(t,r,i){var o=new Z(t,i);if(o.isEmpty)return e.action(i).apply(null,r.args);var a=null;return function t(r){var s=o.getNextMiddleware();var u=s&&s.handler;if(!u)return e.action(i).apply(null,r.args);if(!s.includeHooks&&n[r.name])return t(r);function p(e,n){a=t(e),n&&(a=n(a))}u(r,function p(t,r){a=function t(r){var s=o.getNextMiddleware();var u=s&&s.handler;if(!u)return e.action(i).apply(null,r.args);if(!s.includeHooks&&n[r.name])return t(r);function p(e,n){a=t(e),n&&(a=n(a))}u(r,p,function(t){a=t});return a}(t),r&&(a=r(a))},function(t){a=t});return a}(r)}(i,t,r)}finally{H=a,i._isRunningAction=o}}function Y(t){if(t)return"action"===t.type?t:t.parentActionEvent}function G(t,e,n){var r=function(){var r=$(),i=H,o=Y(i);return W({type:"action",name:e,id:r,args:Rt(arguments),context:t,tree:g(t),rootId:i?i.rootId:r,parentId:i?i.id:0,allParentIds:i?f(i.allParentIds,[i.id]):[],parentEvent:i,parentActionEvent:o},n)};return r._isMSTAction=!0,r}function J(t,e,n){return void 0===n&&(n=!0),ht(t).addMiddleWare(e,n)}var Z=function(){function t(t,e){this.arrayIndex=0,this.inArrayIndex=0,this.middlewares=[],e.$mst_middleware&&this.middlewares.push(e.$mst_middleware);for(var n=t;n;)n.middlewares&&this.middlewares.push(n.middlewares),n=n.parent}return Object.defineProperty(t.prototype,"isEmpty",{get:function(){return this.middlewares.length<=0},enumerable:!0,configurable:!0}),t.prototype.getNextMiddleware=function(){var t=this.middlewares[this.arrayIndex];if(t){var e=t[this.inArrayIndex++];return e||(this.arrayIndex++,this.inArrayIndex=0,this.getNextMiddleware())}},t}();function B(){for(var t=U();t&&"action"!==t.type;)t=t.parentActionEvent;return t}function K(t,e,n){for(var r="number"==typeof e?e:e.id,i=n?t:t.parentActionEvent;i;){if(i.id===r)return!0;i=i.parentActionEvent}return!1}function q(t){return"function"==typeof t?"<function"+(t.name?" "+t.name:"")+">":ct(t)?"<"+t+">":"`"+function(t){try{return JSON.stringify(t)}catch(t){return"<Unserializable: "+t+">"}}(t)+"`"}function Q(t){var e=t.value,n=t.context[t.context.length-1].type,r=t.context.map(function(t){return t.path}).filter(function(t){return t.length>0}).join("/"),i=r.length>0?'at path "/'+r+'" ':"",o=ct(e)?"value of type "+ht(e).type.name+":":Ot(e)?"value":"snapshot",a=n&&ct(e)&&n.is(ht(e).snapshot);return""+i+o+" "+q(e)+" is not assignable "+(n?"to type: `"+n.name+"`":"")+(t.message?" ("+t.message+")":"")+(n?ye(n)||Ot(e)?".":", expected an instance of `"+n.name+"` or a snapshot like `"+n.describe()+"` instead."+(a?" (Note that a snapshot of the provided value is compatible with the targeted type)":""):".")}function X(t,e,n){return t.concat([{path:e,type:n}])}function tt(){return wt}function et(t,e,n){return[{context:t,value:e,message:n}]}function nt(t){return t.reduce(function(t,e){return t.concat(e)},[])}function rt(t,e){"undefined"!=typeof process&&process.env&&"true"===process.env.ENABLE_TYPE_CHECK&&it(t,e)}function it(t,e){var n=t.validate(e,[{path:"",type:t}]);if(n.length>0)throw St(function(t,e,n){if(0===n.length)return;return"Error while converting "+(r=q(e),r.length<280?r:r.substring(0,272)+"......"+r.substring(r.length-8))+" to `"+t.name+"`:\n\n "+n.map(Q).join("\n ");var r}(t,e,n))}var ot,at=0,st=function(){function t(){this.cacheId=at++,this.cache=e.observable.map(),this.lastCacheModificationPerId=e.observable.map()}return t.prototype.updateLastCacheModificationPerId=function(t){var e=this.lastCacheModificationPerId.get(t);this.lastCacheModificationPerId.set(t,void 0===e?1:e+1)},t.prototype.getLastCacheModificationPerId=function(t){var e=this.lastCacheModificationPerId.get(t)||0;return this.cacheId+"-"+e},t.prototype.addNodeToCache=function(t,n){if(void 0===n&&(n=!0),t.identifierAttribute){var r=t.identifier;this.cache.has(r)||this.cache.set(r,e.observable.array([],Pt));var i=this.cache.get(r);if(-1!==i.indexOf(t))throw St("Already registered");i.push(t),n&&this.updateLastCacheModificationPerId(r)}},t.prototype.mergeCache=function(t){var n=this;e.values(t.identifierCache.cache).forEach(function(t){return t.forEach(function(t){n.addNodeToCache(t)})})},t.prototype.notifyDied=function(t){if(t.identifierAttribute){var e=t.identifier,n=this.cache.get(e);n&&(n.remove(t),n.length||this.cache.delete(e),this.updateLastCacheModificationPerId(t.identifier))}},t.prototype.splitCache=function(n){var r=this,i=new t,o=n.path;return e.entries(this.cache).forEach(function(t){for(var e=h(t,2),n=e[0],a=e[1],s=!1,u=a.length-1;u>=0;u--)0===a[u].path.indexOf(o)&&(i.addNodeToCache(a[u],!1),a.splice(u,1),s=!0);s&&r.updateLastCacheModificationPerId(n)}),i},t.prototype.has=function(t,e){var n=this.cache.get(e);return!!n&&n.some(function(e){return t.isAssignableFrom(e.type)})},t.prototype.resolve=function(t,e){var n=this.cache.get(e);if(!n)return null;var r=n.filter(function(e){return t.isAssignableFrom(e.type)});switch(r.length){case 0:return null;case 1:return r[0];default:throw St("Cannot resolve a reference to type '"+t.name+"' with id: '"+e+"' unambigously, there are multiple candidates: "+r.map(function(t){return t.path}).join(", "))}},t}();function ut(t,e,n,r,i){var o=ft(i);if(o){if(o.parent)throw St("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '"+(e?e.path:"")+"/"+n+"', but it lives already at '"+o.path+"'");return e&&o.setParent(e,n),o}return new V(t,e,n,r,i)}function pt(t,e,n,r,i){return new T(t,e,n,r,i)}function ct(t){return!(!t||!t.$treenode)}function ht(t){if(!ct(t))throw St("Value "+t+" is no MST Node");return t.$treenode}function ft(t){return t&&t.$treenode||null}function lt(){return ht(this).snapshot}!function(t){t[t.INITIALIZING=0]="INITIALIZING",t[t.CREATED=1]="CREATED",t[t.FINALIZED=2]="FINALIZED",t[t.DETACHING=3]="DETACHING",t[t.DEAD=4]="DEAD"}(ot||(ot={}));var dt=function(t){return".."};function yt(t,e){if(t.root!==e.root)throw St("Cannot calculate relative path: objects '"+t+"' and '"+e+"' are not part of the same object tree");for(var n=Yt(t.path),r=Yt(e.path),i=0;i<n.length&&n[i]===r[i];i++);return n.slice(i).map(dt).join("/")+Wt(r.slice(i))}function vt(t,e,n){return void 0===n&&(n=!0),bt(t,Yt(e),n)}function bt(t,e,n){void 0===n&&(n=!0);for(var r=t,i=0;i<e.length;i++){var o=e[i];if(".."===o){if(r=r.parent)continue}else{if("."===o)continue;if(r){if(r instanceof T)try{var a=r.value;ct(a)&&(r=ht(a))}catch(t){if(!n)return;throw t}if(r instanceof V)if(r.getChildType(o)&&(r=r.getChildNode(o)))continue}}if(n)throw St("Could not resolve '"+o+"' in path '"+(Wt(e.slice(0,i))||"/")+"' while resolving '"+Wt(e)+"'");return}return r}function gt(t){if(!t)return wt;var e=Object.keys(t);if(!e.length)return wt;var n=new Array(e.length);return e.forEach(function(e,r){n[r]=t[e]}),n}var mt="See https://github.com/mobxjs/mobx-state-tree/issues/399 for more information. Note that the middleware event types starting with `process` now start with `flow`.";var wt=Object.freeze([]),_t=Object.freeze({}),Pt="string"==typeof e.$mobx?{deep:!1}:{deep:!1,proxy:!1};function St(t){return void 0===t&&(t="Illegal state"),new Error("[mobx-state-tree] "+t)}function At(t){return t}Object.freeze(Pt);var It=Number.isInteger||function(t){return"number"==typeof t&&isFinite(t)&&Math.floor(t)===t};function Tt(t){return Array.isArray(t)||e.isObservableArray(t)}function Ct(t){return t?Tt(t)?t:[t]:wt}function Et(t){if(null===t||"object"!=typeof t)return!1;var e=Object.getPrototypeOf(t);return e===Object.prototype||null===e}function Vt(t){return!(null===t||"object"!=typeof t||t instanceof Date||t instanceof RegExp)}function Ot(t,e){return void 0===e&&(e=!0),null==t||!!("string"==typeof t||"number"==typeof t||"boolean"==typeof t||e&&t instanceof Date)}function Nt(t){return t}function jt(t,e,n){Object.defineProperty(t,e,{enumerable:!1,writable:!1,configurable:!0,value:n})}var Dt=function(){function t(){this.handlers=[]}return Object.defineProperty(t.prototype,"hasSubscribers",{get:function(){return this.handlers.length>0},enumerable:!0,configurable:!0}),t.prototype.register=function(t,e){var n=this;return void 0===e&&(e=!1),e?this.handlers.unshift(t):this.handlers.push(t),function(){n.unregister(t)}},t.prototype.has=function(t){return this.handlers.indexOf(t)>=0},t.prototype.unregister=function(t){var e=this.handlers.indexOf(t);e>=0&&this.handlers.splice(e,1)},t.prototype.clear=function(){this.handlers.length=0},t.prototype.emit=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.handlers.slice().forEach(function(e){return e.apply(void 0,f(t))})},t}(),xt=function(){function t(){}return t.prototype.hasSubscribers=function(t){var e=this.eventHandlers&&this.eventHandlers[t];return!!e&&e.hasSubscribers},t.prototype.register=function(t,e,n){void 0===n&&(n=!1),this.eventHandlers||(this.eventHandlers={});var r=this.eventHandlers[t];return r||(r=this.eventHandlers[t]=new Dt),r.register(e,n)},t.prototype.has=function(t,e){var n=this.eventHandlers&&this.eventHandlers[t];return!!n&&n.has(e)},t.prototype.unregister=function(t,e){var n=this.eventHandlers&&this.eventHandlers[t];n&&n.unregister(e)},t.prototype.clear=function(t){this.eventHandlers&&delete this.eventHandlers[t]},t.prototype.clearAll=function(){this.eventHandlers=void 0},t.prototype.emit=function(t){for(var e,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var i=this.eventHandlers&&this.eventHandlers[t];i&&(e=i).emit.apply(e,f(n))},t}();function Rt(t){for(var e=new Array(t.length),n=0;n<t.length;n++)e[n]=t[n];return e}function kt(t,e){return 0===t.indexOf(e)}var zt=function(t,e){};function Ft(t){console.warn(new Error("[mobx-state-tree] "+t))}function Mt(t){return function(t,e){var n=function(){var r=$(),i=U();if(!i)throw St("a mst flow must always have a parent context");var o=Y(i);if(!o)throw St("a mst flow must always have a parent action context");var a={name:t,id:r,tree:i.tree,context:i.context,parentId:i.id,allParentIds:f(i.allParentIds,[i.id]),rootId:i.rootId,parentEvent:i,parentActionEvent:o},s=arguments;function p(t,e,r){t.$mst_middleware=n.$mst_middleware,W(u({},a,{type:e,args:[r]}),t)}return new Promise(function(t,r){var i,o=function(){i=e.apply(null,arguments),c(void 0)};function c(t){var e;try{p(function(t){e=i.next(t)},"flow_resume",t)}catch(t){return void setImmediate(function(){p(function(e){r(t)},"flow_throw",t)})}f(e)}function h(t){var e;try{p(function(t){e=i.throw(t)},"flow_resume_error",t)}catch(t){return void setImmediate(function(){p(function(e){r(t)},"flow_throw",t)})}f(e)}function f(e){if(!e.done){if(!e.value||"function"!=typeof e.value.then)throw St("Only promises can be yielded to `async`, got: "+e);return e.value.then(c,h)}setImmediate(function(){p(function(e){t(e)},"flow_return",e.value)})}o.$mst_middleware=n.$mst_middleware,W(u({},a,{type:"flow_spawn",args:Rt(s)}),o)})};return n}(t.name,t)}function Ht(t){switch(t.op){case"add":return{op:"add",path:t.path,value:t.value};case"remove":return{op:"remove",path:t.path};case"replace":return{op:"replace",path:t.path,value:t.value}}}function Lt(t){switch(t.op){case"add":return{op:"remove",path:t.path};case"remove":return{op:"add",path:t.path,value:t.oldValue};case"replace":return{op:"replace",path:t.path,value:t.oldValue}}}function Ut(t){return!0==("number"==typeof t)?""+t:-1===t.indexOf("/")&&-1===t.indexOf("~")?t:t.replace(/~/g,"~0").replace(/\//g,"~1")}function $t(t){return t.replace(/~1/g,"/").replace(/~0/g,"~")}function Wt(t){if(0===t.length)return"";var e=function(t){return t.map(Ut).join("/")};return"."===t[0]||".."===t[0]?e(t):"/"+e(t)}function Yt(t){var e=t.split("/").map($t);if(!(""===t||"."===t||".."===t||kt(t,"/")||kt(t,"./")||kt(t,"../")))throw St("a json path must be either rooted, empty or relative, but got '"+t+"'");return""===e[0]&&e.shift(),e}zt.ids={};var Gt=function(t){function e(e,n,r){var i=t.call(this,r||e.name)||this;return i._subtype=e,i._processors=n,i}return s(e,t),Object.defineProperty(e.prototype,"flags",{get:function(){return this._subtype.flags|A.SnapshotProcessor},enumerable:!0,configurable:!0}),e.prototype.describe=function(){return"snapshotProcessor("+this._subtype.describe()+")"},e.prototype.preProcessSnapshot=function(t){return this._processors.preProcessor?this._processors.preProcessor.call(null,t):t},e.prototype.postProcessSnapshot=function(t){return this._processors.postProcessor?this._processors.postProcessor.call(null,t):t},e.prototype._fixNode=function(t){var e=this;!function(t,e){for(var n,r,i=[],o=2;o<arguments.length;o++)i[o-2]=arguments[o];try{for(var a=c(i),s=a.next();!s.done;s=a.next()){var u=s.value;t[u]=e[u].bind(e)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}}(t.type,this,"isAssignableFrom","create");var n=t.getSnapshot;t.getSnapshot=function(){return e.postProcessSnapshot(n.call(t))}},e.prototype.instantiate=function(t,e,n,r){var i=ct(r)?r:this.preProcessSnapshot(r),o=this._subtype.instantiate(t,e,n,i);return this._fixNode(o),o},e.prototype.reconcile=function(t,e,n,r){var i=this._subtype.reconcile(t,ct(e)?e:this.preProcessSnapshot(e),n,r);return i!==t&&this._fixNode(i),i},e.prototype.getSnapshot=function(t,e){void 0===e&&(e=!0);var n=this._subtype.getSnapshot(t);return e?this.postProcessSnapshot(n):n},e.prototype.isValidSnapshot=function(t,e){var n=this.preProcessSnapshot(t);return this._subtype.validate(n,e)},e.prototype.getSubTypes=function(){return this._subtype},e.prototype.is=function(t){return 0===this._subtype.validate(x(t)?this._subtype:this.preProcessSnapshot(t),[{path:"",type:this._subtype}]).length},e}(N);var Jt,Zt="Map.put can only be used to store complex values that have an identifier type attribute";!function(t){t[t.UNKNOWN=0]="UNKNOWN",t[t.YES=1]="YES",t[t.NO=2]="NO"}(Jt||(Jt={}));var Bt=function(t){function n(n){return t.call(this,n,e.observable.ref.enhancer)||this}return s(n,t),n.prototype.get=function(e){return t.prototype.get.call(this,""+e)},n.prototype.has=function(e){return t.prototype.has.call(this,""+e)},n.prototype.delete=function(e){return t.prototype.delete.call(this,""+e)},n.prototype.set=function(e,n){return t.prototype.set.call(this,""+e,n)},n.prototype.put=function(t){if(!t)throw St("Map.put cannot be used to set empty values");if(ct(t)){var e=ht(t);if(null===e.identifier)throw St(Zt);return this.set(e.identifier,t),t}if(Vt(t)){var n=ht(this),r=n.type;if(r.identifierMode!==Jt.YES)throw St(Zt);var i=t[r.mapIdentifierAttribute];if(!Ue(i)){var o=this.put(r.getChildType().create(t,n.environment));return this.put(b(o))}var a=Le(i);return this.set(a,t),this.get(a)}throw St("Map.put can only be used to store complex values")},n}(e.ObservableMap),Kt=function(t){function n(e,n){var r=t.call(this,e)||this;return r._subType=n,r.identifierMode=Jt.UNKNOWN,r.mapIdentifierAttribute=void 0,r.flags=A.Map,r._determineIdentifierMode(),r}return s(n,t),n.prototype.instantiate=function(t,e,n,r){return this._determineIdentifierMode(),ut(this,t,e,n,r)},n.prototype._determineIdentifierMode=function(){if(this.identifierMode===Jt.UNKNOWN){var t=[];if(function t(e,n){var r,i,o=e.getSubTypes();if(o===O)return!1;if(o){var a=Ct(o);try{for(var s=c(a),u=s.next();!u.done;u=s.next())if(!t(u.value,n))return!1}catch(t){r={error:t}}finally{try{u&&!u.done&&(i=s.return)&&i.call(s)}finally{if(r)throw r.error}}}return e instanceof ie&&n.push(e),!0}(this._subType,t)){var e=void 0;t.forEach(function(t){if(t.identifierAttribute){if(e&&e!==t.identifierAttribute)throw St("The objects in a map should all have the same identifier attribute, expected '"+e+"', but child of type '"+t.name+"' declared attribute '"+t.identifierAttribute+"' as identifier");e=t.identifierAttribute}}),e?(this.identifierMode=Jt.YES,this.mapIdentifierAttribute=e):this.identifierMode=Jt.NO}}},n.prototype.initializeChildNodes=function(t,e){void 0===e&&(e={});var n=t.type._subType,r={};return Object.keys(e).forEach(function(i){r[i]=n.instantiate(t,i,void 0,e[i])}),r},n.prototype.createNewInstance=function(t){return new Bt(t)},n.prototype.finalizeNewInstance=function(t,n){e._interceptReads(n,t.unbox),e.intercept(n,this.willChange),e.observe(n,this.didChange)},n.prototype.describe=function(){return"Map<string, "+this._subType.describe()+">"},n.prototype.getChildren=function(t){return e.values(t.storedValue)},n.prototype.getChildNode=function(t,e){var n=t.storedValue.get(""+e);if(!n)throw St("Not a child "+e);return n},n.prototype.willChange=function(t){var e=ht(t.object),n=t.name;e.assertWritable({subpath:n});var r=e.type,i=r._subType;switch(t.type){case"update":var o=t.newValue;if(o===t.object.get(n))return null;rt(i,o),t.newValue=i.reconcile(e.getChildNode(n),t.newValue,e,n),r.processIdentifier(n,t.newValue);break;case"add":rt(i,t.newValue),t.newValue=i.instantiate(e,n,void 0,t.newValue),r.processIdentifier(n,t.newValue)}return t},n.prototype.processIdentifier=function(t,e){if(this.identifierMode===Jt.YES&&e instanceof V){var n=e.identifier;if(n!==t)throw St("A map of objects containing an identifier should always store the object under their own identifier. Trying to store key '"+n+"', but expected: '"+t+"'")}},n.prototype.getSnapshot=function(t){var e={};return t.getChildren().forEach(function(t){e[t.subpath]=t.snapshot}),e},n.prototype.processInitialSnapshot=function(t){var e={};return Object.keys(t).forEach(function(n){e[n]=t[n].getSnapshot()}),e},n.prototype.didChange=function(t){var e=ht(t.object);switch(t.type){case"update":return void e.emitPatch({op:"replace",path:Ut(t.name),value:t.newValue.snapshot,oldValue:t.oldValue?t.oldValue.snapshot:void 0},e);case"add":return void e.emitPatch({op:"add",path:Ut(t.name),value:t.newValue.snapshot,oldValue:void 0},e);case"delete":var n=t.oldValue.snapshot;return t.oldValue.die(),void e.emitPatch({op:"remove",path:Ut(t.name),oldValue:n},e)}},n.prototype.applyPatchLocally=function(t,e,n){var r=t.storedValue;switch(n.op){case"add":case"replace":r.set(e,n.value);break;case"remove":r.delete(e)}},n.prototype.applySnapshot=function(t,e){rt(this,e);var n=t.storedValue,r={};if(Array.from(n.keys()).forEach(function(t){r[t]=!1}),e)for(var i in e)n.set(i,e[i]),r[""+i]=!0;Object.keys(r).forEach(function(t){!1===r[t]&&n.delete(t)})},n.prototype.getChildType=function(){return this._subType},n.prototype.isValidSnapshot=function(t,e){var n=this;return Et(t)?nt(Object.keys(t).map(function(r){return n._subType.validate(t[r],X(e,r,n._subType))})):et(e,t,"Value is not a plain object")},n.prototype.getDefaultSnapshot=function(){return _t},n.prototype.removeChild=function(t,e){t.storedValue.delete(e)},p([e.action],n.prototype,"applySnapshot",null),n}(j);var qt=function(t){function n(e,n){var r=t.call(this,e)||this;return r._subType=n,r.flags=A.Array,r}return s(n,t),n.prototype.instantiate=function(t,e,n,r){return ut(this,t,e,n,r)},n.prototype.initializeChildNodes=function(t,e){void 0===e&&(e=[]);var n=t.type._subType,r={};return e.forEach(function(e,i){var o=""+i;r[o]=n.instantiate(t,o,void 0,e)}),r},n.prototype.createNewInstance=function(t){return e.observable.array(gt(t),Pt)},n.prototype.finalizeNewInstance=function(t,n){e._getAdministration(n).dehancer=t.unbox,e.intercept(n,this.willChange),e.observe(n,this.didChange)},n.prototype.describe=function(){return this._subType.describe()+"[]"},n.prototype.getChildren=function(t){return t.storedValue.slice()},n.prototype.getChildNode=function(t,e){var n=Number(e);if(n<t.storedValue.length)return t.storedValue[n];throw St("Not a child: "+e)},n.prototype.willChange=function(t){var e=ht(t.object);e.assertWritable({subpath:""+t.index});var n=e.type._subType,r=e.getChildren();switch(t.type){case"update":if(t.newValue===t.object[t.index])return null;var i=Qt(e,n,[r[t.index]],[t.newValue],[t.index]);if(!i)return null;t.newValue=i[0];break;case"splice":var o=t.index,a=t.removedCount,s=t.added,u=Qt(e,n,r.slice(o,o+a),s,s.map(function(t,e){return o+e}));if(!u)return null;t.added=u;for(var p=o+a;p<r.length;p++)r[p].setParent(e,""+(p+s.length-a))}return t},n.prototype.getSnapshot=function(t){return t.getChildren().map(function(t){return t.snapshot})},n.prototype.processInitialSnapshot=function(t){var e=[];return Object.keys(t).forEach(function(n){e.push(t[n].getSnapshot())}),e},n.prototype.didChange=function(t){var e=ht(t.object);switch(t.type){case"update":return void e.emitPatch({op:"replace",path:""+t.index,value:t.newValue.snapshot,oldValue:t.oldValue?t.oldValue.snapshot:void 0},e);case"splice":for(var n=t.removedCount-1;n>=0;n--)e.emitPatch({op:"remove",path:""+(t.index+n),oldValue:t.removed[n].snapshot},e);for(n=0;n<t.addedCount;n++)e.emitPatch({op:"add",path:""+(t.index+n),value:e.getChildNode(""+(t.index+n)).snapshot,oldValue:void 0},e);return}},n.prototype.applyPatchLocally=function(t,e,n){var r=t.storedValue,i="-"===e?r.length:Number(e);switch(n.op){case"replace":r[i]=n.value;break;case"add":r.splice(i,0,n.value);break;case"remove":r.splice(i,1)}},n.prototype.applySnapshot=function(t,e){rt(this,e),t.storedValue.replace(e)},n.prototype.getChildType=function(){return this._subType},n.prototype.isValidSnapshot=function(t,e){var n=this;return Tt(t)?nt(t.map(function(t,r){return n._subType.validate(t,X(e,""+r,n._subType))})):et(e,t,"Value is not an array")},n.prototype.getDefaultSnapshot=function(){return wt},n.prototype.removeChild=function(t,e){t.storedValue.splice(Number(e),1)},p([e.action],n.prototype,"applySnapshot",null),n}(j);function Qt(t,e,n,r,i){for(var o,a=!0,s=0;;s++){var u=s<=r.length-1,p=n[s],c=u?r[s]:void 0,h=""+i[s];if(((o=c)instanceof T||o instanceof V)&&(c=c.storedValue),!p&&!u)break;if(u)if(p)if(te(p,c))n[s]=Xt(e,t,h,c,p);else{for(var f=void 0,l=s;l<n.length;l++)if(te(n[l],c)){f=n.splice(l,1)[0];break}a=!1;d=Xt(e,t,h,c,f);n.splice(s,0,d)}else{if(ct(c)&&ht(c).parent===t)throw St("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '"+t.path+"/"+h+"', but it lives already at '"+ht(c).path+"'");a=!1;var d=Xt(e,t,h,c);n.splice(s,0,d)}else a=!1,n.splice(s,1),p instanceof V&&p.createObservableInstanceIfNeeded(),p.die(),s--}return a?null:n}function Xt(t,e,n,r,i){rt(t,r);var o=function(){if(ct(r)){var o=ht(r);if(o.assertAlive(_t),null!==o.parent&&o.parent===e)return o.setParent(e,n),o}return i?t.reconcile(i,r,e,n):t.instantiate(e,n,void 0,r)}();return i&&i!==o&&(i instanceof V&&i.createObservableInstanceIfNeeded(),i.die()),o}function te(t,e){if(!t.isAlive)return!1;if(ct(e)){var n=ht(e);return n.isAlive&&n===t}return t.snapshot===e||t instanceof V&&null!==t.identifier&&t.identifierAttribute&&Et(e)&&t.identifier===Le(e[t.identifierAttribute])&&t.type.is(e)}function ee(){return ht(this).toString()}var ne={name:"AnonymousModel",properties:{},initializers:wt};function re(t){return Object.keys(t).reduce(function(t,e){var r,i,o;if(e in n)throw St("Hook '"+e+"' was defined as property. Hooks should be defined as part of the actions");var a=Object.getOwnPropertyDescriptor(t,e);if("get"in a)throw St("Getters are not supported as properties. Please use views instead");var s=a.value;if(null==s)throw St("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");if(Ot(s))return Object.assign({},t,((r={})[e]=Pe(function(t){switch(typeof t){case"string":return se;case"number":return ue;case"boolean":return ce;case"object":if(t instanceof Date)return de}throw St("Cannot determine primitive type from value "+t)}(s),s),r));if(s instanceof Kt)return Object.assign({},t,((i={})[e]=Pe(s,{}),i));if(s instanceof qt)return Object.assign({},t,((o={})[e]=Pe(s,[]),o));if(x(s))return t;throw St("Invalid type definition for property '"+e+"', cannot infer a type from a value like '"+s+"' ("+typeof s+")")},t)}var ie=function(t){function r(e){var n=t.call(this,e.name||ne.name)||this;n.flags=A.Object,n.named=function(t){return n.cloneAndEnhance({name:t})},n.props=function(t){return n.cloneAndEnhance({properties:t})},n.preProcessSnapshot=function(t){var e=n.preProcessor;return e?n.cloneAndEnhance({preProcessor:function(n){return e(t(n))}}):n.cloneAndEnhance({preProcessor:t})},n.postProcessSnapshot=function(t){var e=n.postProcessor;return e?n.cloneAndEnhance({postProcessor:function(n){return t(e(n))}}):n.cloneAndEnhance({postProcessor:t})};var r=e.name||ne.name;if(!/^\w[\w\d_]*$/.test(r))throw St("Typename should be a valid identifier: "+r);return Object.assign(n,ne,e),n.properties=re(n.properties),Nt(n.properties),n.propertyNames=Object.keys(n.properties),n.identifierAttribute=n._getIdentifierAttribute(),n}return s(r,t),r.prototype._getIdentifierAttribute=function(){var t=void 0;return this.forAllProps(function(e,n){if(n.flags&A.Identifier){if(t)throw St("Cannot define property '"+e+"' as object identifier, property '"+t+"' is already defined as identifier property");t=e}}),t},r.prototype.cloneAndEnhance=function(t){return new r({name:t.name||this.name,properties:Object.assign({},this.properties,t.properties),initializers:this.initializers.concat(t.initializers||[]),preProcessor:t.preProcessor||this.preProcessor,postProcessor:t.postProcessor||this.postProcessor})},r.prototype.actions=function(t){var e=this;return this.cloneAndEnhance({initializers:[function(n){return e.instantiateActions(n,t(n)),n}]})},r.prototype.instantiateActions=function(t,e){if(!Et(e))throw St("actions initializer should return a plain object containing actions");Object.keys(e).forEach(function(r){if("preProcessSnapshot"===r)throw St("Cannot define action 'preProcessSnapshot', it should be defined using 'type.preProcessSnapshot(fn)' instead");if("postProcessSnapshot"===r)throw St("Cannot define action 'postProcessSnapshot', it should be defined using 'type.postProcessSnapshot(fn)' instead");var i=e[r],o=t[r];if(r in n&&o){var a=i;i=function(){o.apply(null,arguments),a.apply(null,arguments)}}var s=i.$mst_middleware,u=i.bind(e);u.$mst_middleware=s;var p=G(t,r,u);e[r]=p,jt(t,r,p)})},r.prototype.volatile=function(t){var e=this;return this.cloneAndEnhance({initializers:[function(n){return e.instantiateVolatileState(n,t(n)),n}]})},r.prototype.instantiateVolatileState=function(t,n){if(!Et(n))throw St("volatile state initializer should return a plain object containing state");e.set(t,n)},r.prototype.extend=function(t){var e=this;return this.cloneAndEnhance({initializers:[function(n){var r=t(n),i=r.actions,o=r.views,a=r.state,s=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}(r,["actions","views","state"]);for(var u in s)throw St("The `extend` function should return an object with a subset of the fields 'actions', 'views' and 'state'. Found invalid key '"+u+"'");return a&&e.instantiateVolatileState(n,a),o&&e.instantiateViews(n,o),i&&e.instantiateActions(n,i),n}]})},r.prototype.views=function(t){var e=this;return this.cloneAndEnhance({initializers:[function(n){return e.instantiateViews(n,t(n)),n}]})},r.prototype.instantiateViews=function(t,n){if(!Et(n))throw St("views initializer should return a plain object containing views");Object.keys(n).forEach(function(r){var i=Object.getOwnPropertyDescriptor(n,r);if("get"in i)if(e.isComputedProp(t,r)){var o=e._getAdministration(t,r);o.derivation=i.get,o.scope=t,i.set&&(o.setter=e.action(o.name+"-setter",i.set))}else e.computed(t,r,i,!0);else{if("function"!=typeof i.value)throw St("A view member should either be a function or getter based property");jt(t,r,i.value)}})},r.prototype.instantiate=function(t,e,n,r){return ut(this,t,e,n,ct(r)