Comparing version 0.0.4 to 0.0.5
@@ -70,6 +70,2 @@ export declare type Action = ModifyAction | PatchAction | ReplaceAction; | ||
declare type EmptyObject = { | ||
[X: string | number | symbol]: never; | ||
}; | ||
export declare const emptyObject: any; | ||
@@ -112,3 +108,3 @@ | ||
export declare type ModelActions<Model> = Model extends ModelOptions<any, infer A, any, any> ? Actions<A> & EmptyObject : never; | ||
export declare type ModelActions<Model> = Model extends ModelOptions<any, infer A, any, any> ? Actions<A> : never; | ||
@@ -187,3 +183,3 @@ export declare type ModelAPI<IModel extends AnyModel> = ModelData<IModel> & ModelActions<IModel>; | ||
export declare type ModelViews<Model> = Model extends ModelOptions<any, any, infer V, any> ? Views<V> & EmptyObject : never; | ||
export declare type ModelViews<Model> = Model extends ModelOptions<any, any, infer V, any> ? Views<V> : never; | ||
@@ -190,0 +186,0 @@ declare interface ModifyAction extends ActionBase { |
@@ -1,1 +0,1 @@ | ||
const t=()=>{},e=Object.create(null),s=Object.assign,n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var s in e)e.hasOwnProperty(s)&&(t[s]=e[s])},i=Object.prototype.hasOwnProperty,r=(t,e)=>i.call(t,e),o=Object.prototype.toString,c=t=>o.call(t),a=Array.isArray,h=t=>"[object Map]"===c(t),u=t=>"[object Set]"===c(t),l=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 S=Array.prototype.slice;function O(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);let 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 m="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:m)(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){h(t)?t.set(e,s):u(t)?(t.delete(e),t.add(s)):t[e]=s}const E=t=>t;function P(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 R(t){const e=M(t);return e?R(e.base):t}function M(t){return t&&t.__r_state}function D(t){return t.copy||t.base}function A(t){t.modified||(t.modified=!0,t.parent&&A(t.parent))}let $;class I{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 k=t=>{const e=new Set(t);return e.w=0,e.n=0,e},L=t=>(t.w&H)>0,N=t=>(t.n&H)>0,T=new WeakMap,V=new WeakMap;let U=0,H=1;let X;const K=Symbol(""),z=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=X,e=Y;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=X,X=this,Y=!0,H=1<<++U,U<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=H})(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];L(i)&&!N(i)?i.delete(t):e[s++]=i,i.w&=~H,i.n&=~H}e.length=s}})(this),H=1<<--U,X=this.parent,Y=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){X===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 Y=!0;const B=[];function q(){B.push(Y),Y=!1}function G(){const t=B.pop();Y=void 0===t||t}function Q(t,e,s){if(Y&&X){let e=T.get(t);e||T.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=k()),tt(n)}}function Z(t){if(Y&&X){const e=t.__r_state;if(!e)return;let s=V.get(e);s||V.set(e,s=k()),tt(s)}}function tt(t){let e=!1;U<=30?N(t)||(t.n|=H,e=!L(t)):e=!t.has(X),e&&(t.add(X),X.deps.push(t))}function et(t){const e=V.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function st(t,e,s,n,i){var r;const o=T.get(t),c=t.base;let u=[];if(o)if("clear"===e)u=[...o.values()];else if("length"===s&&a(c))o.forEach(((t,e)=>{("length"===e||e>=n)&&u.push(t)}));else switch(void 0!==s&&u.push(o.get(s)),e){case"add":a(c)?g(s)&&u.push(o.get("length")):(u.push(o.get(K)),h(c)&&u.push(o.get(z)));break;case"delete":a(c)||(u.push(o.get(K)),h(c)&&u.push(o.get(z)));break;case"set":h(c)&&u.push(o.get(K))}if(1===u.length)u[0]&&nt(u[0]);else{const t=[];for(const e of u)e&&t.push(...e);nt(k(t))}const l=null===(r=t.root)||void 0===r?void 0:r.listeners;l&&l.length&&l.forEach((t=>t()))}function nt(t){const e=a(t)?t:[...t];for(const s of e)s.view&&it(s);for(const s of e)s.view||it(s)}function it(t){(t!==X||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class rt{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,e){(t=R(t)).dep&&nt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=R(this);return function(t){Y&&X&&tt((t=R(t)).dep||(t.dep=k()))}(t),!t._dirty&&t._cacheable||(t._dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const ot=new Set(["__proto__"]),ct=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function at(t){t.copy||(t.copy=O(t.base))}function ht(t,e){const s=t.__r_state;return(s?D(s):t)[e]}const ut=pt(),lt=ft();function ft(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=M(this),n=D(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(R)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){q();const s=D(M(this))[e].apply(this,t);return G(),s}})),t}function pt(){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(lt,e))return Reflect.get(lt,e,s);let i=Reflect.get(n,e,s);return(p(e)?ct.has(e):ot.has(e))?i:(Q(t,0,e),r(n,e)?(t.disposed||!d(i)||(C(i)||(at(t),i=t.copy[e]=Mt(i,t)),Z(i)),i):i)}}function dt(){return function(t,e,s,n){const i=D(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;at(t),A(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===M(n)&&(c?b(s,o)||st(t,"set",e,s):st(t,"add",e,s),function(t){let e=t;for(;e;)et(e),e=e.parent}(t))),!0}}const _t={get:ut,set:dt(),deleteProperty:function(t,e){const s=r(D(t),e);if((void 0!==ht(t.base,e)||e in t.base)&&(at(t),A(t)),t.copy){const n=delete t.copy[e];return n&&s&&st(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=D(t),n=Reflect.has(s,e);return p(e)&&ct.has(e)||Q(t,0,e),n},ownKeys:function(t){const e=D(t);return Q(t,0,a(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=D(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!a(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function yt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=Mt(e,t);Z(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function gt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):yt(t)}function bt(t){const e=this.__r_state,s=D(e);if(Q(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(C(n)||(gt(e),n=Mt(n,e),e.copy.set(t,n)),Z(n),n):n}function wt(t,e){const s=this.__r_state,n=D(s),i=n.has(t),r=n.get(t),o=()=>{gt(s),A(s),s.copy.set(t,e)};return i?b(e,r)||(o(),st(s,"set",t,e)):(o(),st(s,"add",t,e)),this}function vt(t){const e=this.__r_state;return D(e).has(t)||(yt(e),A(e),e.copy.add(t),st(e,"add",t,t)),this}function St(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 Ot(t){const e=this.__r_state,s=D(e),n=s.has(t);"get"in s&&s.get(t),gt(e),A(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&&st(e,"delete",t,void 0),i}function mt(){const t=this.__r_state,e=D(t),s=0!==e.size,n=e.clear();return s&&(gt(t),A(t),t.copy.clear(),st(t,"clear",void 0,void 0)),n}function xt(){const t={get:bt,get size(){return Q(t=this.__r_state,0,K),D(t).size;var t},has:St,add:vt,set:wt,delete:Ot,clear:mt,keys(){const t=this.__r_state;return 1===t.type?function(t){return Q(t,0,z),D(t).keys()}(t):function(t){return Q(t,0,K),yt(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),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 Q(s,0,K),yt(s),s.copy[t](...e)}}(e)})),[t]}const[jt]=xt();function Et(){const t=jt;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 Pt={get:Et()};const Ct=new WeakMap;let Rt=0;function Mt(t,e){const s=P(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:Rt++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=_t;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=Pt):4===s?(n.type=1,i=new Map,r=Pt):2===s&&(i=new Array),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(P(t)){case 1:case 2:return l;case 4:case 3:return u;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&&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]=o(t)}));const u={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(r(a,e)&&e in t?a:t,e,s)},l={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 At(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),$t(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 $t(t,e){t.modified=!1,t.base=e}function It(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=function(){return s.value};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 kt=s({$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)}),Lt={get:({_:t},e)=>{const{actions:s,views:n,accessCache:i,accessContext:o,ctx:c,stateValue:a}=t;if("$"!==e[0]){const t=i[e];if(void 0!==t)switch(t){case 0:return a[e];case 2:return n[e];case 1:if(1===o)return;return s[e];case 3:return c[e]}else{if(r(a,e))return i[e]=0,a[e];if(r(c,e))return i[e]=3,c[e]}}const h=kt[e];return h?h(t):r(c,e)?(i[e]=3,c[e]):void((t=>"_"===t||"$"===t)(e[0])&&r(a,e)&&function(t,...e){console.warn(`[Doura warn] ${t}`,...e)}(`Property ${JSON.stringify(e)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},set({_:t},e,s){const{ctx:n,actions:i,views:o,accessContext:c,stateRef:{value:a}}=t;return 1!==c&&(r(a,e)?(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(kt,e))&&(n[e]=s,!0))))},has({_:t},e){const{actions:s,views:n,accessCache:i,accessContext:o,ctx:c,stateValue:a}=t;return!!i[e]||r(a,e)||r(n,e)||1!==o&&r(s,e)||r(c,e)||r(kt,e)}},Nt={0:"scheduler flush. This is likely a Doura internals bug. Please open an issue at https://github.com/dourajs/doura/issues/new"};function Tt(t,e,...s){const n=Nt[e],i=n?"function"==typeof n?n.apply(null,s):n:"unknown error nr: "+Tt;throw new Error(`[Doura] ${i}`)}let Vt=!1,Ut=!1;const Ht=[];let Xt=0;const Kt=[];let zt=null,Jt=0;const Ft=[];let Wt=null,Yt=0;const Bt=Promise.resolve();let qt=null,Gt=null;function Qt(t){const e=qt||Bt;return t?e.then(this?t.bind(this):t):e}function Zt(t){Ht.length&&Ht.includes(t,Vt&&t.allowRecurse?Xt+1:Xt)||t===Gt||(null==t.id?Ht.push(t):Ht.splice(function(t){let e=Xt+1,s=Ht.length;for(;e<s;){const n=e+s>>>1;ee(Ht[n])<t?e=n+1:s=n}return e}(t.id),0,t),Vt||Ut||(Ut=!0,qt=Bt.then(se)))}function te(t,e=null){if(Kt.length){for(Gt=e,zt=[...new Set(Kt)],Kt.length=0,Jt=0;Jt<zt.length;Jt++)zt[Jt]();zt=null,Jt=0,Gt=null,te(t,e)}}const ee=t=>null==t.id?1/0:t.id;function se(t){Ut=!1,Vt=!0,te(t),Ht.sort(((t,e)=>ee(t)-ee(e)));try{for(Xt=0;Xt<Ht.length;Xt++){const t=Ht[Xt];if(t&&!1!==t.active){0;try{t()}catch(e){console.log("err",e),Tt(0,0)}}}}finally{Xt=0,Ht.length=0,function(t){if(te(),Ft.length){const t=[...new Set(Ft)];if(Ft.length=0,Wt)return void Wt.push(...t);for(Wt=t,Wt.sort(((t,e)=>ee(t)-ee(e))),Yt=0;Yt<Wt.length;Yt++)Wt[Yt]();Wt=null,Yt=0}}(),Vt=!1,qt=null,(Ht.length||Kt.length||Ft.length)&&se(t)}}var ne;function ie(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{r(t,s)&&y(e[s])?ie(t[s],e[s]):t[s]=e[s]}))}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(ne||(ne={}));class re{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=Mt({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&&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.effectScope=new I(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:ne.PATCH,payload:At(this.stateRef.value,this.stateRef.value),args:{patch:t}}))}replace(t){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const e of this.viewInstances)e.effect.scheduler();this.dispatch({type:ne.REPLACE,payload: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 G(),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 rt(t,e)}((()=>{const e=this.accessContext;this.accessContext=1;try{return t.call(this.proxy,this.proxy)}finally{this.accessContext=e}}))})),this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case ne.REPLACE:case ne.MODIFY:case ne.PATCH: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)&&this.dispatch({type:ne.MODIFY,payload:At(this.stateRef.value,this.stateRef)})}_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)=>{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=this.createView(t[e]),n=this;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get(){const t=s;let e=s.value;return s.mightChange?(s.mightChange=!1,t.__snapshot=At(e,n.stateRef.value)):t.__pre!==e&&(t.__snapshot=At(e,n.stateRef.value)),t.__pre=e,t.__snapshot},set:()=>!1})}}}function oe(t,e={}){return new re(t,e)}class ce{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=()=>{Zt(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._getNamedModel(t,e).proxy}getDetachedModel(t){return this._getDetachedModel(t).proxy}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}_getNamedModel(t,e){let s=this._models.get(t);if(s)return s;if("function"==typeof e){const s=this._createUseContext();e=e({use:this._use.bind(this,s)});const n=this._initModel(t,e);return s.setInitiator(n),n}return this._initModel(t,e)}_createUseContext(){let t,e=[];return{add(s){t?t.depend(s):e.push(s)},setInitiator(s){if(!t){t=s;for(const s of e)t.depend(s);e.length=0}}}}_getDetachedModel(t){let e;if("function"==typeof t){const s=t,n=this._createUseContext();e=oe(s({use:this._use.bind(this,n)})),n.setInitiator(e)}else{if("object"!=typeof t)throw new Error("invalid model");e=oe(t)}return e}_use(t,e,s){let n;const i=t;if("string"==typeof e){n=this._getNamedModel(e,s)}else{n=this._getDetachedModel(e)}return i&&i.add(n),n.proxy}_initModel(t,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.proxy,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}function ae({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new ce(t,e)}({initialState:t,plugins:e})}var he=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit({},{doura:n}){if(window.__doura=n,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(n.getState());const i=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(n.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(n.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(i))}},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","target","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,s as assign,w as def,E as defineModel,he as devtool,ae as doura,x as each,e as emptyObject,n as extend,r as hasOwn,v as invariant,b as is,a as isArray,l as isFunction,g as isIntegerKey,h as isMap,d as isObject,y as isPlainObject,u as isSet,f as isString,p as isSymbol,Qt as nextTick,o as objectToString,m as ownKeys,j as set,O as shallowCopy,_ as toRawType,c as toTypeString}; | ||
const t=()=>{},e=Object.create(null),s=Object.assign,n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var s in e)e.hasOwnProperty(s)&&(t[s]=e[s])},i=Object.prototype.hasOwnProperty,r=(t,e)=>i.call(t,e),o=Object.prototype.toString,c=t=>o.call(t),a=Array.isArray,h=t=>"[object Map]"===c(t),u=t=>"[object Set]"===c(t),l=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 S=Array.prototype.slice;function O(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);let 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 m="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:m)(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){h(t)?t.set(e,s):u(t)?(t.delete(e),t.add(s)):t[e]=s}const E=t=>t;function P(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 R(t){const e=M(t);return e?R(e.base):t}function M(t){return t&&t.__r_state}function D(t){return t.copy||t.base}function A(t){t.modified||(t.modified=!0,t.parent&&A(t.parent))}let $;class I{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 k=t=>{const e=new Set(t);return e.w=0,e.n=0,e},L=t=>(t.w&H)>0,N=t=>(t.n&H)>0,T=new WeakMap,V=new WeakMap;let U=0,H=1;let X;const K=Symbol(""),z=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=X,e=Y;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=X,X=this,Y=!0,H=1<<++U,U<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=H})(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];L(i)&&!N(i)?i.delete(t):e[s++]=i,i.w&=~H,i.n&=~H}e.length=s}})(this),H=1<<--U,X=this.parent,Y=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){X===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 Y=!0;const B=[];function q(){B.push(Y),Y=!1}function G(){const t=B.pop();Y=void 0===t||t}function Q(t,e,s){if(Y&&X){let e=T.get(t);e||T.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=k()),tt(n)}}function Z(t){if(Y&&X){const e=t.__r_state;if(!e)return;let s=V.get(e);s||V.set(e,s=k()),tt(s)}}function tt(t){let e=!1;U<=30?N(t)||(t.n|=H,e=!L(t)):e=!t.has(X),e&&(t.add(X),X.deps.push(t))}function et(t){const e=V.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function st(t,e,s,n,i){var r;const o=T.get(t),c=t.base;let u=[];if(o)if("clear"===e)u=[...o.values()];else if("length"===s&&a(c))o.forEach(((t,e)=>{("length"===e||e>=n)&&u.push(t)}));else switch(void 0!==s&&u.push(o.get(s)),e){case"add":a(c)?g(s)&&u.push(o.get("length")):(u.push(o.get(K)),h(c)&&u.push(o.get(z)));break;case"delete":a(c)||(u.push(o.get(K)),h(c)&&u.push(o.get(z)));break;case"set":h(c)&&u.push(o.get(K))}if(1===u.length)u[0]&&nt(u[0]);else{const t=[];for(const e of u)e&&t.push(...e);nt(k(t))}const l=null===(r=t.root)||void 0===r?void 0:r.listeners;l&&l.length&&l.forEach((t=>t()))}function nt(t){const e=a(t)?t:[...t];for(const s of e)s.view&&it(s);for(const s of e)s.view||it(s)}function it(t){(t!==X||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class rt{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,e){(t=R(t)).dep&&nt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=R(this);return function(t){Y&&X&&tt((t=R(t)).dep||(t.dep=k()))}(t),!t._dirty&&t._cacheable||(t._dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const ot=new Set(["__proto__"]),ct=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function at(t){t.copy||(t.copy=O(t.base))}function ht(t,e){const s=t.__r_state;return(s?D(s):t)[e]}const ut=pt(),lt=ft();function ft(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=M(this),n=D(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(R)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){q();const s=D(M(this))[e].apply(this,t);return G(),s}})),t}function pt(){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(lt,e))return Reflect.get(lt,e,s);let i=Reflect.get(n,e,s);return(p(e)?ct.has(e):ot.has(e))?i:(Q(t,0,e),r(n,e)?(t.disposed||!d(i)||(C(i)||(at(t),i=t.copy[e]=Mt(i,t)),Z(i)),i):i)}}function dt(){return function(t,e,s,n){const i=D(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;at(t),A(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===M(n)&&(c?b(s,o)||st(t,"set",e,s):st(t,"add",e,s),function(t){let e=t;for(;e;)et(e),e=e.parent}(t))),!0}}const _t={get:ut,set:dt(),deleteProperty:function(t,e){const s=r(D(t),e);if((void 0!==ht(t.base,e)||e in t.base)&&(at(t),A(t)),t.copy){const n=delete t.copy[e];return n&&s&&st(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=D(t),n=Reflect.has(s,e);return p(e)&&ct.has(e)||Q(t,0,e),n},ownKeys:function(t){const e=D(t);return Q(t,0,a(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=D(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!a(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function yt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=Mt(e,t);Z(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function gt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):yt(t)}function bt(t){const e=this.__r_state,s=D(e);if(Q(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(C(n)||(gt(e),n=Mt(n,e),e.copy.set(t,n)),Z(n),n):n}function wt(t,e){const s=this.__r_state,n=D(s),i=n.has(t),r=n.get(t),o=()=>{gt(s),A(s),s.copy.set(t,e)};return i?b(e,r)||(o(),st(s,"set",t,e)):(o(),st(s,"add",t,e)),this}function vt(t){const e=this.__r_state;return D(e).has(t)||(yt(e),A(e),e.copy.add(t),st(e,"add",t,t)),this}function St(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 Ot(t){const e=this.__r_state,s=D(e),n=s.has(t);"get"in s&&s.get(t),gt(e),A(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&&st(e,"delete",t,void 0),i}function mt(){const t=this.__r_state,e=D(t),s=0!==e.size,n=e.clear();return s&&(gt(t),A(t),t.copy.clear(),st(t,"clear",void 0,void 0)),n}function xt(){const t={get:bt,get size(){return Q(t=this.__r_state,0,K),D(t).size;var t},has:St,add:vt,set:wt,delete:Ot,clear:mt,keys(){const t=this.__r_state;return 1===t.type?function(t){return Q(t,0,z),D(t).keys()}(t):function(t){return Q(t,0,K),yt(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),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 Q(s,0,K),yt(s),s.copy[t](...e)}}(e)})),[t]}const[jt]=xt();function Et(){const t=jt;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 Pt={get:Et()};const Ct=new WeakMap;let Rt=0;function Mt(t,e){const s=P(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:Rt++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=_t;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=Pt):4===s?(n.type=1,i=new Map,r=Pt):2===s&&(i=new Array),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(P(t)){case 1:case 2:return l;case 4:case 3:return u;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&&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]=o(t)}));const u={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(r(a,e)&&e in t?a:t,e,s)},l={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 At(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),$t(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 $t(t,e){t.modified=!1,t.base=e}function It(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=function(){return s.value};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 kt=s({$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)}),Lt={get:({_:t},e)=>{const{actions:s,views:n,accessCache:i,ctx:o,stateValue:c}=t;if("$"!==e[0]){const t=i[e];if(void 0!==t)switch(t){case 0:return c[e];case 2:return n[e];case 1:return s[e];case 3:return o[e]}else{if(r(c,e))return i[e]=0,c[e];if(r(o,e))return i[e]=3,o[e]}}const a=kt[e];return a?a(t):r(o,e)?(i[e]=3,o[e]):void((t=>"_"===t||"$"===t)(e[0])&&r(c,e)&&function(t,...e){console.warn(`[Doura warn] ${t}`,...e)}(`Property ${JSON.stringify(e)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},set({_: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(kt,e))&&(n[e]=s,!0)))},has({_:t},e){const{actions:s,views:n,accessCache:i,accessContext:o,ctx:c,stateValue:a}=t;return!!i[e]||r(a,e)||r(n,e)||1!==o&&r(s,e)||r(c,e)||r(kt,e)}},Nt={0:"scheduler flush. This is likely a Doura internals bug. Please open an issue at https://github.com/dourajs/doura/issues/new"};function Tt(t,e,...s){const n=Nt[e],i=n?"function"==typeof n?n.apply(null,s):n:"unknown error nr: "+Tt;throw new Error(`[Doura] ${i}`)}let Vt=!1,Ut=!1;const Ht=[];let Xt=0;const Kt=[];let zt=null,Jt=0;const Ft=[];let Wt=null,Yt=0;const Bt=Promise.resolve();let qt=null,Gt=null;function Qt(t){const e=qt||Bt;return t?e.then(this?t.bind(this):t):e}function Zt(t){Ht.length&&Ht.includes(t,Vt&&t.allowRecurse?Xt+1:Xt)||t===Gt||(null==t.id?Ht.push(t):Ht.splice(function(t){let e=Xt+1,s=Ht.length;for(;e<s;){const n=e+s>>>1;ee(Ht[n])<t?e=n+1:s=n}return e}(t.id),0,t),Vt||Ut||(Ut=!0,qt=Bt.then(se)))}function te(t,e=null){if(Kt.length){for(Gt=e,zt=[...new Set(Kt)],Kt.length=0,Jt=0;Jt<zt.length;Jt++)zt[Jt]();zt=null,Jt=0,Gt=null,te(t,e)}}const ee=t=>null==t.id?1/0:t.id;function se(t){Ut=!1,Vt=!0,te(t),Ht.sort(((t,e)=>ee(t)-ee(e)));try{for(Xt=0;Xt<Ht.length;Xt++){const t=Ht[Xt];if(t&&!1!==t.active){0;try{t()}catch(e){Tt(0,0)}}}}finally{Xt=0,Ht.length=0,function(t){if(te(),Ft.length){const t=[...new Set(Ft)];if(Ft.length=0,Wt)return void Wt.push(...t);for(Wt=t,Wt.sort(((t,e)=>ee(t)-ee(e))),Yt=0;Yt<Wt.length;Yt++)Wt[Yt]();Wt=null,Yt=0}}(),Vt=!1,qt=null,(Ht.length||Kt.length||Ft.length)&&se(t)}}var ne;function ie(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{r(t,s)&&y(e[s])?ie(t[s],e[s]):t[s]=e[s]}))}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(ne||(ne={}));class re{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=Mt({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&&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.effectScope=new I(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:ne.PATCH,payload:At(this.stateRef.value,this.stateRef.value),args:{patch:t}}))}replace(t){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const e of this.viewInstances)e.effect.scheduler();this.dispatch({type:ne.REPLACE,payload: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 G(),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 rt(t,e)}((()=>{const e=this.accessContext;this.accessContext=1;try{return t.call(this.proxy,this.proxy)}finally{this.accessContext=e}}))})),this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case ne.REPLACE:case ne.MODIFY:case ne.PATCH: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)&&this.dispatch({type:ne.MODIFY,payload:At(this.stateRef.value,this.stateRef)})}_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=this.createView(t[e]),n=this;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get(){const t=s;let e=s.value;return s.mightChange?(s.mightChange=!1,t.__snapshot=At(e,n.stateRef.value)):t.__pre!==e&&(t.__snapshot=At(e,n.stateRef.value)),t.__pre=e,t.__snapshot},set:()=>!1})}}}function oe(t,e={}){return new re(t,e)}class ce{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=()=>{Zt(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._getNamedModel(t,e).proxy}getDetachedModel(t){return this._getDetachedModel(t).proxy}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}_getNamedModel(t,e){let s=this._models.get(t);if(s)return s;if("function"==typeof e){const s=this._createUseContext();e=e({use:this._use.bind(this,s)});const n=this._initModel(t,e);return s.setInitiator(n),n}return this._initModel(t,e)}_createUseContext(){let t,e=[];return{add(s){t?t.depend(s):e.push(s)},setInitiator(s){if(!t){t=s;for(const s of e)t.depend(s);e.length=0}}}}_getDetachedModel(t){let e;if("function"==typeof t){const s=t,n=this._createUseContext();e=oe(s({use:this._use.bind(this,n)})),n.setInitiator(e)}else{if("object"!=typeof t)throw new Error("invalid model");e=oe(t)}return e}_use(t,e,s){let n;const i=t;if("string"==typeof e){n=this._getNamedModel(e,s)}else{n=this._getDetachedModel(e)}return i&&i.add(n),n.proxy}_initModel(t,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.proxy,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}function ae({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new ce(t,e)}({initialState:t,plugins:e})}var he=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit({},{doura:n}){if(window.__doura=n,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(n.getState());const i=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(n.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(n.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(i))}},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","target","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,s as assign,w as def,E as defineModel,he as devtool,ae as doura,x as each,e as emptyObject,n as extend,r as hasOwn,v as invariant,b as is,a as isArray,l as isFunction,g as isIntegerKey,h as isMap,d as isObject,y as isPlainObject,u as isSet,f as isString,p as isSymbol,Qt as nextTick,o as objectToString,m as ownKeys,j as set,O as shallowCopy,_ as toRawType,c as toTypeString}; |
@@ -1,1 +0,1 @@ | ||
var Doura=function(t){"use strict";const e=()=>{},s=Object.create(null),n=Object.assign,i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var s in e)e.hasOwnProperty(s)&&(t[s]=e[s])},r=Object.prototype.hasOwnProperty,o=(t,e)=>r.call(t,e),c=Object.prototype.toString,a=t=>c.call(t),h=Array.isArray,u=t=>"[object Map]"===a(t),l=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 O(t){if(u(t))return new Map(t);if(l(t))return new Set(t);if(Array.isArray(t))return S.call(t);const e=Object.getOwnPropertyDescriptors(t);let 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 m="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function x(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function j(t){return!!t&&!!t.__r_state}function E(t){const e=P(t);return e?E(e.base):t}function P(t){return t&&t.__r_state}function C(t){return t.copy||t.base}function R(t){t.modified||(t.modified=!0,t.parent&&R(t.parent))}let M;class D{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&M&&(this.parent=M,this.index=(M.scopes||(M.scopes=[])).push(this)-1)}run(t){if(this.active){const e=M;try{return M=this,t()}finally{M=e}}}on(){M=this}off(){M=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 A=t=>{const e=new Set(t);return e.w=0,e.n=0,e},$=t=>(t.w&L)>0,I=t=>(t.n&L)>0,T=new WeakMap,k=new WeakMap;let N=0,L=1;let V;const U=Symbol(""),H=Symbol("");let K=0;class X{constructor(t,e=null,s){this.fn=t,this.scheduler=e,this.id=K++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=M){e&&e.active&&e.effects.push(t)}(this,s)}run(){if(!this.active)return this.fn();let t=V,e=F;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=V,V=this,F=!0,L=1<<++N,N<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=L})(this):z(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)&&!I(i)?i.delete(t):e[s++]=i,i.w&=~L,i.n&=~L}e.length=s}})(this),L=1<<--N,V=this.parent,F=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){V===this?this.deferStop=!0:this.active&&(z(this),this.onStop&&this.onStop(),this.active=!1)}}function z(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 Y(){const t=J.pop();F=void 0===t||t}function B(t,e,s){if(F&&V){let e=T.get(t);e||T.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=A()),G(n)}}function q(t){if(F&&V){const e=t.__r_state;if(!e)return;let s=k.get(e);s||k.set(e,s=A()),G(s)}}function G(t){let e=!1;N<=30?I(t)||(t.n|=L,e=!$(t)):e=!t.has(V),e&&(t.add(V),V.deps.push(t))}function Q(t){const e=k.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function Z(t,e,s,n,i){var r;const o=T.get(t),c=t.base;let a=[];if(o)if("clear"===e)a=[...o.values()];else if("length"===s&&h(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":h(c)?g(s)&&a.push(o.get("length")):(a.push(o.get(U)),u(c)&&a.push(o.get(H)));break;case"delete":h(c)||(a.push(o.get(U)),u(c)&&a.push(o.get(H)));break;case"set":u(c)&&a.push(o.get(U))}if(1===a.length)a[0]&&tt(a[0]);else{const t=[];for(const e of a)e&&t.push(...e);tt(A(t))}const l=null===(r=t.root)||void 0===r?void 0:r.listeners;l&&l.length&&l.forEach((t=>t()))}function tt(t){const e=h(t)?t:[...t];for(const s of e)s.view&&et(s);for(const s of e)s.view||et(s)}function et(t){(t!==V||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class st{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this._dirty=!0,this.effect=new X(t,(()=>{this._dirty||(this._dirty=!0,function(t,e){(t=E(t)).dep&&tt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=E(this);return function(t){F&&V&&G((t=E(t)).dep||(t.dep=A()))}(t),!t._dirty&&t._cacheable||(t._dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const nt=new Set(["__proto__"]),it=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function rt(t){t.copy||(t.copy=O(t.base))}function ot(t,e){const s=t.__r_state;return(s?C(s):t)[e]}const ct=ut(),at=ht();function ht(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=P(this),n=C(s);for(let e=0,r=this.length;e<r;e++)B(s,0,e+"");const i=n[e](...t);return-1===i||!1===i?n[e](...t.map(E)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){W();const s=C(P(this))[e].apply(this,t);return Y(),s}})),t}function ut(){return function(t,e,s){const n=C(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&s===jt.get(t))return t;if(h(n)&&o(at,e))return Reflect.get(at,e,s);let i=Reflect.get(n,e,s);return(p(e)?it.has(e):nt.has(e))?i:(B(t,0,e),o(n,e)?(t.disposed||!d(i)||(j(i)||(rt(t),i=t.copy[e]=Pt(i,t)),q(i)),i):i)}}function lt(){return function(t,e,s,n){const i=C(t),r=ot(i,e),c=h(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;rt(t),R(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===P(n)&&(c?b(s,r)||Z(t,"set",e,s):Z(t,"add",e,s),function(t){let e=t;for(;e;)Q(e),e=e.parent}(t))),!0}}const ft={get:ct,set:lt(),deleteProperty:function(t,e){const s=o(C(t),e);if((void 0!==ot(t.base,e)||e in t.base)&&(rt(t),R(t)),t.copy){const n=delete t.copy[e];return n&&s&&Z(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=C(t),n=Reflect.has(s,e);return p(e)&&it.has(e)||B(t,0,e),n},ownKeys:function(t){const e=C(t);return B(t,0,h(e)?"length":U),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=C(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!h(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function pt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=Pt(e,t);q(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function dt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):pt(t)}function _t(t){const e=this.__r_state,s=C(e);if(B(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(j(n)||(dt(e),n=Pt(n,e),e.copy.set(t,n)),q(n),n):n}function yt(t,e){const s=this.__r_state,n=C(s),i=n.has(t),r=n.get(t),o=()=>{dt(s),R(s),s.copy.set(t,e)};return i?b(e,r)||(o(),Z(s,"set",t,e)):(o(),Z(s,"add",t,e)),this}function gt(t){const e=this.__r_state;return C(e).has(t)||(pt(e),R(e),e.copy.add(t),Z(e,"add",t,t)),this}function bt(t){const e=this.__r_state;return B(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 wt(t){const e=this.__r_state,s=C(e),n=s.has(t);"get"in s&&s.get(t),dt(e),R(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&&Z(e,"delete",t,void 0),i}function vt(){const t=this.__r_state,e=C(t),s=0!==e.size,n=e.clear();return s&&(dt(t),R(t),t.copy.clear(),Z(t,"clear",void 0,void 0)),n}function St(){const t={get:_t,get size(){return B(t=this.__r_state,0,U),C(t).size;var t},has:bt,add:gt,set:yt,delete:wt,clear:vt,keys(){const t=this.__r_state;return 1===t.type?function(t){return B(t,0,H),C(t).keys()}(t):function(t){return B(t,0,U),pt(t),t.copy.values()}(t)},forEach(t,e){const s=this.__r_state;1===s.type?function(t,e,s,n){B(e,0,U),C(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 B(s,0,U),pt(s),s.copy[t](...e)}}(e)})),[t]}const[Ot]=St();function mt(){const t=Ot;return(e,s,n)=>{if("__r_isReactive"===s)return!0;if("__r_state"===s)return e;const i=C(e);return Reflect.get(o(t,s)&&s in i?t:i,s,n)}}const xt={get:mt()};const jt=new WeakMap;let Et=0;function Pt(t,e){const s=x(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:Et++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=ft;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=xt):4===s?(n.type=1,i=new Map,r=xt):2===s&&(i=new Array),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,jt.set(i,o),i.proxy}function Ct(t,e){const s=function(t,{copies:e,proxies:s}){const n=t=>{switch(x(t)){case 1:case 2:return l;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(j(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&&u(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,u(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 h={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(o(a,e)&&e in t?a:t,e,s)},l={get:(t,e,s)=>i(Reflect.get(t,e,s))};return n(t)}(t,e);if(j(t)){return new Proxy(e.copies.get(t.__r_state),s)}return new Proxy(O(t),s)}function Rt(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),Mt(t,s)):s=t.base,e.set(t,s);for(const e of t.children)n.push(e)}return s}(e);return Ct(t,s)}function Mt(t,e){t.modified=!1,t.base=e}function Dt(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=function(){return s.value};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 At=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=>Dt.bind(null,t)}),$t={get:({_:t},e)=>{const{actions:s,views:n,accessCache:i,accessContext:r,ctx:c,stateValue:a}=t;if("$"!==e[0]){const t=i[e];if(void 0!==t)switch(t){case 0:return a[e];case 2:return n[e];case 1:if(1===r)return;return s[e];case 3:return c[e]}else{if(o(a,e))return i[e]=0,a[e];if(o(c,e))return i[e]=3,c[e]}}const h=At[e];return h?h(t):o(c,e)?(i[e]=3,c[e]):void((t=>"_"===t||"$"===t)(e[0])&&o(a,e)&&function(t,...e){console.warn(`[Doura warn] ${t}`,...e)}(`Property ${JSON.stringify(e)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},set({_:t},e,s){const{ctx:n,actions:i,views:r,accessContext:c,stateRef:{value:a}}=t;return 1!==c&&(o(a,e)?(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(At,e))&&(n[e]=s,!0))))},has({_:t},e){const{actions:s,views:n,accessCache:i,accessContext:r,ctx:c,stateValue:a}=t;return!!i[e]||o(a,e)||o(n,e)||1!==r&&o(s,e)||o(c,e)||o(At,e)}},It={0:"scheduler flush. This is likely a Doura internals bug. Please open an issue at https://github.com/dourajs/doura/issues/new"};function Tt(t,e,...s){const n=It[e],i=n?"function"==typeof n?n.apply(null,s):n:"unknown error nr: "+Tt;throw new Error(`[Doura] ${i}`)}let kt=!1,Nt=!1;const Lt=[];let Vt=0;const Ut=[];let Ht=null,Kt=0;const Xt=[];let zt=null,Ft=0;const Jt=Promise.resolve();let Wt=null,Yt=null;function Bt(t){Lt.length&&Lt.includes(t,kt&&t.allowRecurse?Vt+1:Vt)||t===Yt||(null==t.id?Lt.push(t):Lt.splice(function(t){let e=Vt+1,s=Lt.length;for(;e<s;){const n=e+s>>>1;Gt(Lt[n])<t?e=n+1:s=n}return e}(t.id),0,t),kt||Nt||(Nt=!0,Wt=Jt.then(Qt)))}function qt(t,e=null){if(Ut.length){for(Yt=e,Ht=[...new Set(Ut)],Ut.length=0,Kt=0;Kt<Ht.length;Kt++)Ht[Kt]();Ht=null,Kt=0,Yt=null,qt(t,e)}}const Gt=t=>null==t.id?1/0:t.id;function Qt(t){Nt=!1,kt=!0,qt(t),Lt.sort(((t,e)=>Gt(t)-Gt(e)));try{for(Vt=0;Vt<Lt.length;Vt++){const t=Lt[Vt];if(t&&!1!==t.active){0;try{t()}catch(e){console.log("err",e),Tt(0,0)}}}}finally{Vt=0,Lt.length=0,function(t){if(qt(),Xt.length){const t=[...new Set(Xt)];if(Xt.length=0,zt)return void zt.push(...t);for(zt=t,zt.sort(((t,e)=>Gt(t)-Gt(e))),Ft=0;Ft<zt.length;Ft++)zt[Ft]();zt=null,Ft=0}}(),kt=!1,Wt=null,(Lt.length||Ut.length||Xt.length)&&Qt(t)}}var Zt;function te(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{o(t,s)&&y(e[s])?te(t[s],e[s]):t[s]=e[s]}))}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(Zt||(Zt={}));class ee{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=Pt({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&&Bt(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,$t),this.effectScope=new D(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,te(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:Zt.PATCH,payload:Rt(this.stateRef.value,this.stateRef.value),args:{patch:t}}))}replace(t){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const e of this.viewInstances)e.effect.scheduler();this.dispatch({type:Zt.REPLACE,payload: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 Y(),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 st(t,e)}((()=>{const e=this.accessContext;this.accessContext=1;try{return t.call(this.proxy,this.proxy)}finally{this.accessContext=e}}))})),this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case Zt.REPLACE:case Zt.MODIFY:case Zt.PATCH: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)&&this.dispatch({type:Zt.MODIFY,payload:Rt(this.stateRef.value,this.stateRef)})}_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)=>{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=this.createView(t[e]),n=this;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get(){const t=s;let e=s.value;return s.mightChange?(s.mightChange=!1,t.__snapshot=Rt(e,n.stateRef.value)):t.__pre!==e&&(t.__snapshot=Rt(e,n.stateRef.value)),t.__pre=e,t.__snapshot},set:()=>!1})}}}function se(t,e={}){return new ee(t,e)}class ne{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=()=>{Bt(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._getNamedModel(t,e).proxy}getDetachedModel(t){return this._getDetachedModel(t).proxy}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}_getNamedModel(t,e){let s=this._models.get(t);if(s)return s;if("function"==typeof e){const s=this._createUseContext();e=e({use:this._use.bind(this,s)});const n=this._initModel(t,e);return s.setInitiator(n),n}return this._initModel(t,e)}_createUseContext(){let t,e=[];return{add(s){t?t.depend(s):e.push(s)},setInitiator(s){if(!t){t=s;for(const s of e)t.depend(s);e.length=0}}}}_getDetachedModel(t){let e;if("function"==typeof t){const s=t,n=this._createUseContext();e=se(s({use:this._use.bind(this,n)})),n.setInitiator(e)}else{if("object"!=typeof t)throw new Error("invalid model");e=se(t)}return e}_use(t,e,s){let n;const i=t;if("string"==typeof e){n=this._getNamedModel(e,s)}else{n=this._getDetachedModel(e)}return i&&i.add(n),n.proxy}_initModel(t,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=se(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.proxy,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}var ie=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit({},{doura:n}){if(window.__doura=n,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(n.getState());const i=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(n.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(n.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(i))}},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","target","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=n,t.def=w,t.defineModel=t=>t,t.devtool=ie,t.doura=function({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new ne(t,e)}({initialState:t,plugins:e})},t.each=function(t,e,s=!1){y(t)?(s?Object.keys:m)(t).forEach((n=>{s&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach(((s,n)=>e(n,s,t)))},t.emptyObject=s,t.extend=i,t.hasOwn=o,t.invariant=v,t.is=b,t.isArray=h,t.isFunction=t=>"function"==typeof t,t.isIntegerKey=g,t.isMap=u,t.isObject=d,t.isPlainObject=y,t.isSet=l,t.isString=f,t.isSymbol=p,t.nextTick=function(t){const e=Wt||Jt;return t?e.then(this?t.bind(this):t):e},t.objectToString=c,t.ownKeys=m,t.set=function(t,e,s){u(t)?t.set(e,s):l(t)?(t.delete(e),t.add(s)):t[e]=s},t.shallowCopy=O,t.toRawType=_,t.toTypeString=a,t}({}); | ||
var Doura=function(t){"use strict";const e=()=>{},s=Object.create(null),n=Object.assign,i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var s in e)e.hasOwnProperty(s)&&(t[s]=e[s])},r=Object.prototype.hasOwnProperty,o=(t,e)=>r.call(t,e),c=Object.prototype.toString,a=t=>c.call(t),h=Array.isArray,u=t=>"[object Map]"===a(t),l=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 O(t){if(u(t))return new Map(t);if(l(t))return new Set(t);if(Array.isArray(t))return S.call(t);const e=Object.getOwnPropertyDescriptors(t);let 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 m="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?t=>Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t)):Object.getOwnPropertyNames;function x(t){switch(_(t)){case"Object":return 1;case"Array":return 2;case"Set":return 3;case"Map":return 4;default:return 0}}function j(t){return!!t&&!!t.__r_state}function E(t){const e=P(t);return e?E(e.base):t}function P(t){return t&&t.__r_state}function C(t){return t.copy||t.base}function R(t){t.modified||(t.modified=!0,t.parent&&R(t.parent))}let M;class D{constructor(t=!1){this.active=!0,this.effects=[],this.cleanups=[],!t&&M&&(this.parent=M,this.index=(M.scopes||(M.scopes=[])).push(this)-1)}run(t){if(this.active){const e=M;try{return M=this,t()}finally{M=e}}}on(){M=this}off(){M=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 A=t=>{const e=new Set(t);return e.w=0,e.n=0,e},$=t=>(t.w&L)>0,I=t=>(t.n&L)>0,T=new WeakMap,k=new WeakMap;let N=0,L=1;let V;const U=Symbol(""),H=Symbol("");let K=0;class X{constructor(t,e=null,s){this.fn=t,this.scheduler=e,this.id=K++,this.deps=[],this.active=!0,this.parent=void 0,function(t,e=M){e&&e.active&&e.effects.push(t)}(this,s)}run(){if(!this.active)return this.fn();let t=V,e=F;for(;t;){if(t===this)return;t=t.parent}try{return this.parent=V,V=this,F=!0,L=1<<++N,N<=30?(({deps:t})=>{if(t.length)for(let e=0;e<t.length;e++)t[e].w|=L})(this):z(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)&&!I(i)?i.delete(t):e[s++]=i,i.w&=~L,i.n&=~L}e.length=s}})(this),L=1<<--N,V=this.parent,F=e,this.parent=void 0,this.deferStop&&this.stop()}}stop(){V===this?this.deferStop=!0:this.active&&(z(this),this.onStop&&this.onStop(),this.active=!1)}}function z(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 Y(){const t=J.pop();F=void 0===t||t}function B(t,e,s){if(F&&V){let e=T.get(t);e||T.set(t,e=new Map);let n=e.get(s);n||e.set(s,n=A()),G(n)}}function q(t){if(F&&V){const e=t.__r_state;if(!e)return;let s=k.get(e);s||k.set(e,s=A()),G(s)}}function G(t){let e=!1;N<=30?I(t)||(t.n|=L,e=!$(t)):e=!t.has(V),e&&(t.add(V),V.deps.push(t))}function Q(t){const e=k.get(t);if(e){const t=[...e];for(const e of t)e.view&&(e.view.mightChange=!0)}}function Z(t,e,s,n,i){var r;const o=T.get(t),c=t.base;let a=[];if(o)if("clear"===e)a=[...o.values()];else if("length"===s&&h(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":h(c)?g(s)&&a.push(o.get("length")):(a.push(o.get(U)),u(c)&&a.push(o.get(H)));break;case"delete":h(c)||(a.push(o.get(U)),u(c)&&a.push(o.get(H)));break;case"set":u(c)&&a.push(o.get(U))}if(1===a.length)a[0]&&tt(a[0]);else{const t=[];for(const e of a)e&&t.push(...e);tt(A(t))}const l=null===(r=t.root)||void 0===r?void 0:r.listeners;l&&l.length&&l.forEach((t=>t()))}function tt(t){const e=h(t)?t:[...t];for(const s of e)s.view&&et(s);for(const s of e)s.view||et(s)}function et(t){(t!==V||t.allowRecurse)&&(t.scheduler?t.scheduler():t.run())}class st{constructor(t,{disableCache:e=!1}){this.dep=void 0,this.mightChange=!1,this._dirty=!0,this.effect=new X(t,(()=>{this._dirty||(this._dirty=!0,function(t,e){(t=E(t)).dep&&tt(t.dep)}(this))})),this.effect.view=this,this.effect.active=this._cacheable=!e}get value(){const t=E(this);return function(t){F&&V&&G((t=E(t)).dep||(t.dep=A()))}(t),!t._dirty&&t._cacheable||(t._dirty=!1,t._value=t.effect.run()),t._value}set value(t){}}const nt=new Set(["__proto__"]),it=new Set(Object.getOwnPropertyNames(Symbol).filter((t=>"arguments"!==t&&"caller"!==t)).map((t=>Symbol[t])).filter(p));function rt(t){t.copy||(t.copy=O(t.base))}function ot(t,e){const s=t.__r_state;return(s?C(s):t)[e]}const ct=ut(),at=ht();function ht(){const t={};return["includes","indexOf","lastIndexOf"].forEach((e=>{t[e]=function(...t){const s=P(this),n=C(s);for(let e=0,r=this.length;e<r;e++)B(s,0,e+"");const i=n[e](...t);return-1===i||!1===i?n[e](...t.map(E)):i}})),["push","pop","shift","unshift","splice"].forEach((e=>{t[e]=function(...t){W();const s=C(P(this))[e].apply(this,t);return Y(),s}})),t}function ut(){return function(t,e,s){const n=C(t);if("__r_isReactive"===e)return!0;if("__r_state"===e&&s===jt.get(t))return t;if(h(n)&&o(at,e))return Reflect.get(at,e,s);let i=Reflect.get(n,e,s);return(p(e)?it.has(e):nt.has(e))?i:(B(t,0,e),o(n,e)?(t.disposed||!d(i)||(j(i)||(rt(t),i=t.copy[e]=Pt(i,t)),q(i)),i):i)}}function lt(){return function(t,e,s,n){const i=C(t),r=ot(i,e),c=h(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;rt(t),R(t)}return b(t.copy[e],s)&&(void 0!==s||e in t.copy)||(t.copy[e]=s,t===P(n)&&(c?b(s,r)||Z(t,"set",e,s):Z(t,"add",e,s),function(t){let e=t;for(;e;)Q(e),e=e.parent}(t))),!0}}const ft={get:ct,set:lt(),deleteProperty:function(t,e){const s=o(C(t),e);if((void 0!==ot(t.base,e)||e in t.base)&&(rt(t),R(t)),t.copy){const n=delete t.copy[e];return n&&s&&Z(t,"delete",e,void 0),n}return!0},has:function(t,e){const s=C(t),n=Reflect.has(s,e);return p(e)&&it.has(e)||B(t,0,e),n},ownKeys:function(t){const e=C(t);return B(t,0,h(e)?"length":U),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=C(t),n=Reflect.getOwnPropertyDescriptor(s,e);return n?{writable:!0,configurable:!h(s)||"length"!==e,enumerable:n.enumerable,value:s[e]}:n}};function pt(t){t.copy||(t.copy=new Set,t.base.forEach((e=>{if(d(e)){const s=Pt(e,t);q(s),e!==s&&t.drafts.set(e,s),t.copy.add(s)}else t.copy.add(e)})))}function dt(t){1===t.type?function(t){t.copy||(t.copy=new Map(t.base))}(t):pt(t)}function _t(t){const e=this.__r_state,s=C(e);if(B(e,0,t),!s.has(t))return;let n=s.get(t);return d(n)?(j(n)||(dt(e),n=Pt(n,e),e.copy.set(t,n)),q(n),n):n}function yt(t,e){const s=this.__r_state,n=C(s),i=n.has(t),r=n.get(t),o=()=>{dt(s),R(s),s.copy.set(t,e)};return i?b(e,r)||(o(),Z(s,"set",t,e)):(o(),Z(s,"add",t,e)),this}function gt(t){const e=this.__r_state;return C(e).has(t)||(pt(e),R(e),e.copy.add(t),Z(e,"add",t,t)),this}function bt(t){const e=this.__r_state;return B(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 wt(t){const e=this.__r_state,s=C(e),n=s.has(t);"get"in s&&s.get(t),dt(e),R(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&&Z(e,"delete",t,void 0),i}function vt(){const t=this.__r_state,e=C(t),s=0!==e.size,n=e.clear();return s&&(dt(t),R(t),t.copy.clear(),Z(t,"clear",void 0,void 0)),n}function St(){const t={get:_t,get size(){return B(t=this.__r_state,0,U),C(t).size;var t},has:bt,add:gt,set:yt,delete:wt,clear:vt,keys(){const t=this.__r_state;return 1===t.type?function(t){return B(t,0,H),C(t).keys()}(t):function(t){return B(t,0,U),pt(t),t.copy.values()}(t)},forEach(t,e){const s=this.__r_state;1===s.type?function(t,e,s,n){B(e,0,U),C(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 B(s,0,U),pt(s),s.copy[t](...e)}}(e)})),[t]}const[Ot]=St();function mt(){const t=Ot;return(e,s,n)=>{if("__r_isReactive"===s)return!0;if("__r_state"===s)return e;const i=C(e);return Reflect.get(o(t,s)&&s in i?t:i,s,n)}}const xt={get:mt()};const jt=new WeakMap;let Et=0;function Pt(t,e){const s=x(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:Et++,root:null,parent:e,base:t,proxy:null,copy:null,modified:!1,disposed:!1,listeners:[],children:[]},i=n,r=ft;3===s?(n.type=2,n.drafts=new Map,i=new Set,r=xt):4===s?(n.type=1,i=new Map,r=xt):2===s&&(i=new Array),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,jt.set(i,o),i.proxy}function Ct(t,e){const s=function(t,{copies:e,proxies:s}){const n=t=>{switch(x(t)){case 1:case 2:return l;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(j(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&&u(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,u(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 h={get:(t,e,s)=>"__r_raw"===e?t:Reflect.get(o(a,e)&&e in t?a:t,e,s)},l={get:(t,e,s)=>i(Reflect.get(t,e,s))};return n(t)}(t,e);if(j(t)){return new Proxy(e.copies.get(t.__r_state),s)}return new Proxy(O(t),s)}function Rt(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),Mt(t,s)):s=t.base,e.set(t,s);for(const e of t.children)n.push(e)}return s}(e);return Ct(t,s)}function Mt(t,e){t.modified=!1,t.base=e}function Dt(t,e){const s=t.createView((function(){return e(this,this.$actions)})),n=function(){return s.value};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 At=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=>Dt.bind(null,t)}),$t={get:({_:t},e)=>{const{actions:s,views:n,accessCache:i,ctx:r,stateValue:c}=t;if("$"!==e[0]){const t=i[e];if(void 0!==t)switch(t){case 0:return c[e];case 2:return n[e];case 1:return s[e];case 3:return r[e]}else{if(o(c,e))return i[e]=0,c[e];if(o(r,e))return i[e]=3,r[e]}}const a=At[e];return a?a(t):o(r,e)?(i[e]=3,r[e]):void((t=>"_"===t||"$"===t)(e[0])&&o(c,e)&&function(t,...e){console.warn(`[Doura warn] ${t}`,...e)}(`Property ${JSON.stringify(e)} must be accessed via $state because it starts with a reserved character ("$" or "_") and is not proxied on the render context.`))},set({_: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(At,e))&&(n[e]=s,!0)))},has({_:t},e){const{actions:s,views:n,accessCache:i,accessContext:r,ctx:c,stateValue:a}=t;return!!i[e]||o(a,e)||o(n,e)||1!==r&&o(s,e)||o(c,e)||o(At,e)}},It={0:"scheduler flush. This is likely a Doura internals bug. Please open an issue at https://github.com/dourajs/doura/issues/new"};function Tt(t,e,...s){const n=It[e],i=n?"function"==typeof n?n.apply(null,s):n:"unknown error nr: "+Tt;throw new Error(`[Doura] ${i}`)}let kt=!1,Nt=!1;const Lt=[];let Vt=0;const Ut=[];let Ht=null,Kt=0;const Xt=[];let zt=null,Ft=0;const Jt=Promise.resolve();let Wt=null,Yt=null;function Bt(t){Lt.length&&Lt.includes(t,kt&&t.allowRecurse?Vt+1:Vt)||t===Yt||(null==t.id?Lt.push(t):Lt.splice(function(t){let e=Vt+1,s=Lt.length;for(;e<s;){const n=e+s>>>1;Gt(Lt[n])<t?e=n+1:s=n}return e}(t.id),0,t),kt||Nt||(Nt=!0,Wt=Jt.then(Qt)))}function qt(t,e=null){if(Ut.length){for(Yt=e,Ht=[...new Set(Ut)],Ut.length=0,Kt=0;Kt<Ht.length;Kt++)Ht[Kt]();Ht=null,Kt=0,Yt=null,qt(t,e)}}const Gt=t=>null==t.id?1/0:t.id;function Qt(t){Nt=!1,kt=!0,qt(t),Lt.sort(((t,e)=>Gt(t)-Gt(e)));try{for(Vt=0;Vt<Lt.length;Vt++){const t=Lt[Vt];if(t&&!1!==t.active){0;try{t()}catch(e){Tt(0,0)}}}}finally{Vt=0,Lt.length=0,function(t){if(qt(),Xt.length){const t=[...new Set(Xt)];if(Xt.length=0,zt)return void zt.push(...t);for(zt=t,zt.sort(((t,e)=>Gt(t)-Gt(e))),Ft=0;Ft<zt.length;Ft++)zt[Ft]();zt=null,Ft=0}}(),kt=!1,Wt=null,(Lt.length||Ut.length||Xt.length)&&Qt(t)}}var Zt;function te(t,e){const s=Object.keys(e);s.length&&s.forEach((s=>{o(t,s)&&y(e[s])?te(t[s],e[s]):t[s]=e[s]}))}!function(t){t.REPLACE="replace",t.MODIFY="modify",t.PATCH="patch"}(Zt||(Zt={}));class ee{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=Pt({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&&Bt(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,$t),this.effectScope=new D(r),this._initActions(),this._initViews()}patch(t){y(t)&&this._currentState&&(this._watchStateChange=!1,te(this.proxy.$state,t),this._watchStateChange=!0,this.dispatch({type:Zt.PATCH,payload:Rt(this.stateRef.value,this.stateRef.value),args:{patch:t}}))}replace(t){this._watchStateChange=!1,this.stateRef.value=t,this._watchStateChange=!0;for(const e of this.viewInstances)e.effect.scheduler();this.dispatch({type:Zt.REPLACE,payload: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 Y(),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 st(t,e)}((()=>{const e=this.accessContext;this.accessContext=1;try{return t.call(this.proxy,this.proxy)}finally{this.accessContext=e}}))})),this.viewInstances.push(e),e}reducer(t,e){switch(e.type){case Zt.REPLACE:case Zt.MODIFY:case Zt.PATCH: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)&&this.dispatch({type:Zt.MODIFY,payload:Rt(this.stateRef.value,this.stateRef)})}_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=this.createView(t[e]),n=this;Object.defineProperty(this.views,e,{configurable:!0,enumerable:!0,get(){const t=s;let e=s.value;return s.mightChange?(s.mightChange=!1,t.__snapshot=Rt(e,n.stateRef.value)):t.__pre!==e&&(t.__snapshot=Rt(e,n.stateRef.value)),t.__pre=e,t.__snapshot},set:()=>!1})}}}function se(t,e={}){return new ee(t,e)}class ne{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=()=>{Bt(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._getNamedModel(t,e).proxy}getDetachedModel(t){return this._getDetachedModel(t).proxy}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}_getNamedModel(t,e){let s=this._models.get(t);if(s)return s;if("function"==typeof e){const s=this._createUseContext();e=e({use:this._use.bind(this,s)});const n=this._initModel(t,e);return s.setInitiator(n),n}return this._initModel(t,e)}_createUseContext(){let t,e=[];return{add(s){t?t.depend(s):e.push(s)},setInitiator(s){if(!t){t=s;for(const s of e)t.depend(s);e.length=0}}}}_getDetachedModel(t){let e;if("function"==typeof t){const s=t,n=this._createUseContext();e=se(s({use:this._use.bind(this,n)})),n.setInitiator(e)}else{if("object"!=typeof t)throw new Error("invalid model");e=se(t)}return e}_use(t,e,s){let n;const i=t;if("string"==typeof e){n=this._getNamedModel(e,s)}else{n=this._getDetachedModel(e)}return i&&i.add(n),n.proxy}_initModel(t,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=se(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.proxy,{doura:this})})),s}_getInitialState(t){const e=this._initialState[t];return e&&delete this._initialState[t],e}}var ie=function(){if("undefined"==typeof window||!window.__REDUX_DEVTOOLS_EXTENSION__)return{};let t=0;const e=new Set;let s;return{onInit({},{doura:n}){if(window.__doura=n,"undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__){s=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:`doura${0===t?"":t}`}),t+=1,s.init(n.getState());const i=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(n.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(n.getState());case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return}}};e.add(s.subscribe(i))}},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","target","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=n,t.def=w,t.defineModel=t=>t,t.devtool=ie,t.doura=function({initialState:t,plugins:e=[]}={}){return function({initialState:t,plugins:e}={}){return new ne(t,e)}({initialState:t,plugins:e})},t.each=function(t,e,s=!1){y(t)?(s?Object.keys:m)(t).forEach((n=>{s&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach(((s,n)=>e(n,s,t)))},t.emptyObject=s,t.extend=i,t.hasOwn=o,t.invariant=v,t.is=b,t.isArray=h,t.isFunction=t=>"function"==typeof t,t.isIntegerKey=g,t.isMap=u,t.isObject=d,t.isPlainObject=y,t.isSet=l,t.isString=f,t.isSymbol=p,t.nextTick=function(t){const e=Wt||Jt;return t?e.then(this?t.bind(this):t):e},t.objectToString=c,t.ownKeys=m,t.set=function(t,e,s){u(t)?t.set(e,s):l(t)?(t.delete(e),t.add(s)):t[e]=s},t.shallowCopy=O,t.toRawType=_,t.toTypeString=a,t}({}); |
{ | ||
"name": "doura", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"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
436561
11196