Comparing version 0.0.8 to 0.0.9
@@ -1,1 +0,1 @@ | ||
const t=()=>{},e=Object.create(null),s=[],n=Object.assign,i=Object.prototype.hasOwnProperty,r=(t,e)=>i.call(t,e),o=Object.prototype.toString,c=t=>o.call(t),a=Array.isArray,l=t=>"[object Map]"===c(t),h=t=>"[object Set]"===c(t),u=t=>"function"==typeof t,f=t=>"string"==typeof t,p=t=>"symbol"==typeof t,d=t=>null!==t&&"object"==typeof t,_=t=>c(t).slice(8,-1),y=t=>"[object Object]"===c(t),g=t=>f(t)&&"NaN"!==t&&"-"!==t[0]&&""+parseInt(t,10)===t,b=(t,e)=>Object.is(t,e),w=(t,e,s)=>{Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value:s})};function v(t,e){if(!t)throw new Error(`[Doura] ${e||""}`)}const m=Array.prototype.slice;function S(t){if(l(t))return new Map(t);if(h(t))return new Set(t);if(Array.isArray(t))return m.call(t);const e=Object.getOwnPropertyDescriptors(t),s=Reflect.ownKeys(e);for(let n=0;n<s.length;n++){const i=s[n],r=e[i];!1===r.writable&&(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(e[i]={configurable:!0,writable:!0,enumerable:r.enumerable,value:t[i]})}return Object.create(Object.getPrototypeOf(t),e)}const O="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function x(t,e,s=!1){y(t)?(s?Object.keys:O)(t).forEach((n=>{s&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach(((s,n)=>e(n,s,t)))}function j(t,e,s){l(t)?t.set(e,s):h(t)?(t.delete(e),t.add(s)):t[e]=s}const E=t=>t;function P(t,...e){console.warn(`[Doura warn] ${t}`,...e)}function M(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function R(t){return!!t&&!!t.__r_state}function C(t){const e=I(t);return e?C(e.base):t}function I(t){return t&&t.__r_state}function A(t){return t.copy||t.base}function D(t){t.modified||(t.modified=!0,t.parent&&D(t.parent))}let $;class k{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&$&&(this.parent=$,this.index=($.scopes||($.scopes=[])).push(this)-1)}run(t){if(this.active){const e=$;try{return $=this,t()}finally{$=e}}}on(){$=this}off(){$=this.parent}stop(t){if(this.active){let e,s;for(e=0,s=this.effects.length;e<s;e++)this.effects[e].stop();for(e=0,s=this.cleanups.length;e<s;e++)this.cleanups[e]();if(this.scopes)for(e=0,s=this.scopes.length;e<s;e++)this.scopes[e].stop(!0);if(this.parent&&!t){const t=this.parent.scopes.pop();t&&t!==this&&(this.parent.scopes[this.index]=t,t.index=this.index)}this.active=!1}}}const L=t=>{const e=new Set(t);return e.w=0,e.n=0,e},T=t=>(t.w&X)>0,N=t=>(t.n&X)>0,V=new WeakMap,H=new WeakMap;let U=0,X=1;let K;const z=Symbol(""),Y=Symbol("");let J=0;class F{constructor(t,e=null,s){this.fn=t,this.scheduler=e,this.id=J++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=$){e&&e.active&&e.effects.push(t)}(this,s)}run(){if(!this.active)return this.fn();let t=K;const e=B;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=K,K=this,B=!0,X=1<<++U,U<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=X})(this):W(this),this.fn()}finally{U<=30&&(t=>{const{deps:e}=t;if(e.length){let s=0;for(let n=0;n<e.length;n++){const i=e[n];T(i)&&!N(i)?i.delete(t):e[s++]=i,i.w&=~X,i.n&=~X}e.length=s}})(this),X=1<<--U,K=this.parent,B=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){K===this?this.deferStop=!0:this.active&&(W(this),this.onStop&&this.onStop(),this.active=!1)}}function W(t){const{deps:e}=t;if(e.length){for(let s=0;s<e.length;s++)e[s].delete(t);e.length=0}}let B=!0;const q=[];function G(){q.push(B),B=!1}function Q(){const t=q.pop();B=void 0===t||t}function Z(t,e,s){if(B&&K){let e=V.get(t);e||V.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=L()),et(n)}}function tt(t){if(B&&K){const e=t.__r_state;if(!e)return;let s=H.get(e);s||H.set(e,s=L()),et(s)}}function et(t){let e=!1;U<=30?N(t)||(t.n|=X,e=!T(t)):e=!t.has(K),e&&(t.add(K),K.deps.push(t))}function st(t){const e=H.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function nt(t,e,s,n,i){var r;const o=V.get(t),c=t.base;let h=[];if(o)if("clear"===e)h=[...o.values()];else if("length"===s&&a(c))o.forEach(((t,e)=>{("length"===e||e>=n)&&h.push(t)}));else switch(void 0!==s&&h.push(o.get(s)),e){case"add":a(c)?g(s)&&h.push(o.get("length")):(h.push(o.get(z)),l(c)&&h.push(o.get(Y)));break;case"delete":a(c)||(h.push(o.get(z)),l(c)&&h.push(o.get(Y)));break;case"set":l(c)&&h.push(o.get(z))}if(1===h.length)h[0]&&it(h[0]);else{const t=[];for(const e of h)e&&t.push(...e);it(L(t))}const u=null===(r=t.root)||void 0===r?void 0:r.listeners;u&&u.length&&u.forEach((t=>t()))}function it(t){const e=a(t)?t:[...t];for(const s of e)s.view&&rt(s);for(const s of e)s.view||rt(s)}function rt(t){(t!==K||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class ot{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this.dirty=!0,this.effect=new F(t,(()=>{this.dirty||(this.dirty=!0,function(t){(t=C(t)).dep&&it(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=C(this);return function(t){B&&K&&et((t=C(t)).dep||(t.dep=L()))}(t),!t.dirty&&t._cacheable||(t.dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const ct=new Set(["__proto__"]),at=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function lt(t){t.copy||(t.copy=S(t.base))}function ht(t,e){const s=t.__r_state;return(s?A(s):t)[e]}const ut=dt(),ft=pt();function pt(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=I(this),n=A(s);for(let e=0,r=this.length;e<r;e++)Z(s,0,e+"");const i=n[e](...t);return-1===i||!1===i?n[e](...t.map(C)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){G();const s=A(I(this))[e].apply(this,t);return Q(),s}})),t}function dt(){return function(t,e,s){const n=A(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&s===Rt.get(t))return t;if(a(n)&&r(ft,e))return Reflect.get(ft,e,s);let i=Reflect.get(n,e,s);return(p(e)?at.has(e):ct.has(e))?i:(Z(t,0,e),r(n,e)?(t.disposed||!d(i)||(R(i)||(lt(t),i=t.copy[e]=It(i,t)),tt(i)),i):i)}}function _t(){return function(t,e,s,n){const i=A(t),o=ht(i,e),c=a(i)&&g(e)?Number(e)<i.length:r(i,e);if(!t.modified){const n=null==o?void 0:o.__r_state;if(n&&n.base===s)return t.copy[e]=s,!0;if(b(s,o)&&(void 0!==s||r(t.base,e)))return!0;lt(t),D(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===I(n)&&(c?b(s,o)||nt(t,"set",e,s):nt(t,"add",e,s),function(t){let e=t;for(;e;)st(e),e=e.parent}(t))),!0}}const yt={get:ut,set:_t(),deleteProperty:function(t,e){const s=r(A(t),e);if((void 0!==ht(t.base,e)||e in t.base)&&(lt(t),D(t)),t.copy){const n=delete t.copy[e];return n&&s&&nt(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=A(t),n=Reflect.has(s,e);return p(e)&&at.has(e)||Z(t,0,e),n},ownKeys:function(t){const e=A(t);return Z(t,0,a(e)?"length":z),Reflect.ownKeys(e)},setPrototypeOf:function(t,e){const s=Reflect.setPrototypeOf(t.base,e);return s&&t.copy&&Reflect.setPrototypeOf(t.copy,e),s},getOwnPropertyDescriptor:function(t,e){const s=A(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!a(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function gt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=It(e,t);tt(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function bt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):gt(t)}function wt(t){const e=this.__r_state,s=A(e);if(Z(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(R(n)||(bt(e),n=It(n,e),e.copy.set(t,n)),tt(n),n):n}function vt(t,e){const s=this.__r_state,n=A(s),i=n.has(t),r=n.get(t),o=()=>{bt(s),D(s),s.copy.set(t,e)};return i?b(e,r)||(o(),nt(s,"set",t,e)):(o(),nt(s,"add",t,e)),this}function mt(t){const e=this.__r_state;return A(e).has(t)||(gt(e),D(e),e.copy.add(t),nt(e,"add",t,t)),this}function St(t){const e=this.__r_state;return Z(e,0,t),e.copy?!!e.copy.has(t)||2===e.type&&(e.drafts.has(t)&&e.drafts.has(e.drafts.get(t))):e.base.has(t)}function Ot(t){const e=this.__r_state,s=A(e),n=s.has(t);"get"in s&&s.get(t),bt(e),D(e);let i=e.copy.delete(t);return 2!==e.type||i||(i=!!e.drafts.has(t)&&e.drafts.delete(e.drafts.get(t))),n&&nt(e,"delete",t,void 0),i}function xt(){const t=this.__r_state,e=A(t),s=0!==e.size,n=e.clear();return s&&(bt(t),D(t),t.copy.clear(),nt(t,"clear",void 0,void 0)),n}function jt(){const t={get:wt,get size(){return Z(t=this.__r_state,0,z),A(t).size;var t},has:St,add:mt,set:vt,delete:Ot,clear:xt,keys(){const t=this.__r_state;return 1===t.type?function(t){return Z(t,0,Y),A(t).keys()}(t):function(t){return Z(t,0,z),gt(t),t.copy.values()}(t)},forEach(t,e){const s=this.__r_state;1===s.type?function(t,e,s,n){Z(e,0,z),A(e).forEach(((i,r)=>s.call(n,e.proxy.get(r),r,t)))}(this,s,t,e):function(t,e,s,n){const i=e.proxy.values();let r=i.next();for(;!r.done;)s.call(n,r.value,r.value,t),r=i.next()}(this,s,t,e)}};return["values","entries",Symbol.iterator].forEach((e=>{t[e]=function(t){return function(...e){const s=this.__r_state,n=1===s.type,i="entries"===t||t===Symbol.iterator&&n;if(n){const t=s.proxy.keys();return{next(){const{value:e,done:n}=t.next(),r=s.proxy.get(e);return n?{value:r,done:n}:{value:i?[e,r]:r,done:n}},[Symbol.iterator](){return this}}}return Z(s,0,z),gt(s),s.copy[t](...e)}}(e)})),[t]}const[Et]=jt();function Pt(){const t=Et;return(e,s,n)=>{if("__r_isReactive"===s)return!0;if("__r_state"===s)return e;const i=A(e);return Reflect.get(r(t,s)&&s in i?t:i,s,n)}}const Mt={get:Pt()};const Rt=new WeakMap;let Ct=0;function It(t,e){const s=M(t);if(0===s)return t;if(t.__r_skip||!Object.isExtensible(t))return t;if(t.__r_state)return t;let n={type:0,id:Ct++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=yt;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=Mt):4===s?(n.type=1,i=new Map,r=Mt):2===s&&(i=[]),i!==n&&Object.keys(n).forEach((t=>{Object.defineProperty(i,t,{configurable:!0,enumerable:!0,writable:!0,value:n[t]})}));const o=new Proxy(i,r);return i.proxy=o,e?(i.root=e.root,e.children.push(i)):i.root=i,Rt.set(i,o),i.proxy}function At(t,e){const s=function(t,{copies:e,proxies:s}){const n=t=>{switch(M(t)){case 1:case 2:return u;case 4:case 3:return h;default:throw new Error("Unpected Error. Please file an issue on https://github.com/dourajs/doura")}},i=t=>{if(!d(t))return t;if(R(t)){let i=s.get(t);if(!i){const r=e.get(t.__r_state);s.set(t,i=new Proxy(r,n(r)))}return i}return t},o=t=>function(...e){const s=this.__r_raw,n="entries"===t||t===Symbol.iterator&&l(this),r=s[t](...e);return{next(){const{value:t,done:e}=r.next();return e?{value:i(t),done:e}:{value:n?[t[0],i(t[1])]:i(t),done:e}},[Symbol.iterator](){return this}}},c=t=>function(...e){return this.__r_raw[t](...e)},a={get size(){return this.__r_raw.size},get(t){const e=this.__r_raw;if(e.has(t))return i(e.get(t))},forEach(t,e){const s=this.__r_raw;return s.forEach(((n,r)=>(n=i(n),t.call(e,n,l(s)?r:n,s))))}};["has","add","set","delete","clear"].forEach((t=>{a[t]=c(t)})),["keys","values","entries",Symbol.iterator].forEach((t=>{a[t]=o(t)}));const h={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(r(a,e)&&e in t?a:t,e,s)},u={get:(t,e,s)=>i(Reflect.get(t,e,s))};return n(t)}(t,e);if(R(t)){return new Proxy(e.copies.get(t.__r_state),s)}return new Proxy(S(t),s)}function Dt(t,e){if(!d(t))return t;const s=function(t){const e=new Map,s={copies:e,proxies:new Map},n=[t.__r_state];for(;n.length;){const t=n.pop();let s;t.modified?(s=S(t.copy),$t(t,s)):s=t.base,e.set(t,s);for(const e of t.children)n.push(e)}return s}(e);return At(t,s)}function $t(t,e){t.modified=!1,t.base=e}function kt(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=s.getSnapshot;return n.destory=function(){s.effect.stop();const e=t.effectScope.effects.indexOf(s.effect);e>=0&&t.effectScope.effects.splice(e,1)},n}const Lt=n({$name:t=>t.name,$rawState:t=>t.getState(),$state:t=>t.stateValue,$actions:t=>t.actions,$views:t=>t.views,$patch:t=>t.patch,$onAction:t=>t.onAction,$subscribe:t=>t.subscribe,$isolate:t=>t.isolate,$getApi:t=>t.getApi,$createView:t=>kt.bind(null,t)}),Tt=t=>({_:e},s)=>{const{actions:n,views:i,accessCache:o,ctx:c}=e;let a;if(a=t?e.getState():e.stateValue,"$"!==s[0]){const t=o[s];if(void 0!==t)switch(t){case 0:return a[s];case 2:return i[s];case 1:return n[s];case 3:return c[s]}else{if(r(a,s))return o[s]=0,a[s];if(r(c,s))return o[s]=3,c[s]}}const l=Lt[s];return l?l(e):r(c,s)?(o[s]=3,c[s]):void((t=>"_"===t||"$"===t)(s[0])&&r(a,s)&&P(`Property ${JSON.stringify(s)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},Nt=({_:t},e,s)=>{const{ctx:n,actions:i,views:o,accessContext:c,stateRef:{value:a}}=t;return r(a,e)?1!==c&&(a[e]=s,!0):!r(i,e)&&(!r(o,e)&&("$state"===e?"bigint"!=typeof s&&"symbol"!=typeof s&&(t.replace(s),!0):("$"!==e[0]||!r(Lt,e))&&(n[e]=s,!0)))},Vt={get:Tt(!1),set:Nt},Ht={get:Tt(!0),set:Nt};function Ut(t,e,...s){console.error(t)}let Xt=!1,Kt=!1;const zt=[];let Yt=0;const Jt=[];let Ft=null,Wt=0;const Bt=[];let qt=null,Gt=0;const Qt=Promise.resolve();let Zt=null,te=null;function ee(t){const e=Zt||Qt;return t?e.then(this?t.bind(this):t):e}function se(t){zt.length&&zt.includes(t,Xt&&t.allowRecurse?Yt+1:Yt)||t===te||(null==t.id?zt.push(t):zt.splice(function(t){let e=Yt+1,s=zt.length;for(;e<s;){const n=e+s>>>1;ie(zt[n])<t?e=n+1:s=n}return e}(t.id),0,t),Xt||Kt||(Kt=!0,Zt=Qt.then(re)))}function ne(t,e=null){if(Jt.length){for(te=e,Ft=[...new Set(Jt)],Jt.length=0,Wt=0;Wt<Ft.length;Wt++)Ft[Wt]();Ft=null,Wt=0,te=null,ne(t,e)}}const ie=t=>null==t.id?1/0:t.id;function re(t){Kt=!1,Xt=!0,ne(t),zt.sort(((t,e)=>ie(t)-ie(e)));try{for(Yt=0;Yt<zt.length;Yt++){const t=zt[Yt];if(t&&!1!==t.active){0;try{t()}catch(e){Ut(e)}}}}finally{Yt=0,zt.length=0,function(t){if(ne(),Bt.length){const t=[...new Set(Bt)];if(Bt.length=0,qt)return void qt.push(...t);for(qt=t,qt.sort(((t,e)=>ie(t)-ie(e))),Gt=0;Gt<qt.length;Gt++)qt[Gt]();qt=null,Gt=0}}(),Xt=!1,Zt=null,(zt.length||Jt.length||Bt.length)&&re(t)}}var oe;function ce(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{r(t,s)&&y(e[s])?ce(t[s],e[s]):t[s]=e[s]}))}function ae(t){t.dirty=!0}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(oe||(oe={}));class le{constructor(e,{name:s,initState:n}){this.viewInstances=[],this._actionDepth=0,this._api=null,this._actionListeners=new Set,this._subscribers=new Set,this._depListenersHandlers=[],this._watchStateChange=!0,this._destroyed=!1,this.patch=this.patch.bind(this),this.onAction=this.onAction.bind(this),this.subscribe=this.subscribe.bind(this),this.isolate=this.isolate.bind(this),this.getApi=this.getApi.bind(this),this.options=e,this.name=s||"",this._isDispatching=!1,this._initState=n||e.state,this.stateRef=It({value:this._initState});const i=this._update.bind(this);var r;this._draftListenerHandler=function(e,s){const n=e.__r_state;return n.disposed?t:(n.listeners.push(s),()=>{const t=n.listeners.indexOf(s);t>=0&&n.listeners.splice(t,1)})}(this.stateRef,(()=>{this._watchStateChange&&se(i)})),this._setState(this._initState),this.actions=Object.create(null),this.views=Object.create(null),this.accessContext=0,this.ctx={},w(this.ctx,"_",this),this.accessCache=Object.create(null),this.proxy=new Proxy(this.ctx,Vt),this.publicInst=new Proxy(this.ctx,Ht),this.effectScope=new k(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,ce(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:oe.PATCH,args:{patch:t}}))}replace(t){if(d(t)){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const t of this.viewInstances)t.effect.scheduler();this.dispatch({type:oe.REPLACE,payload:t})}else P(`replace argument should be an object, but receive a ${Object.prototype.toString.call(t)}`)}getState(){return this._currentState}getApi(){if(null===this._api){const t=this._api=Object.assign(Object.assign({},this._currentState),this.views);for(const e of Object.keys(this.actions))w(t,e,this.actions[e])}return this._api}onAction(t){return this._actionListeners.add(t),()=>{this._actionListeners.delete(t)}}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}isolate(t){G();const e=t(this.stateValue);return Q(),e}depend(t){this._depListenersHandlers.push(t.subscribe((t=>{this._triggerListener(Object.assign(Object.assign({},t),{model:this.proxy}))})))}createView(t){let e;return this.effectScope.run((()=>{e=function(t,e={}){return new ot(t,e)}((()=>{const n=this.accessContext;this.accessContext=1;const i=e.__externalArgs;try{return t.call(this.proxy,this.proxy,...i||s)}finally{this.accessContext=n}}))})),e.getSnapshot=()=>{const t=e.value;return e.mightChange?(e.mightChange=!1,e.__snapshot=Dt(t,this.stateRef.value)):e.__pre!==t&&(e.__snapshot=Dt(t,this.stateRef.value)),e.__pre=t,e.__snapshot},this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case oe.MODIFY:case oe.PATCH:return Dt(this.stateRef.value,this.stateRef.value);case oe.REPLACE:return e.payload;default:return t}}dispatch(t){if(void 0===t.type)return t;if(this._isDispatching)return t;let e;try{this._isDispatching=!0,e=this.reducer(this._currentState,t)}finally{this._isDispatching=!1}return e!==this._currentState&&(this._setState(e),this._triggerListener(Object.assign({type:t.type,model:this.proxy,target:this.proxy},t.args))),t}destroy(){this._destroyed=!0,this._api=null,this._currentState=null,this.stateRef={value:null},this._subscribers.clear(),this.effectScope.stop();for(const t of this._depListenersHandlers)t();this._draftListenerHandler()}_setState(t){this._api=null,this._currentState=t,this.stateValue=this.stateRef.value}_update(){!this._destroyed&&function(t){return t.__r_state.modified}(this.stateRef.value)&&this.dispatch({type:oe.MODIFY})}_triggerListener(t){for(const e of this._subscribers)e(t)}_initActions(){const t=this.options.actions;if(t)for(const e of Object.keys(t)){this.accessCache[e]=1;const s=t[e];Object.defineProperty(this.actions,e,{configurable:!0,enumerable:!0,writable:!1,value:(...t)=>{if(1===this.accessContext)return;let n;this._actionDepth++;try{for(const s of this._actionListeners)s({name:e,args:t});n=s.call(this.proxy,...t)}finally{0==--this._actionDepth&&this._update()}return n}})}}_initViews(){const t=this.options.views;if(t)for(const e of Object.keys(t)){this.accessCache[e]=2;const s=t[e],n=s.length>1,i=this.createView(s),r=i;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get:n?()=>(...t)=>{const e=r.__externalArgs;if(e){if(e.length!==t.length)ae(i);else for(let s=0;s<e.length;s++)if(e[s]!==t[s]){ae(i);break}}else ae(i);return r.__externalArgs=t,i.getSnapshot()}:i.getSnapshot,set:()=>!1})}}}function he(t,e={}){return new le(t,e)}let ue=null;function fe(t){ue=t}class pe{constructor(t=e,s=[]){this._subscribers=new Set,this._initialState=t,this._models=(()=>{const t=new Map,e={get:e=>t.get(e),set(e,s){t.set(e,s)},each(e){for(const[s,n]of t.entries())e(n,s)},clear(){e.each((t=>t.destroy())),t.clear()}};return e})();const n=()=>{for(const t of this._subscribers)t()};this._onModelChange=()=>{se(n)},this._hooks=s.map((([t,e])=>t(e))),this._hooks.map((e=>{var s;return null===(s=e.onInit)||void 0===s?void 0:s.call(e,{initialState:t},{doura:this})}))}getModel(t,e){return this.getModelInstance({name:t,model:e}).publicInst}getDetachedModel(t){return this.getModelInstance({model:t}).publicInst}getModelInstance({name:t,model:e}){const s=t&&this._models.get(t);if(s)return s;let n;if("function"==typeof e){const s=ue,i=this._createModelProxy();try{fe({manager:this,model:i}),n=this._initModel({name:t,model:e()})}finally{fe(s)}i.setModel(n)}else{if("object"!=typeof e)throw new Error("invalid model");n=this._initModel({name:t,model:e})}return n}getState(){const t={};return this._models.each(((e,s)=>{t[s]=e.getState()})),t}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}destroy(){this._hooks.map((t=>{var e;return null===(e=t.onDestroy)||void 0===e?void 0:e.call(t)})),this._models.clear(),this._subscribers.clear(),this._initialState=e}_createModelProxy(){const t=[];return{addChild(e){t.push(e)},setModel(e){for(const s of t)e.depend(s);t.length=0}}}_initModel({name:t,model:e}){if(!t)return he(e);this._hooks.map((s=>{var n;return null===(n=s.onModel)||void 0===n?void 0:n.call(s,t,e,{doura:this})}));const s=he(e,{name:t,initState:this._getInitialState(t)});return s.subscribe(this._onModelChange),this._models.set(t,s),this._hooks.map((t=>{var e;null===(e=t.onModelInstance)||void 0===e||e.call(t,s.publicInst,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}function de(t,e){if(!ue)throw new Error("Invalid use() call. use() can only be called inside of the body of a function model. This could happen for one of the following reasons:\n1. You might be breaking the Rules of Doura\n2. You might have more than one copy of Doura in the same app");const{manager:s,model:n}=ue;let i,r;return"string"==typeof t?(r=t,i=s.getModelInstance({name:r,model:e})):i=s.getModelInstance({model:e=t}),n.addChild(i),i.proxy}function _e({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new pe(t,e)}({initialState:t,plugins:e})}var ye=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit(n,{doura:i}){if(window.__doura=i,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(i.getState());const n=t=>{switch(t.type){case"ACTION":return void v("string"==typeof t.payload,"Unsupported action format");case"DISPATCH":switch(t.payload.type){case"RESET":case"COMMIT":return s.init(i.getState());case"ROLLBACK":try{const e=JSON.parse(t.state);return s.init(e)}catch(e){console.warn("[Doura Devtool] Could not parse the received json.")}return s.init(i.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(n))}},onModelInstance(t,{doura:e}){t.$subscribe((t=>{var{type:n,model:i}=t,r=function(t,e){var s={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(s[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(t);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(t,n[i])&&(s[n[i]]=t[n[i]])}return s}(t,["type","model"]);const o=e.getState();delete o._,s.send(Object.assign({type:`${i.$name||"anonymous"}@${n}`},r),o)}))},onDestroy(){window.__REDUX_DEVTOOLS_EXTENSION__.disconnect(s);for(const t of e.values())t();e.clear()}}};export{t as NOOP,n as assign,w as def,E as defineModel,ye as devtool,_e as doura,It as draft,x as each,s as emptyArray,e as emptyObject,r as hasOwn,v as invariant,b as is,a as isArray,u as isFunction,g as isIntegerKey,l as isMap,d as isObject,y as isPlainObject,h as isSet,f as isString,p as isSymbol,ee as nextTick,o as objectToString,O as ownKeys,j as set,S as shallowCopy,Dt as snapshot,_ as toRawType,c as toTypeString,de as use}; | ||
const t=()=>{},e=Object.create(null),s=[],n=Object.assign,i=Object.prototype.hasOwnProperty,r=(t,e)=>i.call(t,e),o=Object.prototype.toString,c=t=>o.call(t),a=Array.isArray,l=t=>"[object Map]"===c(t),h=t=>"[object Set]"===c(t),u=t=>"function"==typeof t,f=t=>"string"==typeof t,p=t=>"symbol"==typeof t,d=t=>null!==t&&"object"==typeof t,_=t=>c(t).slice(8,-1),y=t=>"[object Object]"===c(t),g=t=>f(t)&&"NaN"!==t&&"-"!==t[0]&&""+parseInt(t,10)===t,b=(t,e)=>Object.is(t,e),w=(t,e,s)=>{Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value:s})};function v(t,e){if(!t)throw new Error(`[Doura] ${e||""}`)}const m=Array.prototype.slice;const S=Object.prototype.propertyIsEnumerable;function O(t){return Array.isArray(t)?m.call(t):l(t)?new Map(t):h(t)?new Set(t):Object.getPrototypeOf(t)===Object.prototype?function(t){const e={},s=x(t);for(let n=0;n<s.length;n++){const i=s[n],r=t[i];S.call(t,i)?e[i]=r:Object.defineProperty(e,i,{configurable:!0,writable:!0,enumerable:!1,value:r})}return e}(t):function(t){const e=Object.getOwnPropertyDescriptors(t),s=x(e);for(let n=0;n<s.length;n++){const i=s[n],r=e[i];!1===r.writable&&(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(e[i]={configurable:!0,writable:!0,enumerable:r.enumerable,value:t[i]})}return Object.create(Object.getPrototypeOf(t),e)}(t)}const x="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function j(t,e,s=!1){y(t)?(s?Object.keys:x)(t).forEach((n=>{s&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach(((s,n)=>e(n,s,t)))}function E(t,e,s){l(t)?t.set(e,s):h(t)?(t.delete(e),t.add(s)):t[e]=s}const P=t=>t;function M(t,...e){console.warn(`[Doura warn] ${t}`,...e)}function R(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function C(t){return!!t&&!!t.__r_state}function I(t){const e=A(t);return e?I(e.base):t}function A(t){return t&&t.__r_state}function D(t){return t.copy||t.base}function $(t){t.modified||(t.modified=!0,t.parent&&$(t.parent))}let k;class L{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&k&&(this.parent=k,this.index=(k.scopes||(k.scopes=[])).push(this)-1)}run(t){if(this.active){const e=k;try{return k=this,t()}finally{k=e}}}on(){k=this}off(){k=this.parent}stop(t){if(this.active){let e,s;for(e=0,s=this.effects.length;e<s;e++)this.effects[e].stop();for(e=0,s=this.cleanups.length;e<s;e++)this.cleanups[e]();if(this.scopes)for(e=0,s=this.scopes.length;e<s;e++)this.scopes[e].stop(!0);if(this.parent&&!t){const t=this.parent.scopes.pop();t&&t!==this&&(this.parent.scopes[this.index]=t,t.index=this.index)}this.active=!1}}}const T=t=>{const e=new Set(t);return e.w=0,e.n=0,e},N=t=>(t.w&K)>0,V=t=>(t.n&K)>0,H=new WeakMap,U=new WeakMap;let X=0,K=1;let z;const Y=Symbol(""),J=Symbol("");let F=0;class W{constructor(t,e=null,s){this.fn=t,this.scheduler=e,this.id=F++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=k){e&&e.active&&e.effects.push(t)}(this,s)}run(){if(!this.active)return this.fn();let t=z;const e=q;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=z,z=this,q=!0,K=1<<++X,X<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=K})(this):B(this),this.fn()}finally{X<=30&&(t=>{const{deps:e}=t;if(e.length){let s=0;for(let n=0;n<e.length;n++){const i=e[n];N(i)&&!V(i)?i.delete(t):e[s++]=i,i.w&=~K,i.n&=~K}e.length=s}})(this),K=1<<--X,z=this.parent,q=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){z===this?this.deferStop=!0:this.active&&(B(this),this.onStop&&this.onStop(),this.active=!1)}}function B(t){const{deps:e}=t;if(e.length){for(let s=0;s<e.length;s++)e[s].delete(t);e.length=0}}let q=!0;const G=[];function Q(){G.push(q),q=!1}function Z(){const t=G.pop();q=void 0===t||t}function tt(t,e,s){if(q&&z){let e=H.get(t);e||H.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=T()),st(n)}}function et(t){if(q&&z){const e=t.__r_state;if(!e)return;let s=U.get(e);s||U.set(e,s=T()),st(s)}}function st(t){let e=!1;X<=30?V(t)||(t.n|=K,e=!N(t)):e=!t.has(z),e&&(t.add(z),z.deps.push(t))}function nt(t){const e=U.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function it(t,e,s,n,i){var r;const o=H.get(t),c=t.base;let h=[];if(o)if("clear"===e)h=[...o.values()];else if("length"===s&&a(c))o.forEach(((t,e)=>{("length"===e||e>=n)&&h.push(t)}));else switch(void 0!==s&&h.push(o.get(s)),e){case"add":a(c)?g(s)&&h.push(o.get("length")):(h.push(o.get(Y)),l(c)&&h.push(o.get(J)));break;case"delete":a(c)||(h.push(o.get(Y)),l(c)&&h.push(o.get(J)));break;case"set":l(c)&&h.push(o.get(Y))}if(1===h.length)h[0]&&rt(h[0]);else{const t=[];for(const e of h)e&&t.push(...e);rt(T(t))}const u=null===(r=t.root)||void 0===r?void 0:r.listeners;u&&u.length&&u.forEach((t=>t()))}function rt(t){const e=a(t)?t:[...t];for(const s of e)s.view&&ot(s);for(const s of e)s.view||ot(s)}function ot(t){(t!==z||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class ct{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this.dirty=!0,this.effect=new W(t,(()=>{this.dirty||(this.dirty=!0,function(t){(t=I(t)).dep&&rt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=I(this);return function(t){q&&z&&st((t=I(t)).dep||(t.dep=T()))}(t),!t.dirty&&t._cacheable||(t.dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const at=new Set(["__proto__"]),lt=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function ht(t){t.copy||(t.copy=O(t.base))}function ut(t,e){const s=t.__r_state;return(s?D(s):t)[e]}const ft=_t(),pt=dt();function dt(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=A(this),n=D(s);for(let e=0,r=this.length;e<r;e++)tt(s,0,e+"");const i=n[e](...t);return-1===i||!1===i?n[e](...t.map(I)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){Q();const s=D(A(this))[e].apply(this,t);return Z(),s}})),t}function _t(){return function(t,e,s){const n=D(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&s===Ct.get(t))return t;if(a(n)&&r(pt,e))return Reflect.get(pt,e,s);let i=Reflect.get(n,e,s);return(p(e)?lt.has(e):at.has(e))?i:(tt(t,0,e),r(n,e)?(t.disposed||!d(i)||(C(i)||(ht(t),i=t.copy[e]=At(i,t)),et(i)),i):i)}}function yt(){return function(t,e,s,n){const i=D(t),o=ut(i,e),c=a(i)&&g(e)?Number(e)<i.length:r(i,e);if(!t.modified){const n=null==o?void 0:o.__r_state;if(n&&n.base===s)return t.copy[e]=s,!0;if(b(s,o)&&(void 0!==s||r(t.base,e)))return!0;ht(t),$(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===A(n)&&(c?b(s,o)||it(t,"set",e,s):it(t,"add",e,s),function(t){let e=t;for(;e;)nt(e),e=e.parent}(t))),!0}}const gt={get:ft,set:yt(),deleteProperty:function(t,e){const s=r(D(t),e);if((void 0!==ut(t.base,e)||e in t.base)&&(ht(t),$(t)),t.copy){const n=delete t.copy[e];return n&&s&&it(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=D(t),n=Reflect.has(s,e);return p(e)&<.has(e)||tt(t,0,e),n},ownKeys:function(t){const e=D(t);return tt(t,0,a(e)?"length":Y),Reflect.ownKeys(e)},setPrototypeOf:function(t,e){const s=Reflect.setPrototypeOf(t.base,e);return s&&t.copy&&Reflect.setPrototypeOf(t.copy,e),s},getOwnPropertyDescriptor:function(t,e){const s=D(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!a(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function bt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=At(e,t);et(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function wt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):bt(t)}function vt(t){const e=this.__r_state,s=D(e);if(tt(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(C(n)||(wt(e),n=At(n,e),e.copy.set(t,n)),et(n),n):n}function mt(t,e){const s=this.__r_state,n=D(s),i=n.has(t),r=n.get(t),o=()=>{wt(s),$(s),s.copy.set(t,e)};return i?b(e,r)||(o(),it(s,"set",t,e)):(o(),it(s,"add",t,e)),this}function St(t){const e=this.__r_state;return D(e).has(t)||(bt(e),$(e),e.copy.add(t),it(e,"add",t,t)),this}function Ot(t){const e=this.__r_state;return tt(e,0,t),e.copy?!!e.copy.has(t)||2===e.type&&(e.drafts.has(t)&&e.drafts.has(e.drafts.get(t))):e.base.has(t)}function xt(t){const e=this.__r_state,s=D(e),n=s.has(t);"get"in s&&s.get(t),wt(e),$(e);let i=e.copy.delete(t);return 2!==e.type||i||(i=!!e.drafts.has(t)&&e.drafts.delete(e.drafts.get(t))),n&&it(e,"delete",t,void 0),i}function jt(){const t=this.__r_state,e=D(t),s=0!==e.size,n=e.clear();return s&&(wt(t),$(t),t.copy.clear(),it(t,"clear",void 0,void 0)),n}function Et(){const t={get:vt,get size(){return tt(t=this.__r_state,0,Y),D(t).size;var t},has:Ot,add:St,set:mt,delete:xt,clear:jt,keys(){const t=this.__r_state;return 1===t.type?function(t){return tt(t,0,J),D(t).keys()}(t):function(t){return tt(t,0,Y),bt(t),t.copy.values()}(t)},forEach(t,e){const s=this.__r_state;1===s.type?function(t,e,s,n){tt(e,0,Y),D(e).forEach(((i,r)=>s.call(n,e.proxy.get(r),r,t)))}(this,s,t,e):function(t,e,s,n){const i=e.proxy.values();let r=i.next();for(;!r.done;)s.call(n,r.value,r.value,t),r=i.next()}(this,s,t,e)}};return["values","entries",Symbol.iterator].forEach((e=>{t[e]=function(t){return function(...e){const s=this.__r_state,n=1===s.type,i="entries"===t||t===Symbol.iterator&&n;if(n){const t=s.proxy.keys();return{next(){const{value:e,done:n}=t.next(),r=s.proxy.get(e);return n?{value:r,done:n}:{value:i?[e,r]:r,done:n}},[Symbol.iterator](){return this}}}return tt(s,0,Y),bt(s),s.copy[t](...e)}}(e)})),[t]}const[Pt]=Et();function Mt(){const t=Pt;return(e,s,n)=>{if("__r_isReactive"===s)return!0;if("__r_state"===s)return e;const i=D(e);return Reflect.get(r(t,s)&&s in i?t:i,s,n)}}const Rt={get:Mt()};const Ct=new WeakMap;let It=0;function At(t,e){const s=R(t);if(0===s)return t;if(t.__r_skip||!Object.isExtensible(t))return t;if(t.__r_state)return t;let n={type:0,id:It++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=gt;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=Rt):4===s?(n.type=1,i=new Map,r=Rt):2===s&&(i=[]),i!==n&&Object.keys(n).forEach((t=>{Object.defineProperty(i,t,{configurable:!0,enumerable:!0,writable:!0,value:n[t]})}));const o=new Proxy(i,r);return i.proxy=o,e?(i.root=e.root,e.children.push(i)):i.root=i,Ct.set(i,o),i.proxy}function Dt(t,e){const s=function(t,{copies:e,proxies:s}){const n=t=>{switch(R(t)){case 1:case 2:return u;case 4:case 3:return h;default:throw new Error("Unpected Error. Please file an issue on https://github.com/dourajs/doura")}},i=t=>{if(!d(t))return t;if(C(t)){let i=s.get(t);if(!i){const r=e.get(t.__r_state);s.set(t,i=new Proxy(r,n(r)))}return i}return t},o=t=>function(...e){const s=this.__r_raw,n="entries"===t||t===Symbol.iterator&&l(this),r=s[t](...e);return{next(){const{value:t,done:e}=r.next();return e?{value:i(t),done:e}:{value:n?[t[0],i(t[1])]:i(t),done:e}},[Symbol.iterator](){return this}}},c=t=>function(...e){return this.__r_raw[t](...e)},a={get size(){return this.__r_raw.size},get(t){const e=this.__r_raw;if(e.has(t))return i(e.get(t))},forEach(t,e){const s=this.__r_raw;return s.forEach(((n,r)=>(n=i(n),t.call(e,n,l(s)?r:n,s))))}};["has","add","set","delete","clear"].forEach((t=>{a[t]=c(t)})),["keys","values","entries",Symbol.iterator].forEach((t=>{a[t]=o(t)}));const h={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(r(a,e)&&e in t?a:t,e,s)},u={get:(t,e,s)=>i(Reflect.get(t,e,s))};return n(t)}(t,e);if(C(t)){return new Proxy(e.copies.get(t.__r_state),s)}return new Proxy(O(t),s)}function $t(t,e){if(!d(t))return t;const s=function(t){const e=new Map,s={copies:e,proxies:new Map},n=[t.__r_state];for(;n.length;){const t=n.pop();let s;t.modified?(s=O(t.copy),kt(t,s)):s=t.base,e.set(t,s);for(const e of t.children)n.push(e)}return s}(e);return Dt(t,s)}function kt(t,e){t.modified=!1,t.base=e}function Lt(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=s.getSnapshot;return n.destory=function(){s.effect.stop();const e=t.effectScope.effects.indexOf(s.effect);e>=0&&t.effectScope.effects.splice(e,1)},n}const Tt=n({$name:t=>t.name,$rawState:t=>t.getState(),$state:t=>t.stateValue,$actions:t=>t.actions,$views:t=>t.views,$patch:t=>t.patch,$onAction:t=>t.onAction,$subscribe:t=>t.subscribe,$isolate:t=>t.isolate,$getApi:t=>t.getApi,$createView:t=>Lt.bind(null,t)}),Nt=t=>({_:e},s)=>{const{actions:n,views:i,accessCache:o,ctx:c}=e;let a;if(a=t?e.getState():e.stateValue,"$"!==s[0]){const t=o[s];if(void 0!==t)switch(t){case 0:return a[s];case 2:return i[s];case 1:return n[s];case 3:return c[s]}else{if(r(a,s))return o[s]=0,a[s];if(r(c,s))return o[s]=3,c[s]}}const l=Tt[s];return l?l(e):r(c,s)?(o[s]=3,c[s]):void((t=>"_"===t||"$"===t)(s[0])&&r(a,s)&&M(`Property ${JSON.stringify(s)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},Vt=({_:t},e,s)=>{const{ctx:n,actions:i,views:o,accessContext:c,stateRef:{value:a}}=t;return r(a,e)?1!==c&&(a[e]=s,!0):!r(i,e)&&(!r(o,e)&&("$state"===e?"bigint"!=typeof s&&"symbol"!=typeof s&&(t.replace(s),!0):("$"!==e[0]||!r(Tt,e))&&(n[e]=s,!0)))},Ht={get:Nt(!1),set:Vt},Ut={get:Nt(!0),set:Vt};function Xt(t,e,...s){console.error(t)}let Kt=!1,zt=!1;const Yt=[];let Jt=0;const Ft=[];let Wt=null,Bt=0;const qt=[];let Gt=null,Qt=0;const Zt=Promise.resolve();let te=null,ee=null;function se(t){const e=te||Zt;return t?e.then(this?t.bind(this):t):e}function ne(t){Yt.length&&Yt.includes(t,Kt&&t.allowRecurse?Jt+1:Jt)||t===ee||(null==t.id?Yt.push(t):Yt.splice(function(t){let e=Jt+1,s=Yt.length;for(;e<s;){const n=e+s>>>1;re(Yt[n])<t?e=n+1:s=n}return e}(t.id),0,t),Kt||zt||(zt=!0,te=Zt.then(oe)))}function ie(t,e=null){if(Ft.length){for(ee=e,Wt=[...new Set(Ft)],Ft.length=0,Bt=0;Bt<Wt.length;Bt++)Wt[Bt]();Wt=null,Bt=0,ee=null,ie(t,e)}}const re=t=>null==t.id?1/0:t.id;function oe(t){zt=!1,Kt=!0,ie(t),Yt.sort(((t,e)=>re(t)-re(e)));try{for(Jt=0;Jt<Yt.length;Jt++){const t=Yt[Jt];if(t&&!1!==t.active){0;try{t()}catch(e){Xt(e)}}}}finally{Jt=0,Yt.length=0,function(t){if(ie(),qt.length){const t=[...new Set(qt)];if(qt.length=0,Gt)return void Gt.push(...t);for(Gt=t,Gt.sort(((t,e)=>re(t)-re(e))),Qt=0;Qt<Gt.length;Qt++)Gt[Qt]();Gt=null,Qt=0}}(),Kt=!1,te=null,(Yt.length||Ft.length||qt.length)&&oe(t)}}var ce;function ae(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{r(t,s)&&y(e[s])?ae(t[s],e[s]):t[s]=e[s]}))}function le(t){t.dirty=!0}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(ce||(ce={}));class he{constructor(e,{name:s,initState:n}){this.viewInstances=[],this._actionDepth=0,this._api=null,this._actionListeners=new Set,this._subscribers=new Set,this._depListenersHandlers=[],this._watchStateChange=!0,this._destroyed=!1,this.patch=this.patch.bind(this),this.onAction=this.onAction.bind(this),this.subscribe=this.subscribe.bind(this),this.isolate=this.isolate.bind(this),this.getApi=this.getApi.bind(this),this.options=e,this.name=s||"",this._isDispatching=!1,this._initState=n||e.state,this.stateRef=At({value:this._initState});const i=this._update.bind(this);var r;this._draftListenerHandler=function(e,s){const n=e.__r_state;return n.disposed?t:(n.listeners.push(s),()=>{const t=n.listeners.indexOf(s);t>=0&&n.listeners.splice(t,1)})}(this.stateRef,(()=>{this._watchStateChange&&ne(i)})),this._setState(this._initState),this.actions=Object.create(null),this.views=Object.create(null),this.accessContext=0,this.ctx={},w(this.ctx,"_",this),this.accessCache=Object.create(null),this.proxy=new Proxy(this.ctx,Ht),this.publicInst=new Proxy(this.ctx,Ut),this.effectScope=new L(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,ae(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:ce.PATCH,args:{patch:t}}))}replace(t){if(d(t)){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const t of this.viewInstances)t.effect.scheduler();this.dispatch({type:ce.REPLACE,payload:t})}else M(`replace argument should be an object, but receive a ${Object.prototype.toString.call(t)}`)}getState(){return this._currentState}getApi(){if(null===this._api){const t=this._api=Object.assign(Object.assign({},this._currentState),this.views);for(const e of Object.keys(this.actions))w(t,e,this.actions[e])}return this._api}onAction(t){return this._actionListeners.add(t),()=>{this._actionListeners.delete(t)}}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}isolate(t){Q();const e=t(this.stateValue);return Z(),e}depend(t){this._depListenersHandlers.push(t.subscribe((t=>{this._triggerListener(Object.assign(Object.assign({},t),{model:this.proxy}))})))}createView(t){let e;return this.effectScope.run((()=>{e=function(t,e={}){return new ct(t,e)}((()=>{const n=this.accessContext;this.accessContext=1;const i=e.__externalArgs;try{return t.call(this.proxy,this.proxy,...i||s)}finally{this.accessContext=n}}))})),e.getSnapshot=()=>{const t=e.value;return e.mightChange?(e.mightChange=!1,e.__snapshot=$t(t,this.stateRef.value)):e.__pre!==t&&(e.__snapshot=$t(t,this.stateRef.value)),e.__pre=t,e.__snapshot},this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case ce.MODIFY:case ce.PATCH:return $t(this.stateRef.value,this.stateRef.value);case ce.REPLACE:return e.payload;default:return t}}dispatch(t){if(void 0===t.type)return t;if(this._isDispatching)return t;let e;try{this._isDispatching=!0,e=this.reducer(this._currentState,t)}finally{this._isDispatching=!1}return e!==this._currentState&&(this._setState(e),this._triggerListener(Object.assign({type:t.type,model:this.proxy,target:this.proxy},t.args))),t}destroy(){this._destroyed=!0,this._api=null,this._currentState=null,this.stateRef={value:null},this._subscribers.clear(),this.effectScope.stop();for(const t of this._depListenersHandlers)t();this._draftListenerHandler()}_setState(t){this._api=null,this._currentState=t,this.stateValue=this.stateRef.value}_update(){!this._destroyed&&function(t){return t.__r_state.modified}(this.stateRef.value)&&this.dispatch({type:ce.MODIFY})}_triggerListener(t){for(const e of this._subscribers)e(t)}_initActions(){const t=this.options.actions;if(t)for(const e of Object.keys(t)){this.accessCache[e]=1;const s=t[e];Object.defineProperty(this.actions,e,{configurable:!0,enumerable:!0,writable:!1,value:(...t)=>{if(1===this.accessContext)return;let n;this._actionDepth++;try{for(const s of this._actionListeners)s({name:e,args:t});n=s.call(this.proxy,...t)}finally{0==--this._actionDepth&&this._update()}return n}})}}_initViews(){const t=this.options.views;if(t)for(const e of Object.keys(t)){this.accessCache[e]=2;const s=t[e],n=s.length>1,i=this.createView(s),r=i;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get:n?()=>(...t)=>{const e=r.__externalArgs;if(e){if(e.length!==t.length)le(i);else for(let s=0;s<e.length;s++)if(e[s]!==t[s]){le(i);break}}else le(i);return r.__externalArgs=t,i.getSnapshot()}:i.getSnapshot,set:()=>!1})}}}function ue(t,e={}){return new he(t,e)}let fe=null;function pe(t){fe=t}class de{constructor(t=e,s=[]){this._subscribers=new Set,this._initialState=t,this._models=(()=>{const t=new Map,e={get:e=>t.get(e),set(e,s){t.set(e,s)},each(e){for(const[s,n]of t.entries())e(n,s)},clear(){e.each((t=>t.destroy())),t.clear()}};return e})();const n=()=>{for(const t of this._subscribers)t()};this._onModelChange=()=>{ne(n)},this._hooks=s.map((([t,e])=>t(e))),this._hooks.map((e=>{var s;return null===(s=e.onInit)||void 0===s?void 0:s.call(e,{initialState:t},{doura:this})}))}getModel(t,e){return this.getModelInstance({name:t,model:e}).publicInst}getDetachedModel(t){return this.getModelInstance({model:t}).publicInst}getModelInstance({name:t,model:e}){const s=t&&this._models.get(t);if(s)return s;let n;if("function"==typeof e){const s=fe,i=this._createModelProxy();try{pe({manager:this,model:i}),n=this._initModel({name:t,model:e()})}finally{pe(s)}i.setModel(n)}else{if("object"!=typeof e)throw new Error("invalid model");n=this._initModel({name:t,model:e})}return n}getState(){const t={};return this._models.each(((e,s)=>{t[s]=e.getState()})),t}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}destroy(){this._hooks.map((t=>{var e;return null===(e=t.onDestroy)||void 0===e?void 0:e.call(t)})),this._models.clear(),this._subscribers.clear(),this._initialState=e}_createModelProxy(){const t=[];return{addChild(e){t.push(e)},setModel(e){for(const s of t)e.depend(s);t.length=0}}}_initModel({name:t,model:e}){if(!t)return ue(e);this._hooks.map((s=>{var n;return null===(n=s.onModel)||void 0===n?void 0:n.call(s,t,e,{doura:this})}));const s=ue(e,{name:t,initState:this._getInitialState(t)});return s.subscribe(this._onModelChange),this._models.set(t,s),this._hooks.map((t=>{var e;null===(e=t.onModelInstance)||void 0===e||e.call(t,s.publicInst,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}function _e(t,e){if(!fe)throw new Error("Invalid use() call. use() can only be called inside of the body of a function model. This could happen for one of the following reasons:\n1. You might be breaking the Rules of Doura\n2. You might have more than one copy of Doura in the same app");const{manager:s,model:n}=fe;let i,r;return"string"==typeof t?(r=t,i=s.getModelInstance({name:r,model:e})):i=s.getModelInstance({model:e=t}),n.addChild(i),i.proxy}function ye({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new de(t,e)}({initialState:t,plugins:e})}var ge=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit(n,{doura:i}){if(window.__doura=i,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(i.getState());const n=t=>{switch(t.type){case"ACTION":return void v("string"==typeof t.payload,"Unsupported action format");case"DISPATCH":switch(t.payload.type){case"RESET":case"COMMIT":return s.init(i.getState());case"ROLLBACK":try{const e=JSON.parse(t.state);return s.init(e)}catch(e){console.warn("[Doura Devtool] Could not parse the received json.")}return s.init(i.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(n))}},onModelInstance(t,{doura:e}){t.$subscribe((t=>{var{type:n,model:i}=t,r=function(t,e){var s={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(s[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(t);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(t,n[i])&&(s[n[i]]=t[n[i]])}return s}(t,["type","model"]);const o=e.getState();delete o._,s.send(Object.assign({type:`${i.$name||"anonymous"}@${n}`},r),o)}))},onDestroy(){window.__REDUX_DEVTOOLS_EXTENSION__.disconnect(s);for(const t of e.values())t();e.clear()}}};export{t as NOOP,n as assign,w as def,P as defineModel,ge as devtool,ye as doura,At as draft,j as each,s as emptyArray,e as emptyObject,r as hasOwn,v as invariant,b as is,a as isArray,u as isFunction,g as isIntegerKey,l as isMap,d as isObject,y as isPlainObject,h as isSet,f as isString,p as isSymbol,se as nextTick,o as objectToString,x as ownKeys,E as set,O as shallowCopy,$t as snapshot,_ as toRawType,c as toTypeString,_e as use}; |
@@ -1,1 +0,1 @@ | ||
var Doura=function(t){"use strict";const e=()=>{},s=Object.create(null),n=[],i=Object.assign,r=Object.prototype.hasOwnProperty,o=(t,e)=>r.call(t,e),c=Object.prototype.toString,a=t=>c.call(t),l=Array.isArray,h=t=>"[object Map]"===a(t),u=t=>"[object Set]"===a(t),f=t=>"string"==typeof t,p=t=>"symbol"==typeof t,d=t=>null!==t&&"object"==typeof t,_=t=>a(t).slice(8,-1),y=t=>"[object Object]"===a(t),g=t=>f(t)&&"NaN"!==t&&"-"!==t[0]&&""+parseInt(t,10)===t,b=(t,e)=>Object.is(t,e),w=(t,e,s)=>{Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value:s})};function v(t,e){if(!t)throw new Error(`[Doura] ${e||""}`)}const S=Array.prototype.slice;function m(t){if(h(t))return new Map(t);if(u(t))return new Set(t);if(Array.isArray(t))return S.call(t);const e=Object.getOwnPropertyDescriptors(t),s=Reflect.ownKeys(e);for(let n=0;n<s.length;n++){const i=s[n],r=e[i];!1===r.writable&&(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(e[i]={configurable:!0,writable:!0,enumerable:r.enumerable,value:t[i]})}return Object.create(Object.getPrototypeOf(t),e)}const O="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function x(t,...e){console.warn(`[Doura warn] ${t}`,...e)}function j(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function E(t){return!!t&&!!t.__r_state}function P(t){const e=M(t);return e?P(e.base):t}function M(t){return t&&t.__r_state}function R(t){return t.copy||t.base}function C(t){t.modified||(t.modified=!0,t.parent&&C(t.parent))}let A;class I{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&A&&(this.parent=A,this.index=(A.scopes||(A.scopes=[])).push(this)-1)}run(t){if(this.active){const e=A;try{return A=this,t()}finally{A=e}}}on(){A=this}off(){A=this.parent}stop(t){if(this.active){let e,s;for(e=0,s=this.effects.length;e<s;e++)this.effects[e].stop();for(e=0,s=this.cleanups.length;e<s;e++)this.cleanups[e]();if(this.scopes)for(e=0,s=this.scopes.length;e<s;e++)this.scopes[e].stop(!0);if(this.parent&&!t){const t=this.parent.scopes.pop();t&&t!==this&&(this.parent.scopes[this.index]=t,t.index=this.index)}this.active=!1}}}const D=t=>{const e=new Set(t);return e.w=0,e.n=0,e},$=t=>(t.w&V)>0,T=t=>(t.n&V)>0,k=new WeakMap,L=new WeakMap;let N=0,V=1;let H;const K=Symbol(""),U=Symbol("");let X=0;class z{constructor(t,e=null,s){this.fn=t,this.scheduler=e,this.id=X++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=A){e&&e.active&&e.effects.push(t)}(this,s)}run(){if(!this.active)return this.fn();let t=H;const e=F;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=H,H=this,F=!0,V=1<<++N,N<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=V})(this):Y(this),this.fn()}finally{N<=30&&(t=>{const{deps:e}=t;if(e.length){let s=0;for(let n=0;n<e.length;n++){const i=e[n];$(i)&&!T(i)?i.delete(t):e[s++]=i,i.w&=~V,i.n&=~V}e.length=s}})(this),V=1<<--N,H=this.parent,F=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){H===this?this.deferStop=!0:this.active&&(Y(this),this.onStop&&this.onStop(),this.active=!1)}}function Y(t){const{deps:e}=t;if(e.length){for(let s=0;s<e.length;s++)e[s].delete(t);e.length=0}}let F=!0;const J=[];function W(){J.push(F),F=!1}function B(){const t=J.pop();F=void 0===t||t}function q(t,e,s){if(F&&H){let e=k.get(t);e||k.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=D()),Q(n)}}function G(t){if(F&&H){const e=t.__r_state;if(!e)return;let s=L.get(e);s||L.set(e,s=D()),Q(s)}}function Q(t){let e=!1;N<=30?T(t)||(t.n|=V,e=!$(t)):e=!t.has(H),e&&(t.add(H),H.deps.push(t))}function Z(t){const e=L.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function tt(t,e,s,n,i){var r;const o=k.get(t),c=t.base;let a=[];if(o)if("clear"===e)a=[...o.values()];else if("length"===s&&l(c))o.forEach(((t,e)=>{("length"===e||e>=n)&&a.push(t)}));else switch(void 0!==s&&a.push(o.get(s)),e){case"add":l(c)?g(s)&&a.push(o.get("length")):(a.push(o.get(K)),h(c)&&a.push(o.get(U)));break;case"delete":l(c)||(a.push(o.get(K)),h(c)&&a.push(o.get(U)));break;case"set":h(c)&&a.push(o.get(K))}if(1===a.length)a[0]&&et(a[0]);else{const t=[];for(const e of a)e&&t.push(...e);et(D(t))}const u=null===(r=t.root)||void 0===r?void 0:r.listeners;u&&u.length&&u.forEach((t=>t()))}function et(t){const e=l(t)?t:[...t];for(const s of e)s.view&&st(s);for(const s of e)s.view||st(s)}function st(t){(t!==H||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class nt{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this.dirty=!0,this.effect=new z(t,(()=>{this.dirty||(this.dirty=!0,function(t){(t=P(t)).dep&&et(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=P(this);return function(t){F&&H&&Q((t=P(t)).dep||(t.dep=D()))}(t),!t.dirty&&t._cacheable||(t.dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const it=new Set(["__proto__"]),rt=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function ot(t){t.copy||(t.copy=m(t.base))}function ct(t,e){const s=t.__r_state;return(s?R(s):t)[e]}const at=ut(),lt=ht();function ht(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=M(this),n=R(s);for(let e=0,r=this.length;e<r;e++)q(s,0,e+"");const i=n[e](...t);return-1===i||!1===i?n[e](...t.map(P)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){W();const s=R(M(this))[e].apply(this,t);return B(),s}})),t}function ut(){return function(t,e,s){const n=R(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&s===Et.get(t))return t;if(l(n)&&o(lt,e))return Reflect.get(lt,e,s);let i=Reflect.get(n,e,s);return(p(e)?rt.has(e):it.has(e))?i:(q(t,0,e),o(n,e)?(t.disposed||!d(i)||(E(i)||(ot(t),i=t.copy[e]=Mt(i,t)),G(i)),i):i)}}function ft(){return function(t,e,s,n){const i=R(t),r=ct(i,e),c=l(i)&&g(e)?Number(e)<i.length:o(i,e);if(!t.modified){const n=null==r?void 0:r.__r_state;if(n&&n.base===s)return t.copy[e]=s,!0;if(b(s,r)&&(void 0!==s||o(t.base,e)))return!0;ot(t),C(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===M(n)&&(c?b(s,r)||tt(t,"set",e,s):tt(t,"add",e,s),function(t){let e=t;for(;e;)Z(e),e=e.parent}(t))),!0}}const pt={get:at,set:ft(),deleteProperty:function(t,e){const s=o(R(t),e);if((void 0!==ct(t.base,e)||e in t.base)&&(ot(t),C(t)),t.copy){const n=delete t.copy[e];return n&&s&&tt(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=R(t),n=Reflect.has(s,e);return p(e)&&rt.has(e)||q(t,0,e),n},ownKeys:function(t){const e=R(t);return q(t,0,l(e)?"length":K),Reflect.ownKeys(e)},setPrototypeOf:function(t,e){const s=Reflect.setPrototypeOf(t.base,e);return s&&t.copy&&Reflect.setPrototypeOf(t.copy,e),s},getOwnPropertyDescriptor:function(t,e){const s=R(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!l(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function dt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=Mt(e,t);G(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function _t(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):dt(t)}function yt(t){const e=this.__r_state,s=R(e);if(q(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(E(n)||(_t(e),n=Mt(n,e),e.copy.set(t,n)),G(n),n):n}function gt(t,e){const s=this.__r_state,n=R(s),i=n.has(t),r=n.get(t),o=()=>{_t(s),C(s),s.copy.set(t,e)};return i?b(e,r)||(o(),tt(s,"set",t,e)):(o(),tt(s,"add",t,e)),this}function bt(t){const e=this.__r_state;return R(e).has(t)||(dt(e),C(e),e.copy.add(t),tt(e,"add",t,t)),this}function wt(t){const e=this.__r_state;return q(e,0,t),e.copy?!!e.copy.has(t)||2===e.type&&(e.drafts.has(t)&&e.drafts.has(e.drafts.get(t))):e.base.has(t)}function vt(t){const e=this.__r_state,s=R(e),n=s.has(t);"get"in s&&s.get(t),_t(e),C(e);let i=e.copy.delete(t);return 2!==e.type||i||(i=!!e.drafts.has(t)&&e.drafts.delete(e.drafts.get(t))),n&&tt(e,"delete",t,void 0),i}function St(){const t=this.__r_state,e=R(t),s=0!==e.size,n=e.clear();return s&&(_t(t),C(t),t.copy.clear(),tt(t,"clear",void 0,void 0)),n}function mt(){const t={get:yt,get size(){return q(t=this.__r_state,0,K),R(t).size;var t},has:wt,add:bt,set:gt,delete:vt,clear:St,keys(){const t=this.__r_state;return 1===t.type?function(t){return q(t,0,U),R(t).keys()}(t):function(t){return q(t,0,K),dt(t),t.copy.values()}(t)},forEach(t,e){const s=this.__r_state;1===s.type?function(t,e,s,n){q(e,0,K),R(e).forEach(((i,r)=>s.call(n,e.proxy.get(r),r,t)))}(this,s,t,e):function(t,e,s,n){const i=e.proxy.values();let r=i.next();for(;!r.done;)s.call(n,r.value,r.value,t),r=i.next()}(this,s,t,e)}};return["values","entries",Symbol.iterator].forEach((e=>{t[e]=function(t){return function(...e){const s=this.__r_state,n=1===s.type,i="entries"===t||t===Symbol.iterator&&n;if(n){const t=s.proxy.keys();return{next(){const{value:e,done:n}=t.next(),r=s.proxy.get(e);return n?{value:r,done:n}:{value:i?[e,r]:r,done:n}},[Symbol.iterator](){return this}}}return q(s,0,K),dt(s),s.copy[t](...e)}}(e)})),[t]}const[Ot]=mt();function xt(){const t=Ot;return(e,s,n)=>{if("__r_isReactive"===s)return!0;if("__r_state"===s)return e;const i=R(e);return Reflect.get(o(t,s)&&s in i?t:i,s,n)}}const jt={get:xt()};const Et=new WeakMap;let Pt=0;function Mt(t,e){const s=j(t);if(0===s)return t;if(t.__r_skip||!Object.isExtensible(t))return t;if(t.__r_state)return t;let n={type:0,id:Pt++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=pt;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=jt):4===s?(n.type=1,i=new Map,r=jt):2===s&&(i=[]),i!==n&&Object.keys(n).forEach((t=>{Object.defineProperty(i,t,{configurable:!0,enumerable:!0,writable:!0,value:n[t]})}));const o=new Proxy(i,r);return i.proxy=o,e?(i.root=e.root,e.children.push(i)):i.root=i,Et.set(i,o),i.proxy}function Rt(t,e){const s=function(t,{copies:e,proxies:s}){const n=t=>{switch(j(t)){case 1:case 2:return u;case 4:case 3:return l;default:throw new Error("Unpected Error. Please file an issue on https://github.com/dourajs/doura")}},i=t=>{if(!d(t))return t;if(E(t)){let i=s.get(t);if(!i){const r=e.get(t.__r_state);s.set(t,i=new Proxy(r,n(r)))}return i}return t},r=t=>function(...e){const s=this.__r_raw,n="entries"===t||t===Symbol.iterator&&h(this),r=s[t](...e);return{next(){const{value:t,done:e}=r.next();return e?{value:i(t),done:e}:{value:n?[t[0],i(t[1])]:i(t),done:e}},[Symbol.iterator](){return this}}},c=t=>function(...e){return this.__r_raw[t](...e)},a={get size(){return this.__r_raw.size},get(t){const e=this.__r_raw;if(e.has(t))return i(e.get(t))},forEach(t,e){const s=this.__r_raw;return s.forEach(((n,r)=>(n=i(n),t.call(e,n,h(s)?r:n,s))))}};["has","add","set","delete","clear"].forEach((t=>{a[t]=c(t)})),["keys","values","entries",Symbol.iterator].forEach((t=>{a[t]=r(t)}));const l={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(o(a,e)&&e in t?a:t,e,s)},u={get:(t,e,s)=>i(Reflect.get(t,e,s))};return n(t)}(t,e);if(E(t)){return new Proxy(e.copies.get(t.__r_state),s)}return new Proxy(m(t),s)}function Ct(t,e){if(!d(t))return t;const s=function(t){const e=new Map,s={copies:e,proxies:new Map},n=[t.__r_state];for(;n.length;){const t=n.pop();let s;t.modified?(s=m(t.copy),At(t,s)):s=t.base,e.set(t,s);for(const e of t.children)n.push(e)}return s}(e);return Rt(t,s)}function At(t,e){t.modified=!1,t.base=e}function It(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=s.getSnapshot;return n.destory=function(){s.effect.stop();const e=t.effectScope.effects.indexOf(s.effect);e>=0&&t.effectScope.effects.splice(e,1)},n}const Dt=i({$name:t=>t.name,$rawState:t=>t.getState(),$state:t=>t.stateValue,$actions:t=>t.actions,$views:t=>t.views,$patch:t=>t.patch,$onAction:t=>t.onAction,$subscribe:t=>t.subscribe,$isolate:t=>t.isolate,$getApi:t=>t.getApi,$createView:t=>It.bind(null,t)}),$t=t=>({_:e},s)=>{const{actions:n,views:i,accessCache:r,ctx:c}=e;let a;if(a=t?e.getState():e.stateValue,"$"!==s[0]){const t=r[s];if(void 0!==t)switch(t){case 0:return a[s];case 2:return i[s];case 1:return n[s];case 3:return c[s]}else{if(o(a,s))return r[s]=0,a[s];if(o(c,s))return r[s]=3,c[s]}}const l=Dt[s];return l?l(e):o(c,s)?(r[s]=3,c[s]):void((t=>"_"===t||"$"===t)(s[0])&&o(a,s)&&x(`Property ${JSON.stringify(s)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},Tt=({_:t},e,s)=>{const{ctx:n,actions:i,views:r,accessContext:c,stateRef:{value:a}}=t;return o(a,e)?1!==c&&(a[e]=s,!0):!o(i,e)&&(!o(r,e)&&("$state"===e?"bigint"!=typeof s&&"symbol"!=typeof s&&(t.replace(s),!0):("$"!==e[0]||!o(Dt,e))&&(n[e]=s,!0)))},kt={get:$t(!1),set:Tt},Lt={get:$t(!0),set:Tt};function Nt(t,e,...s){console.error(t)}let Vt=!1,Ht=!1;const Kt=[];let Ut=0;const Xt=[];let zt=null,Yt=0;const Ft=[];let Jt=null,Wt=0;const Bt=Promise.resolve();let qt=null,Gt=null;function Qt(t){Kt.length&&Kt.includes(t,Vt&&t.allowRecurse?Ut+1:Ut)||t===Gt||(null==t.id?Kt.push(t):Kt.splice(function(t){let e=Ut+1,s=Kt.length;for(;e<s;){const n=e+s>>>1;te(Kt[n])<t?e=n+1:s=n}return e}(t.id),0,t),Vt||Ht||(Ht=!0,qt=Bt.then(ee)))}function Zt(t,e=null){if(Xt.length){for(Gt=e,zt=[...new Set(Xt)],Xt.length=0,Yt=0;Yt<zt.length;Yt++)zt[Yt]();zt=null,Yt=0,Gt=null,Zt(t,e)}}const te=t=>null==t.id?1/0:t.id;function ee(t){Ht=!1,Vt=!0,Zt(t),Kt.sort(((t,e)=>te(t)-te(e)));try{for(Ut=0;Ut<Kt.length;Ut++){const t=Kt[Ut];if(t&&!1!==t.active){0;try{t()}catch(e){Nt(e)}}}}finally{Ut=0,Kt.length=0,function(t){if(Zt(),Ft.length){const t=[...new Set(Ft)];if(Ft.length=0,Jt)return void Jt.push(...t);for(Jt=t,Jt.sort(((t,e)=>te(t)-te(e))),Wt=0;Wt<Jt.length;Wt++)Jt[Wt]();Jt=null,Wt=0}}(),Vt=!1,qt=null,(Kt.length||Xt.length||Ft.length)&&ee(t)}}var se;function ne(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{o(t,s)&&y(e[s])?ne(t[s],e[s]):t[s]=e[s]}))}function ie(t){t.dirty=!0}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(se||(se={}));class re{constructor(t,{name:s,initState:n}){this.viewInstances=[],this._actionDepth=0,this._api=null,this._actionListeners=new Set,this._subscribers=new Set,this._depListenersHandlers=[],this._watchStateChange=!0,this._destroyed=!1,this.patch=this.patch.bind(this),this.onAction=this.onAction.bind(this),this.subscribe=this.subscribe.bind(this),this.isolate=this.isolate.bind(this),this.getApi=this.getApi.bind(this),this.options=t,this.name=s||"",this._isDispatching=!1,this._initState=n||t.state,this.stateRef=Mt({value:this._initState});const i=this._update.bind(this);var r;this._draftListenerHandler=function(t,s){const n=t.__r_state;return n.disposed?e:(n.listeners.push(s),()=>{const t=n.listeners.indexOf(s);t>=0&&n.listeners.splice(t,1)})}(this.stateRef,(()=>{this._watchStateChange&&Qt(i)})),this._setState(this._initState),this.actions=Object.create(null),this.views=Object.create(null),this.accessContext=0,this.ctx={},w(this.ctx,"_",this),this.accessCache=Object.create(null),this.proxy=new Proxy(this.ctx,kt),this.publicInst=new Proxy(this.ctx,Lt),this.effectScope=new I(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,ne(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:se.PATCH,args:{patch:t}}))}replace(t){if(d(t)){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const t of this.viewInstances)t.effect.scheduler();this.dispatch({type:se.REPLACE,payload:t})}else x(`replace argument should be an object, but receive a ${Object.prototype.toString.call(t)}`)}getState(){return this._currentState}getApi(){if(null===this._api){const t=this._api=Object.assign(Object.assign({},this._currentState),this.views);for(const e of Object.keys(this.actions))w(t,e,this.actions[e])}return this._api}onAction(t){return this._actionListeners.add(t),()=>{this._actionListeners.delete(t)}}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}isolate(t){W();const e=t(this.stateValue);return B(),e}depend(t){this._depListenersHandlers.push(t.subscribe((t=>{this._triggerListener(Object.assign(Object.assign({},t),{model:this.proxy}))})))}createView(t){let e;return this.effectScope.run((()=>{e=function(t,e={}){return new nt(t,e)}((()=>{const s=this.accessContext;this.accessContext=1;const i=e.__externalArgs;try{return t.call(this.proxy,this.proxy,...i||n)}finally{this.accessContext=s}}))})),e.getSnapshot=()=>{const t=e.value;return e.mightChange?(e.mightChange=!1,e.__snapshot=Ct(t,this.stateRef.value)):e.__pre!==t&&(e.__snapshot=Ct(t,this.stateRef.value)),e.__pre=t,e.__snapshot},this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case se.MODIFY:case se.PATCH:return Ct(this.stateRef.value,this.stateRef.value);case se.REPLACE:return e.payload;default:return t}}dispatch(t){if(void 0===t.type)return t;if(this._isDispatching)return t;let e;try{this._isDispatching=!0,e=this.reducer(this._currentState,t)}finally{this._isDispatching=!1}return e!==this._currentState&&(this._setState(e),this._triggerListener(Object.assign({type:t.type,model:this.proxy,target:this.proxy},t.args))),t}destroy(){this._destroyed=!0,this._api=null,this._currentState=null,this.stateRef={value:null},this._subscribers.clear(),this.effectScope.stop();for(const t of this._depListenersHandlers)t();this._draftListenerHandler()}_setState(t){this._api=null,this._currentState=t,this.stateValue=this.stateRef.value}_update(){!this._destroyed&&function(t){return t.__r_state.modified}(this.stateRef.value)&&this.dispatch({type:se.MODIFY})}_triggerListener(t){for(const e of this._subscribers)e(t)}_initActions(){const t=this.options.actions;if(t)for(const e of Object.keys(t)){this.accessCache[e]=1;const s=t[e];Object.defineProperty(this.actions,e,{configurable:!0,enumerable:!0,writable:!1,value:(...t)=>{if(1===this.accessContext)return;let n;this._actionDepth++;try{for(const s of this._actionListeners)s({name:e,args:t});n=s.call(this.proxy,...t)}finally{0==--this._actionDepth&&this._update()}return n}})}}_initViews(){const t=this.options.views;if(t)for(const e of Object.keys(t)){this.accessCache[e]=2;const s=t[e],n=s.length>1,i=this.createView(s),r=i;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get:n?()=>(...t)=>{const e=r.__externalArgs;if(e){if(e.length!==t.length)ie(i);else for(let s=0;s<e.length;s++)if(e[s]!==t[s]){ie(i);break}}else ie(i);return r.__externalArgs=t,i.getSnapshot()}:i.getSnapshot,set:()=>!1})}}}function oe(t,e={}){return new re(t,e)}let ce=null;function ae(t){ce=t}class le{constructor(t=s,e=[]){this._subscribers=new Set,this._initialState=t,this._models=(()=>{const t=new Map,e={get:e=>t.get(e),set(e,s){t.set(e,s)},each(e){for(const[s,n]of t.entries())e(n,s)},clear(){e.each((t=>t.destroy())),t.clear()}};return e})();const n=()=>{for(const t of this._subscribers)t()};this._onModelChange=()=>{Qt(n)},this._hooks=e.map((([t,e])=>t(e))),this._hooks.map((e=>{var s;return null===(s=e.onInit)||void 0===s?void 0:s.call(e,{initialState:t},{doura:this})}))}getModel(t,e){return this.getModelInstance({name:t,model:e}).publicInst}getDetachedModel(t){return this.getModelInstance({model:t}).publicInst}getModelInstance({name:t,model:e}){const s=t&&this._models.get(t);if(s)return s;let n;if("function"==typeof e){const s=ce,i=this._createModelProxy();try{ae({manager:this,model:i}),n=this._initModel({name:t,model:e()})}finally{ae(s)}i.setModel(n)}else{if("object"!=typeof e)throw new Error("invalid model");n=this._initModel({name:t,model:e})}return n}getState(){const t={};return this._models.each(((e,s)=>{t[s]=e.getState()})),t}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}destroy(){this._hooks.map((t=>{var e;return null===(e=t.onDestroy)||void 0===e?void 0:e.call(t)})),this._models.clear(),this._subscribers.clear(),this._initialState=s}_createModelProxy(){const t=[];return{addChild(e){t.push(e)},setModel(e){for(const s of t)e.depend(s);t.length=0}}}_initModel({name:t,model:e}){if(!t)return oe(e);this._hooks.map((s=>{var n;return null===(n=s.onModel)||void 0===n?void 0:n.call(s,t,e,{doura:this})}));const s=oe(e,{name:t,initState:this._getInitialState(t)});return s.subscribe(this._onModelChange),this._models.set(t,s),this._hooks.map((t=>{var e;null===(e=t.onModelInstance)||void 0===e||e.call(t,s.publicInst,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}var he=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit(n,{doura:i}){if(window.__doura=i,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(i.getState());const n=t=>{switch(t.type){case"ACTION":return void v("string"==typeof t.payload,"Unsupported action format");case"DISPATCH":switch(t.payload.type){case"RESET":case"COMMIT":return s.init(i.getState());case"ROLLBACK":try{const e=JSON.parse(t.state);return s.init(e)}catch(e){console.warn("[Doura Devtool] Could not parse the received json.")}return s.init(i.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(n))}},onModelInstance(t,{doura:e}){t.$subscribe((t=>{var{type:n,model:i}=t,r=function(t,e){var s={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(s[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(t);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(t,n[i])&&(s[n[i]]=t[n[i]])}return s}(t,["type","model"]);const o=e.getState();delete o._,s.send(Object.assign({type:`${i.$name||"anonymous"}@${n}`},r),o)}))},onDestroy(){window.__REDUX_DEVTOOLS_EXTENSION__.disconnect(s);for(const t of e.values())t();e.clear()}}};return t.NOOP=e,t.assign=i,t.def=w,t.defineModel=t=>t,t.devtool=he,t.doura=function({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new le(t,e)}({initialState:t,plugins:e})},t.draft=Mt,t.each=function(t,e,s=!1){y(t)?(s?Object.keys:O)(t).forEach((n=>{s&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach(((s,n)=>e(n,s,t)))},t.emptyArray=n,t.emptyObject=s,t.hasOwn=o,t.invariant=v,t.is=b,t.isArray=l,t.isFunction=t=>"function"==typeof t,t.isIntegerKey=g,t.isMap=h,t.isObject=d,t.isPlainObject=y,t.isSet=u,t.isString=f,t.isSymbol=p,t.nextTick=function(t){const e=qt||Bt;return t?e.then(this?t.bind(this):t):e},t.objectToString=c,t.ownKeys=O,t.set=function(t,e,s){h(t)?t.set(e,s):u(t)?(t.delete(e),t.add(s)):t[e]=s},t.shallowCopy=m,t.snapshot=Ct,t.toRawType=_,t.toTypeString=a,t.use=function(t,e){if(!ce)throw new Error("Invalid use() call. use() can only be called inside of the body of a function model. This could happen for one of the following reasons:\n1. You might be breaking the Rules of Doura\n2. You might have more than one copy of Doura in the same app");const{manager:s,model:n}=ce;let i,r;return"string"==typeof t?(r=t,i=s.getModelInstance({name:r,model:e})):i=s.getModelInstance({model:e=t}),n.addChild(i),i.proxy},t}({}); | ||
var Doura=function(t){"use strict";const e=()=>{},n=Object.create(null),s=[],i=Object.assign,r=Object.prototype.hasOwnProperty,o=(t,e)=>r.call(t,e),c=Object.prototype.toString,a=t=>c.call(t),l=Array.isArray,h=t=>"[object Map]"===a(t),u=t=>"[object Set]"===a(t),f=t=>"string"==typeof t,p=t=>"symbol"==typeof t,d=t=>null!==t&&"object"==typeof t,_=t=>a(t).slice(8,-1),y=t=>"[object Object]"===a(t),g=t=>f(t)&&"NaN"!==t&&"-"!==t[0]&&""+parseInt(t,10)===t,b=(t,e)=>Object.is(t,e),w=(t,e,n)=>{Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value:n})};function v(t,e){if(!t)throw new Error(`[Doura] ${e||""}`)}const m=Array.prototype.slice;const S=Object.prototype.propertyIsEnumerable;function O(t){return Array.isArray(t)?m.call(t):h(t)?new Map(t):u(t)?new Set(t):Object.getPrototypeOf(t)===Object.prototype?function(t){const e={},n=x(t);for(let s=0;s<n.length;s++){const i=n[s],r=t[i];S.call(t,i)?e[i]=r:Object.defineProperty(e,i,{configurable:!0,writable:!0,enumerable:!1,value:r})}return e}(t):function(t){const e=Object.getOwnPropertyDescriptors(t),n=x(e);for(let s=0;s<n.length;s++){const i=n[s],r=e[i];!1===r.writable&&(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(e[i]={configurable:!0,writable:!0,enumerable:r.enumerable,value:t[i]})}return Object.create(Object.getPrototypeOf(t),e)}(t)}const x="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function j(t,...e){console.warn(`[Doura warn] ${t}`,...e)}function E(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function P(t){return!!t&&!!t.__r_state}function M(t){const e=R(t);return e?M(e.base):t}function R(t){return t&&t.__r_state}function C(t){return t.copy||t.base}function I(t){t.modified||(t.modified=!0,t.parent&&I(t.parent))}let A;class D{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&A&&(this.parent=A,this.index=(A.scopes||(A.scopes=[])).push(this)-1)}run(t){if(this.active){const e=A;try{return A=this,t()}finally{A=e}}}on(){A=this}off(){A=this.parent}stop(t){if(this.active){let e,n;for(e=0,n=this.effects.length;e<n;e++)this.effects[e].stop();for(e=0,n=this.cleanups.length;e<n;e++)this.cleanups[e]();if(this.scopes)for(e=0,n=this.scopes.length;e<n;e++)this.scopes[e].stop(!0);if(this.parent&&!t){const t=this.parent.scopes.pop();t&&t!==this&&(this.parent.scopes[this.index]=t,t.index=this.index)}this.active=!1}}}const $=t=>{const e=new Set(t);return e.w=0,e.n=0,e},T=t=>(t.w&H)>0,k=t=>(t.n&H)>0,L=new WeakMap,N=new WeakMap;let V=0,H=1;let U;const X=Symbol(""),K=Symbol("");let z=0;class Y{constructor(t,e=null,n){this.fn=t,this.scheduler=e,this.id=z++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=A){e&&e.active&&e.effects.push(t)}(this,n)}run(){if(!this.active)return this.fn();let t=U;const e=J;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=U,U=this,J=!0,H=1<<++V,V<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=H})(this):F(this),this.fn()}finally{V<=30&&(t=>{const{deps:e}=t;if(e.length){let n=0;for(let s=0;s<e.length;s++){const i=e[s];T(i)&&!k(i)?i.delete(t):e[n++]=i,i.w&=~H,i.n&=~H}e.length=n}})(this),H=1<<--V,U=this.parent,J=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){U===this?this.deferStop=!0:this.active&&(F(this),this.onStop&&this.onStop(),this.active=!1)}}function F(t){const{deps:e}=t;if(e.length){for(let n=0;n<e.length;n++)e[n].delete(t);e.length=0}}let J=!0;const W=[];function B(){W.push(J),J=!1}function q(){const t=W.pop();J=void 0===t||t}function G(t,e,n){if(J&&U){let e=L.get(t);e||L.set(t,e=new Map);let s=e.get(n);s||e.set(n,s=$()),Z(s)}}function Q(t){if(J&&U){const e=t.__r_state;if(!e)return;let n=N.get(e);n||N.set(e,n=$()),Z(n)}}function Z(t){let e=!1;V<=30?k(t)||(t.n|=H,e=!T(t)):e=!t.has(U),e&&(t.add(U),U.deps.push(t))}function tt(t){const e=N.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function et(t,e,n,s,i){var r;const o=L.get(t),c=t.base;let a=[];if(o)if("clear"===e)a=[...o.values()];else if("length"===n&&l(c))o.forEach(((t,e)=>{("length"===e||e>=s)&&a.push(t)}));else switch(void 0!==n&&a.push(o.get(n)),e){case"add":l(c)?g(n)&&a.push(o.get("length")):(a.push(o.get(X)),h(c)&&a.push(o.get(K)));break;case"delete":l(c)||(a.push(o.get(X)),h(c)&&a.push(o.get(K)));break;case"set":h(c)&&a.push(o.get(X))}if(1===a.length)a[0]&&nt(a[0]);else{const t=[];for(const e of a)e&&t.push(...e);nt($(t))}const u=null===(r=t.root)||void 0===r?void 0:r.listeners;u&&u.length&&u.forEach((t=>t()))}function nt(t){const e=l(t)?t:[...t];for(const n of e)n.view&&st(n);for(const n of e)n.view||st(n)}function st(t){(t!==U||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class it{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this.dirty=!0,this.effect=new Y(t,(()=>{this.dirty||(this.dirty=!0,function(t){(t=M(t)).dep&&nt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=M(this);return function(t){J&&U&&Z((t=M(t)).dep||(t.dep=$()))}(t),!t.dirty&&t._cacheable||(t.dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const rt=new Set(["__proto__"]),ot=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function ct(t){t.copy||(t.copy=O(t.base))}function at(t,e){const n=t.__r_state;return(n?C(n):t)[e]}const lt=ft(),ht=ut();function ut(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const n=R(this),s=C(n);for(let e=0,r=this.length;e<r;e++)G(n,0,e+"");const i=s[e](...t);return-1===i||!1===i?s[e](...t.map(M)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){B();const n=C(R(this))[e].apply(this,t);return q(),n}})),t}function ft(){return function(t,e,n){const s=C(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&n===Pt.get(t))return t;if(l(s)&&o(ht,e))return Reflect.get(ht,e,n);let i=Reflect.get(s,e,n);return(p(e)?ot.has(e):rt.has(e))?i:(G(t,0,e),o(s,e)?(t.disposed||!d(i)||(P(i)||(ct(t),i=t.copy[e]=Rt(i,t)),Q(i)),i):i)}}function pt(){return function(t,e,n,s){const i=C(t),r=at(i,e),c=l(i)&&g(e)?Number(e)<i.length:o(i,e);if(!t.modified){const s=null==r?void 0:r.__r_state;if(s&&s.base===n)return t.copy[e]=n,!0;if(b(n,r)&&(void 0!==n||o(t.base,e)))return!0;ct(t),I(t)}return b(t.copy[e],n)&&(void 0!==n||e in t.copy)||(t.copy[e]=n,t===R(s)&&(c?b(n,r)||et(t,"set",e,n):et(t,"add",e,n),function(t){let e=t;for(;e;)tt(e),e=e.parent}(t))),!0}}const dt={get:lt,set:pt(),deleteProperty:function(t,e){const n=o(C(t),e);if((void 0!==at(t.base,e)||e in t.base)&&(ct(t),I(t)),t.copy){const s=delete t.copy[e];return s&&n&&et(t,"delete",e,void 0),s}return!0},has:function(t,e){const n=C(t),s=Reflect.has(n,e);return p(e)&&ot.has(e)||G(t,0,e),s},ownKeys:function(t){const e=C(t);return G(t,0,l(e)?"length":X),Reflect.ownKeys(e)},setPrototypeOf:function(t,e){const n=Reflect.setPrototypeOf(t.base,e);return n&&t.copy&&Reflect.setPrototypeOf(t.copy,e),n},getOwnPropertyDescriptor:function(t,e){const n=C(t),s=Reflect.getOwnPropertyDescriptor(n,e);return s?{writable:!0,configurable:!l(n)||"length"!==e,enumerable:s.enumerable,value:n[e]}:s}};function _t(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const n=Rt(e,t);Q(n),e!==n&&t.drafts.set(e,n),t.copy.add(n)}else t.copy.add(e)})))}function yt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):_t(t)}function gt(t){const e=this.__r_state,n=C(e);if(G(e,0,t),!n.has(t))return;let s=n.get(t);return d(s)?(P(s)||(yt(e),s=Rt(s,e),e.copy.set(t,s)),Q(s),s):s}function bt(t,e){const n=this.__r_state,s=C(n),i=s.has(t),r=s.get(t),o=()=>{yt(n),I(n),n.copy.set(t,e)};return i?b(e,r)||(o(),et(n,"set",t,e)):(o(),et(n,"add",t,e)),this}function wt(t){const e=this.__r_state;return C(e).has(t)||(_t(e),I(e),e.copy.add(t),et(e,"add",t,t)),this}function vt(t){const e=this.__r_state;return G(e,0,t),e.copy?!!e.copy.has(t)||2===e.type&&(e.drafts.has(t)&&e.drafts.has(e.drafts.get(t))):e.base.has(t)}function mt(t){const e=this.__r_state,n=C(e),s=n.has(t);"get"in n&&n.get(t),yt(e),I(e);let i=e.copy.delete(t);return 2!==e.type||i||(i=!!e.drafts.has(t)&&e.drafts.delete(e.drafts.get(t))),s&&et(e,"delete",t,void 0),i}function St(){const t=this.__r_state,e=C(t),n=0!==e.size,s=e.clear();return n&&(yt(t),I(t),t.copy.clear(),et(t,"clear",void 0,void 0)),s}function Ot(){const t={get:gt,get size(){return G(t=this.__r_state,0,X),C(t).size;var t},has:vt,add:wt,set:bt,delete:mt,clear:St,keys(){const t=this.__r_state;return 1===t.type?function(t){return G(t,0,K),C(t).keys()}(t):function(t){return G(t,0,X),_t(t),t.copy.values()}(t)},forEach(t,e){const n=this.__r_state;1===n.type?function(t,e,n,s){G(e,0,X),C(e).forEach(((i,r)=>n.call(s,e.proxy.get(r),r,t)))}(this,n,t,e):function(t,e,n,s){const i=e.proxy.values();let r=i.next();for(;!r.done;)n.call(s,r.value,r.value,t),r=i.next()}(this,n,t,e)}};return["values","entries",Symbol.iterator].forEach((e=>{t[e]=function(t){return function(...e){const n=this.__r_state,s=1===n.type,i="entries"===t||t===Symbol.iterator&&s;if(s){const t=n.proxy.keys();return{next(){const{value:e,done:s}=t.next(),r=n.proxy.get(e);return s?{value:r,done:s}:{value:i?[e,r]:r,done:s}},[Symbol.iterator](){return this}}}return G(n,0,X),_t(n),n.copy[t](...e)}}(e)})),[t]}const[xt]=Ot();function jt(){const t=xt;return(e,n,s)=>{if("__r_isReactive"===n)return!0;if("__r_state"===n)return e;const i=C(e);return Reflect.get(o(t,n)&&n in i?t:i,n,s)}}const Et={get:jt()};const Pt=new WeakMap;let Mt=0;function Rt(t,e){const n=E(t);if(0===n)return t;if(t.__r_skip||!Object.isExtensible(t))return t;if(t.__r_state)return t;let s={type:0,id:Mt++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=s,r=dt;3===n?(s.type=2,s.drafts=new Map,i=new Set,r=Et):4===n?(s.type=1,i=new Map,r=Et):2===n&&(i=[]),i!==s&&Object.keys(s).forEach((t=>{Object.defineProperty(i,t,{configurable:!0,enumerable:!0,writable:!0,value:s[t]})}));const o=new Proxy(i,r);return i.proxy=o,e?(i.root=e.root,e.children.push(i)):i.root=i,Pt.set(i,o),i.proxy}function Ct(t,e){const n=function(t,{copies:e,proxies:n}){const s=t=>{switch(E(t)){case 1:case 2:return u;case 4:case 3:return l;default:throw new Error("Unpected Error. Please file an issue on https://github.com/dourajs/doura")}},i=t=>{if(!d(t))return t;if(P(t)){let i=n.get(t);if(!i){const r=e.get(t.__r_state);n.set(t,i=new Proxy(r,s(r)))}return i}return t},r=t=>function(...e){const n=this.__r_raw,s="entries"===t||t===Symbol.iterator&&h(this),r=n[t](...e);return{next(){const{value:t,done:e}=r.next();return e?{value:i(t),done:e}:{value:s?[t[0],i(t[1])]:i(t),done:e}},[Symbol.iterator](){return this}}},c=t=>function(...e){return this.__r_raw[t](...e)},a={get size(){return this.__r_raw.size},get(t){const e=this.__r_raw;if(e.has(t))return i(e.get(t))},forEach(t,e){const n=this.__r_raw;return n.forEach(((s,r)=>(s=i(s),t.call(e,s,h(n)?r:s,n))))}};["has","add","set","delete","clear"].forEach((t=>{a[t]=c(t)})),["keys","values","entries",Symbol.iterator].forEach((t=>{a[t]=r(t)}));const l={get:(t,e,n)=>"__r_raw"===e?t:Reflect.get(o(a,e)&&e in t?a:t,e,n)},u={get:(t,e,n)=>i(Reflect.get(t,e,n))};return s(t)}(t,e);if(P(t)){return new Proxy(e.copies.get(t.__r_state),n)}return new Proxy(O(t),n)}function It(t,e){if(!d(t))return t;const n=function(t){const e=new Map,n={copies:e,proxies:new Map},s=[t.__r_state];for(;s.length;){const t=s.pop();let n;t.modified?(n=O(t.copy),At(t,n)):n=t.base,e.set(t,n);for(const e of t.children)s.push(e)}return n}(e);return Ct(t,n)}function At(t,e){t.modified=!1,t.base=e}function Dt(t,e){const n=t.createView((function(){return e(this,this.$actions)})),s=n.getSnapshot;return s.destory=function(){n.effect.stop();const e=t.effectScope.effects.indexOf(n.effect);e>=0&&t.effectScope.effects.splice(e,1)},s}const $t=i({$name:t=>t.name,$rawState:t=>t.getState(),$state:t=>t.stateValue,$actions:t=>t.actions,$views:t=>t.views,$patch:t=>t.patch,$onAction:t=>t.onAction,$subscribe:t=>t.subscribe,$isolate:t=>t.isolate,$getApi:t=>t.getApi,$createView:t=>Dt.bind(null,t)}),Tt=t=>({_:e},n)=>{const{actions:s,views:i,accessCache:r,ctx:c}=e;let a;if(a=t?e.getState():e.stateValue,"$"!==n[0]){const t=r[n];if(void 0!==t)switch(t){case 0:return a[n];case 2:return i[n];case 1:return s[n];case 3:return c[n]}else{if(o(a,n))return r[n]=0,a[n];if(o(c,n))return r[n]=3,c[n]}}const l=$t[n];return l?l(e):o(c,n)?(r[n]=3,c[n]):void((t=>"_"===t||"$"===t)(n[0])&&o(a,n)&&j(`Property ${JSON.stringify(n)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},kt=({_:t},e,n)=>{const{ctx:s,actions:i,views:r,accessContext:c,stateRef:{value:a}}=t;return o(a,e)?1!==c&&(a[e]=n,!0):!o(i,e)&&(!o(r,e)&&("$state"===e?"bigint"!=typeof n&&"symbol"!=typeof n&&(t.replace(n),!0):("$"!==e[0]||!o($t,e))&&(s[e]=n,!0)))},Lt={get:Tt(!1),set:kt},Nt={get:Tt(!0),set:kt};function Vt(t,e,...n){console.error(t)}let Ht=!1,Ut=!1;const Xt=[];let Kt=0;const zt=[];let Yt=null,Ft=0;const Jt=[];let Wt=null,Bt=0;const qt=Promise.resolve();let Gt=null,Qt=null;function Zt(t){Xt.length&&Xt.includes(t,Ht&&t.allowRecurse?Kt+1:Kt)||t===Qt||(null==t.id?Xt.push(t):Xt.splice(function(t){let e=Kt+1,n=Xt.length;for(;e<n;){const s=e+n>>>1;ee(Xt[s])<t?e=s+1:n=s}return e}(t.id),0,t),Ht||Ut||(Ut=!0,Gt=qt.then(ne)))}function te(t,e=null){if(zt.length){for(Qt=e,Yt=[...new Set(zt)],zt.length=0,Ft=0;Ft<Yt.length;Ft++)Yt[Ft]();Yt=null,Ft=0,Qt=null,te(t,e)}}const ee=t=>null==t.id?1/0:t.id;function ne(t){Ut=!1,Ht=!0,te(t),Xt.sort(((t,e)=>ee(t)-ee(e)));try{for(Kt=0;Kt<Xt.length;Kt++){const t=Xt[Kt];if(t&&!1!==t.active){0;try{t()}catch(e){Vt(e)}}}}finally{Kt=0,Xt.length=0,function(t){if(te(),Jt.length){const t=[...new Set(Jt)];if(Jt.length=0,Wt)return void Wt.push(...t);for(Wt=t,Wt.sort(((t,e)=>ee(t)-ee(e))),Bt=0;Bt<Wt.length;Bt++)Wt[Bt]();Wt=null,Bt=0}}(),Ht=!1,Gt=null,(Xt.length||zt.length||Jt.length)&&ne(t)}}var se;function ie(t,e){const n=Object.keys(e);n.length&&n.forEach((n=>{o(t,n)&&y(e[n])?ie(t[n],e[n]):t[n]=e[n]}))}function re(t){t.dirty=!0}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(se||(se={}));class oe{constructor(t,{name:n,initState:s}){this.viewInstances=[],this._actionDepth=0,this._api=null,this._actionListeners=new Set,this._subscribers=new Set,this._depListenersHandlers=[],this._watchStateChange=!0,this._destroyed=!1,this.patch=this.patch.bind(this),this.onAction=this.onAction.bind(this),this.subscribe=this.subscribe.bind(this),this.isolate=this.isolate.bind(this),this.getApi=this.getApi.bind(this),this.options=t,this.name=n||"",this._isDispatching=!1,this._initState=s||t.state,this.stateRef=Rt({value:this._initState});const i=this._update.bind(this);var r;this._draftListenerHandler=function(t,n){const s=t.__r_state;return s.disposed?e:(s.listeners.push(n),()=>{const t=s.listeners.indexOf(n);t>=0&&s.listeners.splice(t,1)})}(this.stateRef,(()=>{this._watchStateChange&&Zt(i)})),this._setState(this._initState),this.actions=Object.create(null),this.views=Object.create(null),this.accessContext=0,this.ctx={},w(this.ctx,"_",this),this.accessCache=Object.create(null),this.proxy=new Proxy(this.ctx,Lt),this.publicInst=new Proxy(this.ctx,Nt),this.effectScope=new D(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,ie(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:se.PATCH,args:{patch:t}}))}replace(t){if(d(t)){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const t of this.viewInstances)t.effect.scheduler();this.dispatch({type:se.REPLACE,payload:t})}else j(`replace argument should be an object, but receive a ${Object.prototype.toString.call(t)}`)}getState(){return this._currentState}getApi(){if(null===this._api){const t=this._api=Object.assign(Object.assign({},this._currentState),this.views);for(const e of Object.keys(this.actions))w(t,e,this.actions[e])}return this._api}onAction(t){return this._actionListeners.add(t),()=>{this._actionListeners.delete(t)}}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}isolate(t){B();const e=t(this.stateValue);return q(),e}depend(t){this._depListenersHandlers.push(t.subscribe((t=>{this._triggerListener(Object.assign(Object.assign({},t),{model:this.proxy}))})))}createView(t){let e;return this.effectScope.run((()=>{e=function(t,e={}){return new it(t,e)}((()=>{const n=this.accessContext;this.accessContext=1;const i=e.__externalArgs;try{return t.call(this.proxy,this.proxy,...i||s)}finally{this.accessContext=n}}))})),e.getSnapshot=()=>{const t=e.value;return e.mightChange?(e.mightChange=!1,e.__snapshot=It(t,this.stateRef.value)):e.__pre!==t&&(e.__snapshot=It(t,this.stateRef.value)),e.__pre=t,e.__snapshot},this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case se.MODIFY:case se.PATCH:return It(this.stateRef.value,this.stateRef.value);case se.REPLACE:return e.payload;default:return t}}dispatch(t){if(void 0===t.type)return t;if(this._isDispatching)return t;let e;try{this._isDispatching=!0,e=this.reducer(this._currentState,t)}finally{this._isDispatching=!1}return e!==this._currentState&&(this._setState(e),this._triggerListener(Object.assign({type:t.type,model:this.proxy,target:this.proxy},t.args))),t}destroy(){this._destroyed=!0,this._api=null,this._currentState=null,this.stateRef={value:null},this._subscribers.clear(),this.effectScope.stop();for(const t of this._depListenersHandlers)t();this._draftListenerHandler()}_setState(t){this._api=null,this._currentState=t,this.stateValue=this.stateRef.value}_update(){!this._destroyed&&function(t){return t.__r_state.modified}(this.stateRef.value)&&this.dispatch({type:se.MODIFY})}_triggerListener(t){for(const e of this._subscribers)e(t)}_initActions(){const t=this.options.actions;if(t)for(const e of Object.keys(t)){this.accessCache[e]=1;const n=t[e];Object.defineProperty(this.actions,e,{configurable:!0,enumerable:!0,writable:!1,value:(...t)=>{if(1===this.accessContext)return;let s;this._actionDepth++;try{for(const n of this._actionListeners)n({name:e,args:t});s=n.call(this.proxy,...t)}finally{0==--this._actionDepth&&this._update()}return s}})}}_initViews(){const t=this.options.views;if(t)for(const e of Object.keys(t)){this.accessCache[e]=2;const n=t[e],s=n.length>1,i=this.createView(n),r=i;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get:s?()=>(...t)=>{const e=r.__externalArgs;if(e){if(e.length!==t.length)re(i);else for(let n=0;n<e.length;n++)if(e[n]!==t[n]){re(i);break}}else re(i);return r.__externalArgs=t,i.getSnapshot()}:i.getSnapshot,set:()=>!1})}}}function ce(t,e={}){return new oe(t,e)}let ae=null;function le(t){ae=t}class he{constructor(t=n,e=[]){this._subscribers=new Set,this._initialState=t,this._models=(()=>{const t=new Map,e={get:e=>t.get(e),set(e,n){t.set(e,n)},each(e){for(const[n,s]of t.entries())e(s,n)},clear(){e.each((t=>t.destroy())),t.clear()}};return e})();const s=()=>{for(const t of this._subscribers)t()};this._onModelChange=()=>{Zt(s)},this._hooks=e.map((([t,e])=>t(e))),this._hooks.map((e=>{var n;return null===(n=e.onInit)||void 0===n?void 0:n.call(e,{initialState:t},{doura:this})}))}getModel(t,e){return this.getModelInstance({name:t,model:e}).publicInst}getDetachedModel(t){return this.getModelInstance({model:t}).publicInst}getModelInstance({name:t,model:e}){const n=t&&this._models.get(t);if(n)return n;let s;if("function"==typeof e){const n=ae,i=this._createModelProxy();try{le({manager:this,model:i}),s=this._initModel({name:t,model:e()})}finally{le(n)}i.setModel(s)}else{if("object"!=typeof e)throw new Error("invalid model");s=this._initModel({name:t,model:e})}return s}getState(){const t={};return this._models.each(((e,n)=>{t[n]=e.getState()})),t}subscribe(t){return this._subscribers.add(t),()=>{this._subscribers.delete(t)}}destroy(){this._hooks.map((t=>{var e;return null===(e=t.onDestroy)||void 0===e?void 0:e.call(t)})),this._models.clear(),this._subscribers.clear(),this._initialState=n}_createModelProxy(){const t=[];return{addChild(e){t.push(e)},setModel(e){for(const n of t)e.depend(n);t.length=0}}}_initModel({name:t,model:e}){if(!t)return ce(e);this._hooks.map((n=>{var s;return null===(s=n.onModel)||void 0===s?void 0:s.call(n,t,e,{doura:this})}));const n=ce(e,{name:t,initState:this._getInitialState(t)});return n.subscribe(this._onModelChange),this._models.set(t,n),this._hooks.map((t=>{var e;null===(e=t.onModelInstance)||void 0===e||e.call(t,n.publicInst,{doura:this})})),n}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}var ue=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let n;return{onInit(s,{doura:i}){if(window.__doura=i,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){n=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,n.init(i.getState());const s=t=>{switch(t.type){case"ACTION":return void v("string"==typeof t.payload,"Unsupported action format");case"DISPATCH":switch(t.payload.type){case"RESET":case"COMMIT":return n.init(i.getState());case"ROLLBACK":try{const e=JSON.parse(t.state);return n.init(e)}catch(e){console.warn("[Doura Devtool] Could not parse the received json.")}return n.init(i.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(n.subscribe(s))}},onModelInstance(t,{doura:e}){t.$subscribe((t=>{var{type:s,model:i}=t,r=function(t,e){var n={};for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.indexOf(s)<0&&(n[s]=t[s]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(s=Object.getOwnPropertySymbols(t);i<s.length;i++)e.indexOf(s[i])<0&&Object.prototype.propertyIsEnumerable.call(t,s[i])&&(n[s[i]]=t[s[i]])}return n}(t,["type","model"]);const o=e.getState();delete o._,n.send(Object.assign({type:`${i.$name||"anonymous"}@${s}`},r),o)}))},onDestroy(){window.__REDUX_DEVTOOLS_EXTENSION__.disconnect(n);for(const t of e.values())t();e.clear()}}};return t.NOOP=e,t.assign=i,t.def=w,t.defineModel=t=>t,t.devtool=ue,t.doura=function({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new he(t,e)}({initialState:t,plugins:e})},t.draft=Rt,t.each=function(t,e,n=!1){y(t)?(n?Object.keys:x)(t).forEach((s=>{n&&"symbol"==typeof s||e(s,t[s],t)})):t.forEach(((n,s)=>e(s,n,t)))},t.emptyArray=s,t.emptyObject=n,t.hasOwn=o,t.invariant=v,t.is=b,t.isArray=l,t.isFunction=t=>"function"==typeof t,t.isIntegerKey=g,t.isMap=h,t.isObject=d,t.isPlainObject=y,t.isSet=u,t.isString=f,t.isSymbol=p,t.nextTick=function(t){const e=Gt||qt;return t?e.then(this?t.bind(this):t):e},t.objectToString=c,t.ownKeys=x,t.set=function(t,e,n){h(t)?t.set(e,n):u(t)?(t.delete(e),t.add(n)):t[e]=n},t.shallowCopy=O,t.snapshot=It,t.toRawType=_,t.toTypeString=a,t.use=function(t,e){if(!ae)throw new Error("Invalid use() call. use() can only be called inside of the body of a function model. This could happen for one of the following reasons:\n1. You might be breaking the Rules of Doura\n2. You might have more than one copy of Doura in the same app");const{manager:n,model:s}=ae;let i,r;return"string"==typeof t?(r=t,i=n.getModelInstance({name:r,model:e})):i=n.getModelInstance({model:e=t}),s.addChild(i),i.proxy},t}({}); |
{ | ||
"name": "doura", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Reactive and intuitive state management", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
446061
11488