@meshflow/core 0.1.7 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.d.mts +253 -220
- package/index.d.ts +253 -220
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var ne=class{computedRules=[];store={OR:(e,r)=>{let t,l,n=this.computedRules;for(let i=0;i<n.length;i++){let a=n[i],o=a.logic(e);if(o instanceof Promise)return (async()=>{let d=await o;if(a.entityId==="__base__"?l=d:d&&(t=a.value),typeof t>"u")for(let u=i+1;u<n.length;u++){let y=n[u],S=y.logic(e),f=S instanceof Promise?await S:S;if(y.entityId==="__base__"){l=f;continue}if(f){t=y.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}})();let s=o;if(a.entityId==="__base__"){l=s;continue}if(s){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}},PRIORITY:(e,r)=>{let t=null,l=this.computedRules;for(let n=0;n<l.length;n++){let a=l[n].logic(e);if(a instanceof Promise)return (async()=>{let o=await a;if(o!==void 0)return {res:o,version:r};for(let s=n+1;s<l.length;s++){let d=l[s].logic(e),u=d instanceof Promise?await d:d;if(u!==void 0)return {res:u,version:r}}return {res:void 0,version:r}})();if(a!==void 0)return {res:a,version:r}}return {res:t,version:r}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat().sort((r,t)=>t.priority-r.priority):this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,r){return this.CurrentStrategy(e,r)}},$=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,r,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=r==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}updateInputValueRule(e){this.isValue&&this.setRule({priority:1,entityId:"__input_value__",logic:()=>e});}setDefaultRule(e){let r=new Set;r.add(e),this.rules.set(e.id,r);}setRules(e,r){r&&this.updateDeps(r);let t=++this.id,l={...e,entityId:t};for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[r,t]of e)this.deps.set(r,t);}setRule(e,r){if(r&&this.updateDeps(r),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let r=null;if(e.GetToken&&(r=e.GetToken()),this.pendingPromise&&this.promiseToken!==r&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let i=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof i=="object"||typeof a=="object")t=false;else {let o=Array.from(this.deps.keys());for(let s of o){let d=this.deps.get(s),u=e.GetValueByPath(s);if(d!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=r;let l=++this.version,n=this.strategy.evaluate(e,l);if(!(n instanceof Promise)){let{res:i,version:a}=n;return this.finalizeSync(i,a,e,r)}return this.pendingPromise=(async()=>{try{let{res:i,version:a}=await n;return this.finalizeSync(i,a,e,r)}catch(i){throw {path:this.path,error:i}}finally{this.promiseToken===r&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,r,t,l){return l!==this.promiseToken||r<this.version?this.cache:(this.cache=e,this.deps.forEach((n,i)=>{this.deps.set(i,t.GetValueByPath(i));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},te=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},r={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(r.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(r);}evaluate(e,r){let t=true,l=[...this.defaultValidators,...this.validators];for(let n of l){if(!n.condition(r))continue;let a=n.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var ae=(c={frameQuota:12})=>{let e=performance.now(),r=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),r=0,t=true;},shouldYield(){let n=performance.now();return r++,!!((r>=5||t)&&(r=0,n-e>c.frameQuota||l()))},async yieldToMain(){return new Promise(n=>{Pe(()=>{e=performance.now(),r=0,t&&(t=false),n();});})}}},Pe=c=>{let{port1:e,port2:r}=new MessageChannel;e.onmessage=c,r.postMessage(null);};function oe(c,e,r,t,l){let n=new Map,i=c.useGreedy,a=ae();return async(s,d)=>{let y=Symbol("token");n.set(s,y);let S=false;a.reset();let f=new Set,R=new Set,C=new Set(e.GetAllNextDependency(s));C.add(s);let E=new Map,N=new Map,w=new Set,V=e.GetPathToLevelMap(),_=V.get(s)??0,z=0,q=T=>{e.GetAllNextDependency(T).forEach(F=>{let A=V.get(F)||0;A>z&&(z=A);});};q(s),d.forEach(T=>{w.add(T);}),f.add(s);let W=performance.now();t.emit("flow:start",{path:s}),t.callOnStart({path:s});let H=false,J=30,Z=T=>{let{target:x,trigger:F}=T,A=false,O=false,k=r.GetRenderSchemaByPath(x),M=[],m=(p,g)=>{if(f.has(p)||R.has(p)||w.has(p))return;let D=0,v=V.get(p)??0;if(E.has(p))D=E.get(p)-1;else {if(v>_&&E.size>J){N.has(v)||N.set(v,new Set),N.get(v).add(p),t.emit("node:intercept",{path:p,type:7});return}let I=e.GetPrevDependency(p),B=0;for(let U of I){if(f.has(U))continue;(V.get(U)??0)>_&&B++;}D=B;}if(D<=0){let I=w.has(p),B=R.has(p);if(I||B){t.emit("node:intercept",{path:p,type:B?3:3.1});return}E.delete(p),w.add(p),t.emit("node:release",{path:p,type:g,detail:{path:x}});}else E.set(p,D);},b=(p=[])=>{if(n.get(s)!==y)return;if(p.length){let v={};for(let I of p){let B=(I.args||[]).reduce((U,ee)=>(U[ee]=k[ee],U),{});try{let U=I.fn(B);U&&typeof U=="object"&&Object.assign(v,U);}catch(U){}}for(let I in v)k[I]=v[I];A=true;}A&&l.flushPathSet.add(x),t.emit("node:success",{path:x}),f.add(x);let g=e.GetNextDependency(x);(A||O)&&e.GetAllNextDependency(x).forEach(I=>C.add(I));for(let v of g){if(f.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(R.has(v)||w.has(v)){t.emit("node:intercept",{path:v,type:R.has(v)?3:3.1});continue}if(A||O)m(v,1);else if(E.has(v))m(v,2);else {let B=V.get(v);N.has(B)||N.set(B,new Set);let U=N.get(B);U.has(v)||(U.add(v),t.emit("node:stagnate",{path:v,type:1}));}}R.delete(x),(async()=>{if(!S){let v=R.size,I=w.size;t.emit("flow:fire",{path:x,type:1,detail:{active:v,pending:I,blocked:E.size}}),h();}})();},G=p=>{t.emit("node:error",{path:x,error:p});let g=Symbol("abort");n.set(s,g),w.clear(),E.clear(),R.clear(),t.callOnError(p);},P=(p,g)=>{let D=false;p!==k[g]&&(k[g]=p,A=true,t.emit("node:bucket:success",{path:x,key:g,value:p}),g==="value"&&(D=true)),k.nodeBucket[g].isForceNotify()&&(O=true),(D||O)&&q(x);};t.emit("node:start",{path:x});try{let p=[];for(let g in k.nodeBucket){let D=k.nodeBucket[g];p.push(...D.getSideEffect());let v=D.evaluate({affectKey:g,triggerPath:F,GetRenderSchemaByPath:r.GetRenderSchemaByPath,GetValueByPath:I=>r.GetRenderSchemaByPath(I).value,GetToken:()=>y});if(v instanceof Promise){let I=v.then(B=>{n.get(s)===y&&P(B,g);});M.push(I);}else P(v,g);}if(M.length>0)return Promise.all(M).then(()=>{b(p);}).catch(G);b(p);return}catch(p){G(p);}},h=async()=>{if(n.get(s)!==y){S=false;return}S=true;let T=a.getIsFirstFrame(),x=0,F=()=>i&&T?30:1/0,A=0,O=F();try{for(;n.get(s)===y;){let k=A>=O,M=a.shouldYield();if(k||M){if(A>0&&(x++,(T||x%2===0)&&l.requestUpdate()),await a.yieldToMain(),n.get(s)!==y)break;A=0,T=a.getIsFirstFrame();}if(w.size>0&&R.size<5){for(let m of w){if(R.size>=5||A>=O)break;let b=V.get(m)??0,G=e.GetPrevDependency(m),P=G.length>1;if((!i||P)&&b>_){w.delete(m);let g=G.filter(D=>C.has(D)&&!f.has(D)).length;E.set(m,g||0),t.emit("node:intercept",{path:m,type:g>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:g}});continue}if(w.delete(m),R.add(m),t.emit("node:processing",{path:m}),Z({target:m,trigger:s}),A++,A>=O||a.shouldYield())break}if(w.size>0)continue}if(A<O&&i&&E.size>0&&R.size<5){let m=!1,b=0;for(let[G,P]of E)if(P<=0){let p=V.get(G)??0,g=e.GetPrevDependency(G);if(p>_&&g.length>1)continue;if(E.delete(G),w.add(G),b++,m=!0,t.emit("node:release",{path:G,type:4}),b>=O)break}if(b>0)continue;if(m){if(a.shouldYield()&&(await a.yieldToMain(),n.get(s)!==y))break;continue}}if(R.size===0&&w.size===0){let m=new Set;for(let P of N.keys())m.add(P);for(let[P]of E){let p=V.get(P)??0;p>_&&m.add(p);}let b=Array.from(m).sort((P,p)=>P-p),G=b[0];if(b.length>0&&G<=z){let P=b[0];if(P<=z){_=P;let p=N.get(P);p&&(p.forEach(g=>w.add(g)),N.delete(P));for(let[g]of E)(V.get(g)??0)===P&&(E.delete(g),w.add(g),t.emit("node:release",{path:g,type:3,detail:{level:P}}));continue}}else {N.forEach((P,p)=>{P.forEach(g=>{f.add(g),t.emit("node:intercept",{path:g,type:6});});}),N.clear();for(let[P]of E)f.add(P),t.emit("node:intercept",{path:P,type:6});E.clear();break}}w.size>0&&R.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,R.size+E.size+w.size===0){if(n.get(s)===y&&!H){H=true,t.emit("flow:end",{type:1}),l.requestUpdate();let M=performance.now();t.emit("flow:success",{duration:(M-W).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:R.size}});}};h();}}function se(c,e,r,t,l,n){let i=ve(c),a=0,o=new Map,s=new Map,d=new Map,u=false,y=new Set,S=false,f=true,R=null,C=h=>{let T=o.get(h);return s.get(T)},E=h=>d.get(h),N=async()=>{let h=Array.from(y);y.clear();for(let T of h){let x=C(T);n.signalTrigger(x.dirtySignal);}},w=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;y.size>0;)N();}finally{u=false;}}));},V=()=>{let h=(T,x,F)=>{if(typeof T!="object"||T===null||Array.isArray(T)){if(F.length>0){let O=F[F.length-1];x[O]=C(F.join(".")).value;}return}let A=Object.getOwnPropertyNames(T);for(let O of A)F.push(O),h(T[O],T,F),F.pop();};return h(i,null,[]),i},Y=oe({useGreedy:e.useGreedy},r,{GetRenderSchemaByPath:C},l,{requestUpdate:w,flushPathSet:y}),_=async()=>(S&&R||(S=true,R=(async()=>{let h=r.GetDependencyOrder(),T=performance.now(),x=performance.now();try{for(let A=0;A<h.length;A++){let O=h[A];await Promise.all(O.map(async k=>{let M=C(k),m=!1;for(let b in M.nodeBucket){let G=await M.nodeBucket[b].evaluate({affectKey:b,triggerPath:void 0,GetRenderSchemaByPath:C,GetValueByPath:P=>C(P).value,isSameToken:()=>!0});if(b==="options"){let P=!1,p=M.value;for(let g of G)if(g.value==p){P=!0;break}P||(M.value=void 0,m=!0);}G!==M[b]&&(M[b]=G,m=!0);}m&&y.add(k);})),performance.now()-x>12&&(await new Promise(k=>requestAnimationFrame(k)),x=performance.now());}y.size>0&&w(),f=!1;let F=performance.now();l.emit("flow:success",{duration:(F-T).toFixed(2)+"ms"}),l.callOnSuccess();}catch(F){throw l.emit("node:error",{path:F.path,error:F.error}),l.callOnError(F),F}finally{S=false,R=null,f=false;}})()),R),z=h=>{if(f)return;if(!C(h))throw Error("Node undefined");performance.now();y.add(h),w();let F=r.GetNextDependency(h);q(F,h);};function q(h,T){Y(T,h);}let W=h=>{if(!h)throw Error("path error");let T=C(h);T.nodeBucket.value&&T.nodeBucket.value.updateInputValueRule(T.value);},H=(h,T="")=>{let x="name"in h?h.name:void 0,F=x?T===""?x:`${T}.${x}`:T,A=n.signalCreateor(),O=a++,k={getRenderSchema:b=>C(b)},M=(b,G)=>{let P=b(G),p=C(G.path),g=t.createHistoryAction([{path:G.path,value:p.value},{path:G.path,value:P}],D=>{let v=C(D.path);v.value=D.value,W(D.path),z(D.path);});p.value=P,t.pushIntoHistory(g),W(G.path),z(G.path);},m={...h,disabled:!!h.disabled,hidden:"hidden"in h?h.hidden:false,readonly:"readonly"in h?h.readonly:false,required:"required"in h?h.required:false,path:F,dirtySignal:A,uid:O,nodeBucket:{},validators:new te(F),theme:"secondary",dependOn:b=>M(b,{...k,path:F})};return h.type==="group"&&(delete m.nodeBucket,delete m.validators,m.children=h.children.map(b=>H(b,F)),d.set(m.path,m)),o.set(m.path,m.uid),s.set(m.uid,m),m};return {schema:H(c),GetFormData:()=>V(),GetRenderSchemaByPath:C,GetGroupByPath:E,notifyAll:_,convertToRenderSchema:H}}function ve(c,e={}){let r=n=>{if(n.type=="group")return {key:n.name||"",isGroup:true,val:n.children.reduce((i,a)=>[...i,r(a)],[])};if(n.type=="input"||n.type=="number"||n.type=="select"||n.type=="checkbox")return {key:n.name,isGroup:false,val:n.value};throw Error(`undefined type:${n.type}`)},t=(n,i)=>{if(i.isGroup){let a={};i.key===""?a=n:n[i.key]=a,i.val.forEach(o=>{t(a,o);});}else n[i.key]=i.val;},l=r(c);return t(e,l),e}var ie=(c,e,r)=>{let l=n=>{let i=r.triggerPaths.map(s=>n.GetValueByPath(s)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>i}),Object.defineProperty(a,"affectedTatget",{get:()=>n.GetRenderSchemaByPath(c)[e]}),r.logic({slot:a})};return {value:r.value,targetPath:c,triggerPaths:r.triggerPaths,priority:r.priority??10,logic:l}},le=(c,e,r)=>{if(!c)throw Error("");let t=c,l=(a,o)=>{e.has(a)||e.set(a,new Set),e.get(a).add(o),r.has(o)||r.set(o,new Set),r.get(o).add(a);};return {SetRule:(a,o,s,d={logic:u=>{}})=>{let u=t(o),y=ie(o,s,{...d,triggerPaths:[a]}),S=[a].map(f=>[f,t(f).value]);if(l(a,o),u.nodeBucket[s])u.nodeBucket[s].setRule(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRule(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();},SetRules:(a,o,s,d={logic:u=>{}})=>{let u=t(o);for(let f of a)l(f,o);let y=ie(o,s,{...d,triggerPaths:a}),S=a.map(f=>[f,t(f).value]);if(u.nodeBucket[s])u.nodeBucket[s].setRules(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRules(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();}}},ce=c=>{let e=c||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,n)=>{e(t).nodeBucket[l].setStrategy(n);}}};var ue=c=>{let e=c||void 0;return {SetValidators:(t,l)=>{let n=e(t),i=(o,s,d)=>o(s,d),a=(o,s,d)=>o(s,d);if(!n.validators)throw Error("validator init error");n.validators.setValidators({logic:o=>i(l.logic,o,e),condition:typeof l.condition=="function"?o=>a(l.condition,o,e):()=>true});}}};function de(){let c=new Map,e=new Map,r=new Set,t=(i,a)=>{c.set(i,a);let o=e.get(i);o&&o(a);};return {SetTrace:(i,a)=>{e.set(i,a);let o=c.get(i)||"idle";return a(o),()=>{e.delete(i);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{r.forEach(o=>t(o,"idle")),r.clear(),c.clear();}),a.on("node:release",({path:o,type:s})=>{(s==1||s==2)&&(r.add(o),t(o,"pending"));}),a.on("node:pending",({path:o})=>{r.add(o),(!c.has(o)||c.get(o)==="idle")&&t(o,"pending");}),a.on("node:start",({path:o})=>{r.add(o),t(o,"calculating");}),a.on("node:success",({path:o})=>{t(o,"calculated");}),a.on("node:intercept",({path:o,type:s})=>{s==3&&t(o,"calculating"),s==6&&t(o,"idle");}),a.on("node:stagnate",({path:o})=>{t(o,"pending");}),a.on("node:error",({path:o})=>t(o,"error"));}})}}function pe(c,e,r,t){let l=d=>{let u=c(),y=e(),S=new Set;return u.get(d)?.forEach(f=>S.add(f)),S.size===0?[]:Array.from(S).filter(f=>{let R=y.get(f)||new Set;return !Array.from(R).some(N=>S.has(N))})};return {GetNextDependency:d=>{let u=t();return Array.from(u.get(d)||[])},GetPrevDependency:d=>{let u=r();return Array.from(u.get(d)||[])},GetAllPrevDependency:d=>{let u=e();return Array.from(u.get(d)||[])},GetAllNextDependency:d=>{let u=c();return Array.from(u.get(d)||[])},rebuildDirectDependencyMaps:d=>{let u=new Map,y=new Map;for(let S of d){let f=l(S);u.set(S,new Set(f));for(let R of f)y.has(R)||y.set(R,new Set),y.get(R).add(S);}return {directNextMap:u,directPrevMap:y}}}}function fe(c){let e=t=>{let l=[],n=[],i=new Map,a=t.size,o=0,s=0;for(let[d,u]of t)u===0&&n.push(d);if(n.length===0&&a>0)throw Error("Circular dependency detected");for(;n.length>0;){l.push([...n]);let d=[];for(let u of n){o++,i.set(u,s);let y=c.get(u);if(y)for(let S of y){let f=t.get(S)-1;t.set(S,f),f===0&&d.push(S);}}n=d,s++;}if(o<a)throw Error("Circular dependency detected");return {steps:l,levelMap:i}};return ()=>{let t=new Map;for(let l of c.keys()){let n=Array.from(c.get(l)||[]);t.has(l)||t.set(l,0);for(let i of n){let a=t.get(i)||0;t.set(i,++a);}}return e(t)}}function ye(){let c=[],e=[],t={canRedo:()=>{},canUndo:()=>{}},l=()=>{if(!c.length)return;let u=c.pop();u?.undoAction(),o(u);},n=()=>{if(!e.length)return;let u=e.pop();u?.redoAction(),s(u,false);},i=u=>{t.canUndo=()=>u(c.length);},a=u=>{t.canRedo=()=>u(e.length);},o=u=>{e.push(u),e.length>100&&e.shift(),t.canRedo(),t.canUndo();},s=(u,y=true)=>{y&&(e.length=0),c.push(u),c.length>100&&c.shift(),t.canUndo(),t.canRedo();};return {Undo:l,Redo:n,PushIntoHistory:s,CreateHistoryAction:(u,y)=>{let[S,f]=u;return {undoAction:()=>y(S),redoAction:()=>y(f)}},initCanUndo:i,initCanRedo:a}}var Q=()=>{let c=[];return {on:e=>(c.push(e),()=>{let r=c.indexOf(e);r>-1&&c.splice(r,1);}),call:e=>c.forEach(r=>r(e))}};function he(){let{on:c,call:e}=Q();return {onError:c,callOnError:e}}function me(){let{on:c,call:e}=Q();return {onSuccess:c,callOnSuccess:e}}var ge=()=>{let c=new Set,e=new Map,r=(n,i)=>{e.get(n)?.forEach(a=>a(i));},t=(n,i)=>(e.has(n)||e.set(n,new Set),e.get(n).add(i),()=>e.get(n).delete(i));return {usePlugin:n=>{let i=new Set,a=(o,s)=>{let d=t(o,s);return i.add(d),d};return n.apply({on:a}),c.add(n),()=>{i.forEach(o=>o()),i.clear(),c.delete(n);}},emit:r}};function Se(){let{on:c,call:e}=Q();return {onStart:c,callOnStart:e}}function Te(c,e,r){let t=false,l=false,n=new Map,i=new Map,a=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetAllNextDependency:R,rebuildDirectDependencyMaps:C}=pe(()=>n,()=>i,()=>o,()=>a),{Undo:E,Redo:N,PushIntoHistory:w,CreateHistoryAction:V,initCanUndo:Y,initCanRedo:_}=ye(),{onError:z,callOnError:q}=he(),{onSuccess:W,callOnSuccess:H}=me(),{onStart:J,callOnStart:Z}=Se(),{emit:h,usePlugin:T}=ge(),{SetTrace:x,useTrace:F}=de(),A=F();T(A);let{schema:O,GetFormData:k,GetRenderSchemaByPath:M,GetGroupByPath:m,notifyAll:b,convertToRenderSchema:G}=se(c,{useGreedy:e.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:R,GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetPathToLevelMap:()=>d},{pushIntoHistory:w,createHistoryAction:V},{callOnError:q,callOnSuccess:H,callOnStart:Z,emit:h},r),P=(K,L)=>{let j=m(K),re=G(L,K);return j.children.push(re),j.dirtySignal.value++,re},{SetRule:p,SetRules:g}=le(M,n,i),{SetStrategy:D}=ce(M),{SetValidators:v}=ue(M),I=fe(n),B=()=>{let K=I();s=K.steps,d=K.levelMap;};return {schema:O,SetRule:(...K)=>{p.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetRules:(...K)=>{g.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetStrategy:D,SetValidators:v,SetTrace:x,usePlugin:T,SetValue:(K,L)=>{M(K).dependOn(()=>L);},GetValue:(K,L="value")=>M(K)[L],GetFormData:k,GetGroupByPath:m,notifyAll:async()=>{B(),await b();},AddNewSchema:P,GetAllDependency:()=>n,GetDependencyOrder:()=>s,Undo:E,Redo:N,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:W,onStart:J}}var X=new Map,Re=(c,e,r)=>{try{if(typeof r.UITrigger.signalCreateor!="function"||typeof r.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(X.has(c))throw Error("engineID repeated");let t=Te(e,r.config||{useGreedy:!1},r.UITrigger),{schema:l,GetFormData:n,SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,SetValue:d,GetValue:u,usePlugin:y,GetGroupByPath:S,notifyAll:f,SetTrace:R,GetAllDependency:C,GetDependencyOrder:E,AddNewSchema:N,Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:q,onStart:W}=t,H={config:{SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,notifyAll:f,SetTrace:R,usePlugin:y},data:{schema:l,GetFormData:n,AddNewSchema:N,SetValue:d,GetValue:u,GetGroupByPath:S},history:{Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_},dependency:{GetAllDependency:C,GetDependencyOrder:E},hooks:{onError:z,onSuccess:q,onStart:W}};return X.set(c,H),H}catch(t){throw Error(t)}},pt=c=>{if(X.has(c))return X.get(c);throw Error("id undefined")},ft=c=>{X.delete(c);},yt=Re;exports.deleteEngine=ft;exports.useEngine=pt;exports.useEngineManager=Re;exports.useMeshFlow=yt;
|
|
1
|
+
'use strict';var Z=class{computedRules=[];store={OR:(e,n)=>{let t,c,r=this.computedRules;for(let l=0;l<r.length;l++){let o=r[l],s=o.logic(e);if(s instanceof Promise)return (async()=>{let i=await s;if(o.entityId==="__base__"?c=i:i&&(t=i),typeof t>"u")for(let u=l+1;u<r.length;u++){let f=r[u],p=f.logic(e),P=p instanceof Promise?await p:p;if(f.entityId==="__base__"){c=P;continue}if(P){t=f.value;break}}return typeof t>"u"&&(t=c),{res:t,version:n}})();let d=s;if(o.entityId==="__base__"){c=d;continue}if(d){t=o.value;break}}return typeof t>"u"&&(t=c),{res:t,version:n}},PRIORITY:(e,n)=>{let t,c=this.computedRules;for(let r=0;r<c.length;r++){let o=c[r].logic(e);if(o instanceof Promise)return (async()=>{let s=await o;if(s!==void 0)return {res:s,version:n};for(let d=r+1;d<c.length;d++){let i=c[d].logic(e),u=i instanceof Promise?await i:i;if(u!==void 0)return {res:u,version:n}}return {res:void 0,version:n}})();if(o!==void 0)return {res:o,version:n}}return {res:t,version:n}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat().sort((n,t)=>t.priority-n.priority):this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,n){return this.CurrentStrategy(e,n)}},X=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,n,t){let c=()=>this.rules;this.strategy=new Z(c),this.path=t,this.isValue=n==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}setDefaultRule(e){let n=new Set;n.add(e),this.rules.set("defaultRules",n);}setRules(e,n){n&&this.updateDeps(n);let t=++this.id,c={...e,entityId:t};for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(c);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let l=this.rules.get(r);l&&(l.delete(c),l.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[n,t]of e)this.deps.set(n,t);}setRule(e,n){if(n&&this.updateDeps(n),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,c={...e,entityId:t};if(e)for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(c);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let l=this.rules.get(r);l&&(l.delete(c),l.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let n=null;if(e.GetToken&&(n=e.GetToken()),this.pendingPromise&&this.promiseToken!==n&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let l=this.deps.get(e.triggerPath),o=e.GetValueByPath(e.triggerPath);if(typeof l=="object"||typeof o=="object")t=false;else {let s=Array.from(this.deps.keys());for(let d of s){let i=this.deps.get(d),u=e.GetValueByPath(d);if(i!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=n;let c=++this.version,r=this.strategy.evaluate(e,c);if(!(r instanceof Promise)){let{res:l,version:o}=r;return this.finalizeSync(l,o,e,n)}return this.pendingPromise=(async()=>{try{let{res:l,version:o}=await r;return this.finalizeSync(l,o,e,n)}catch(l){throw {path:this.path,error:l}}finally{this.promiseToken===n&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,n,t,c){return c!==this.promiseToken||n<this.version?this.cache:(this.cache=e,this.deps.forEach((r,l)=>{this.deps.set(l,t.GetValueByPath(l));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}};var te=(a,e,n)=>{let c=r=>{let l=n.triggerPaths.map(d=>r.GetValueByPath(d).value),o=Object.create(null);return Object.defineProperty(o,"triggerTargets",{get:()=>l||[]}),Object.defineProperty(o,"affectedTatget",{get:()=>r.GetRenderSchemaByPath(a)[e]}),n.logic({slot:o})};return {value:n.value,targetPath:a,triggerPaths:n.triggerPaths,priority:n.priority??10,logic:c}},ne=(a,e,n,t)=>{if(!a)throw Error("");let c=a,r=(s,d)=>{e.has(s)||e.set(s,new Set),e.get(s).add(d),n.has(d)||n.set(d,new Set),n.get(d).add(s);};return {SetRule:(s,d,i,u={logic:()=>{}})=>{let f=c(d),p=te(d,i,{...u,triggerPaths:[s]}),P=[s].map(g=>[g,c(g).state.value]);if(r(s,d),f.nodeBucket[i])f.nodeBucket[i].setRule(p,P),u.effect&&f.nodeBucket[i].setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]});else {let g=f.meta[i],m=new X(g,i,d);m.setRule(p,P),u.effect&&m.setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]}),f.nodeBucket[i]=m;}f.state[i]=f.meta[i],u.forceNotify&&f.nodeBucket[i].forceNotify();},SetRules:(s,d,i,u={logic:()=>{}})=>{let f=c(d);for(let g of s)r(g,d);let p=te(d,i,{...u,triggerPaths:s}),P=s.map(g=>[g,c(g).state.value]);if(f.nodeBucket[i])f.nodeBucket[i].setRules(p,P),u.effect&&f.nodeBucket[i].setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]});else {let g=f.meta[i],m=new X(g,i,d);m.setRules(p,P),u.effect&&m.setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]}),f.nodeBucket[i]=m;}f.state[i]=f.meta[i],u.forceNotify&&f.nodeBucket[i].forceNotify();}}};var re=a=>{let e=a||void 0;if(!e)throw Error("");return {SetStrategy:(t,c,r)=>{e(t).nodeBucket[c].setStrategy(r);}}};function se(){let a=new Map,e=new Map,n=new Set,t=(l,o)=>{a.set(l,o);let s=e.get(l);s&&s(o);};return {SetTrace:(l,o)=>{e.set(l,o);let s=a.get(l)||"idle";return o(s),{cancel:()=>{e.delete(l);}}},useTrace:()=>({apply:o=>{o.on("flow:start",()=>{n.forEach(s=>t(s,"idle")),n.clear(),a.clear();}),o.on("node:release",({path:s,type:d})=>{(d==1||d==2)&&(n.add(s),t(s,"pending"));}),o.on("node:pending",({path:s})=>{n.add(s),(!a.has(s)||a.get(s)==="idle")&&t(s,"pending");}),o.on("node:start",({path:s})=>{n.add(s),t(s,"calculating");}),o.on("node:success",({path:s})=>{t(s,"calculated");}),o.on("node:intercept",({path:s,type:d})=>{d==3&&t(s,"calculating"),d==6&&t(s,"idle");}),o.on("node:stagnate",({path:s})=>{t(s,"pending");}),o.on("node:error",({path:s})=>t(s,"error"));}})}}function ae(a,e,n,t){let c=i=>{let u=a(),f=e(),p=new Set;return u.get(i)?.forEach(P=>p.add(P)),p.size===0?[]:Array.from(p).filter(P=>{let g=f.get(P)||new Set;return !Array.from(g).some(N=>p.has(N))})};return {GetNextDependency:i=>{let u=t();return Array.from(u.get(i)||[])},GetPrevDependency:i=>{let u=n();return Array.from(u.get(i)||[])},GetAllPrevDependency:i=>{let u=e();return Array.from(u.get(i)||[])},GetAllNextDependency:i=>{let u=a();return Array.from(u.get(i)||[])},rebuildDirectDependencyMaps:i=>{let u=new Map,f=new Map;for(let p of i){let P=c(p);u.set(p,new Set(P));for(let g of P)f.has(g)||f.set(g,new Set),f.get(g).add(p);}return {directNextMap:u,directPrevMap:f}}}}function oe(a){let e=t=>{let c=[],r=[],l=new Map,o=t.size,s=0,d=0;for(let[i,u]of t)u===0&&r.push(i);if(r.length===0&&o>0)throw Error("Circular dependency detected");for(;r.length>0;){c.push([...r]);let i=[];for(let u of r){s++,l.set(u,d);let f=a.get(u);if(f)for(let p of f){let P=t.get(p)-1;t.set(p,P),P===0&&i.push(p);}}r=i,d++;}if(s<o)throw Error("Circular dependency detected");return {steps:c,levelMap:l}};return ()=>{let t=new Map;for(let c of a.keys()){let r=Array.from(a.get(c)||[]);t.has(c)||t.set(c,0);for(let l of r){let o=t.get(l)||0;t.set(l,++o);}}return e(t)}}var q=()=>{let a=[];return {on:e=>(a.push(e),()=>{let n=a.indexOf(e);n>-1&&a.splice(n,1);}),call:e=>a.forEach(n=>{n(e);})}};function ie(){let{on:a,call:e}=q();return {onError:a,callOnError:e}}function le(){let{on:a,call:e}=q();return {onSuccess:a,callOnSuccess:e}}var ce=()=>{let a=new Set,e=new Map,n=(r,l)=>{e.get(r)?.forEach(o=>o(l));},t=(r,l)=>(e.has(r)||e.set(r,new Set),e.get(r).add(l),()=>e.get(r).delete(l));return {usePlugin:r=>{let l=new Set,o=(s,d)=>{let i=t(s,d);return l.add(i),i};return r.apply({on:o}),a.add(r),()=>{l.forEach(s=>s()),l.clear(),a.delete(r);}},emit:n}};function ue(){let{on:a,call:e}=q();return {onStart:a,callOnStart:e}}var de=(a={frameQuota:12})=>{let e=performance.now(),n=0,t=false,c=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),n=0,t=true;},shouldYield(){let r=performance.now();return n++,!!((n>=5||t)&&(n=0,r-e>a.frameQuota||c()))},async yieldToMain(){return new Promise(r=>{Pe(()=>{e=performance.now(),n=0,t&&(t=false),r();});})}}},Pe=a=>{let{port1:e,port2:n}=new MessageChannel;e.onmessage=a,n.postMessage(null);};function fe(a,e,n,t,c){let r=new Map,l=a.useGreedy,o=de();return async(d,i)=>{let f=Symbol("token"),p=d||"__NOTIFY_ALL__";r.set(p,f);let P=false;o.reset();let g=new Set,m=new Set,_=new Set;i.forEach(I=>{_.add(I),e.GetAllNextDependency(I).forEach(v=>_.add(v));});let N=new Map,C=new Map,M=new Set,L=e.GetPathToLevelMap(),A=0,z=0,H=I=>{e.GetAllNextDependency(I).forEach($=>{let G=L.get($)||0;G>z&&(z=G);});};d?(A=L.get(d)??0,H(d),g.add(d)):i.forEach(I=>H(I)),i.forEach(I=>{M.add(I);});let y=performance.now();t.emit("flow:start",{path:p}),t.callOnStart({path:p});let R=false,O=30,Q=I=>{let{target:v,trigger:$}=I,G=false,D=false,k=n.GetNodeByPath(v),W=[],E=(h,T)=>{if(g.has(h)||m.has(h)||M.has(h))return;let K=0,x=L.get(h)??0;if(N.has(h))K=N.get(h)-1;else {if(x>A&&N.size>O){C.has(x)||C.set(x,new Set),C.get(x).add(h),t.emit("node:intercept",{path:h,type:7});return}let F=e.GetPrevDependency(h),S=0;for(let b of F){if(g.has(b))continue;(L.get(b)??0)>A&&S++;}K=S;}if(K<=0){let F=M.has(h),S=m.has(h);if(F||S){t.emit("node:intercept",{path:h,type:S?3:3.1});return}N.delete(h),M.add(h),t.emit("node:release",{path:h,type:T,detail:{path:v}});}else N.set(h,K);},B=(h=[])=>{if(r.get(p)!==f)return;if(h.length){let x={},F=k.proxy;for(let S of h){let b=(S.args||[]).reduce((U,Y)=>(U[Y]=F[Y],U),{});try{let U=S.fn(b);U&&typeof U=="object"&&Object.assign(x,U);}catch(U){}}for(let S in x)if(S in k.state)k.state[S]=x[S];else throw {error:`wrong effect in ${k.path}`};G=true;}G&&c.flushPathSet.add(v),t.emit("node:success",{path:v}),g.add(v);let T=e.GetNextDependency(v);(G||D)&&e.GetAllNextDependency(v).forEach(F=>_.add(F));for(let x of T){if(g.has(x)){t.emit("node:intercept",{path:x,type:2});continue}if(m.has(x)||M.has(x)){t.emit("node:intercept",{path:x,type:m.has(x)?3:3.1});continue}if(G||D)E(x,1);else if(N.has(x))E(x,2);else {let S=L.get(x);C.has(S)||C.set(S,new Set);let b=C.get(S);b.has(x)||(b.add(x),t.emit("node:stagnate",{path:x,type:1}));}}m.delete(v),(async()=>{if(!P){let x=m.size,F=M.size;t.emit("flow:fire",{path:v,type:1,detail:{active:x,pending:F,blocked:N.size}}),j();}})();},V=h=>{t.emit("node:error",{path:v,error:h});let T=Symbol("abort");r.set(p,T),M.clear(),N.clear(),m.clear(),t.callOnError(h);},w=(h,T)=>{let K=false;h!==k[T]&&(k[T]=h,k.state[T]=h,G=true,t.emit("node:bucket:success",{path:v,key:T,value:h}),T==="value"&&(K=true)),k.nodeBucket[T].isForceNotify()&&(D=true),(K||D)&&H(v);};t.emit("node:start",{path:v});try{let h=[];for(let T in k.nodeBucket){let K=k.nodeBucket[T];h.push(...K.getSideEffect());let x=K.evaluate({affectKey:T,triggerPath:$,GetRenderSchemaByPath:F=>n.GetNodeByPath(F).proxy,GetValueByPath:F=>n.GetNodeByPath(F).state,GetToken:()=>f});if(x instanceof Promise){let F=x.then(S=>{r.get(p)===f&&w(S,T);});W.push(F);}else w(x,T);}if(W.length>0)return Promise.all(W).then(()=>{B(h);}).catch(V);B(h);return}catch(h){V(h);}},j=async()=>{if(r.get(p)!==f){P=false;return}P=true;let I=o.getIsFirstFrame(),v=0,$=()=>l&&I?30:1/0,G=0,D=$();try{for(;r.get(p)===f;){let k=G>=D,W=o.shouldYield();if(k||W){if(G>0&&(v++,(I||v%2===0)&&c.requestUpdate()),await o.yieldToMain(),r.get(p)!==f)break;G=0,I=o.getIsFirstFrame();}if(M.size>0&&m.size<5){for(let E of M){if(m.size>=5||G>=D)break;let B=L.get(E)??0,V=e.GetPrevDependency(E),w=V.length>1;if((!l||w)&&B>A){M.delete(E);let T=V.filter(K=>_.has(K)&&!g.has(K)).length;N.set(E,T||0),t.emit("node:intercept",{path:E,type:T>0?4:5,detail:{targetLevel:B,currentLevel:A,pendingParentsCount:T}});continue}if(M.delete(E),m.add(E),t.emit("node:processing",{path:E}),Q({target:E,trigger:d}),G++,G>=D||o.shouldYield())break}if(M.size>0)continue}if(G<D&&l&&N.size>0&&m.size<5){let E=!1,B=0;for(let[V,w]of N)if(w<=0){let h=L.get(V)??0,T=e.GetPrevDependency(V);if(h>A&&T.length>1)continue;if(N.delete(V),M.add(V),B++,E=!0,t.emit("node:release",{path:V,type:4}),B>=D)break}if(B>0)continue;if(E){if(o.shouldYield()&&(await o.yieldToMain(),r.get(p)!==f))break;continue}}if(m.size===0&&M.size===0){let E=new Set;for(let w of C.keys())E.add(w);for(let[w]of N){let h=L.get(w)??0;h>A&&E.add(h);}let B=Array.from(E).sort((w,h)=>w-h),V=B[0];if(B.length>0&&V<=z){let w=B[0];if(w<=z){A=w;let h=C.get(w);h&&(h.forEach(T=>M.add(T)),C.delete(w));for(let[T]of N)(L.get(T)??0)===w&&(N.delete(T),M.add(T),t.emit("node:release",{path:T,type:3,detail:{level:w}}));continue}}else {C.forEach((w,h)=>{w.forEach(T=>{g.add(T),t.emit("node:intercept",{path:T,type:6});});}),C.clear();for(let[w]of N)g.add(w),t.emit("node:intercept",{path:w,type:6});N.clear();break}}M.size>0&&m.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(P=false,m.size+N.size+M.size===0){if(r.get(p)===f&&!R){R=true,t.emit("flow:end",{type:1}),c.requestUpdate();let W=performance.now();t.emit("flow:success",{duration:(W-y).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:m.size}});}};j();}}function ee(a){let{path:e,uid:n,type:t,meta:c,dirtySignal:r,triggerUI:l,state:o}=a,s=null,i={path:e,uid:n,type:t,meta:c,dirtySignal:r,createView:(u={})=>{let f=new Proxy(u,{get(p,P){let g=P;return g in a.state?a.state[g]:g in a?a[g]:c&&g in c?c[g]:Reflect.get(p,P)},set(p,P,g){let m=P;return m in a.state?(a.state[m]=g,l(),true):Reflect.set(p,P,g)},ownKeys(p){let P=new Set([...Reflect.ownKeys(p),...Object.keys(o||{}),...Object.keys(c||{})]);return Array.from(P)},getOwnPropertyDescriptor(p,P){return o&&P in o||c&&P in c?{enumerable:true,configurable:true}:Reflect.getOwnPropertyDescriptor(p,P)}});return s=f,f}};return "children"in a?{...i,children:a.children}:{...i,state:a.state,nodeBucket:a.nodeBucket,notifyKeys:a.notifyKeys,dependOn:a.dependOn,get proxy(){return s}}}function ye(a,e,n,t,c,r){let l=0,o=new Map,s=new Map,d=new Map,i=false,u=new Set,g=async()=>{let y=Array.from(u);u.clear();for(let R of y){let O=M(R);r.signalTrigger(O.dirtySignal);}},m=()=>{i||(i=true,requestAnimationFrame(()=>{try{for(;u.size>0;)g();}finally{i=false;}}));},_=fe({useGreedy:e.useGreedy},n,{GetNodeByPath:M},c,{requestUpdate:m,flushPathSet:u}),N=y=>{if(o.has(y.path))throw new Error(`[MeshFlow] Duplicate Path: ${String(y.path)}`);let R=++l,O={path:y.path,getNode:I=>M(I)},Q=I=>{let v=I({...O}),$=M(y.path);if(t.createHistoryAction&&t.pushIntoHistory){let G=t.createHistoryAction([{path:y.path,value:$.state.value},{path:y.path,value:v}],D=>{let k=M(D.path);k.state.value=D.value,A(D.path);});t.pushIntoHistory(G);}$.state.value=v,A(y.path);},j=ee({uid:R,path:y.path,state:y.state,meta:y.meta,nodeBucket:y.nodeBucket,dirtySignal:y.dirtySignal,notifyKeys:y.notifyKeys,dependOn:Q,triggerUI:()=>r.signalTrigger(y.dirtySignal)});return o.set(j.path,R),s.set(R,j),j},C=y=>{if(o.has(y.path))throw new Error(`[MeshFlow] Duplicate Path: ${String(y.path)}`);let R=++l,O=ee({uid:R,path:y.path,state:{},meta:y,nodeBucket:{},children:y.children});return o.set(O.path,R),d.set(R,O),O};function M(y){let R=o.get(y),O=s.get(R);if(!O)throw Error("wrong ID");return O}function L(y){let R=o.get(y);return d.get(R)}let A=y=>{if(!M(y))throw Error("Node undefined");u.add(y),m();let O=n.GetNextDependency(y);z(O,y);};function z(y,R){_(R,y);}return {registerNode:N,registerGroupNode:C,GetNodeByPath:M,GetGroupByPath:L,notify:A,notifyAll:async()=>{Promise.resolve().then(async()=>{let y=n.GetDependencyOrder();if(!y||y.length===0)return;let R=y[0];try{_(null,R);}catch(O){throw c.callOnError(O),O}finally{m();}});},UITrigger:r,UidToNodeMap:s}}function pe(a,e){let n=false,t=false,c=new Map,r=new Map,l=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:u,GetPrevDependency:f,GetAllPrevDependency:p,GetAllNextDependency:P,rebuildDirectDependencyMaps:g}=ae(()=>c,()=>r,()=>o,()=>l),m={},_={};if(e.modules.useHistory){let{Undo:S,Redo:b,PushIntoHistory:U,CreateHistoryAction:Y,initCanUndo:he,initCanRedo:ge}=e.modules.useHistory();m.pushIntoHistory=U,m.createHistoryAction=Y,_={Undo:S,Redo:b,initCanUndo:he,initCanRedo:ge};}let{onError:N,callOnError:C}=ie(),{onSuccess:M,callOnSuccess:L}=le(),{onStart:A,callOnStart:z}=ue(),{emit:H,usePlugin:y}=ce(),{SetTrace:R,useTrace:O}=se(),Q=O();y(Q);let j=ye(a,{useGreedy:e.config.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:P,GetNextDependency:u,GetPrevDependency:f,GetAllPrevDependency:p,GetPathToLevelMap:()=>d},m,{callOnError:C,callOnSuccess:L,callOnStart:z,emit:H},e.UITrigger),{GetGroupByPath:I,GetNodeByPath:v,notifyAll:$}=j,G={};if(e.modules.useInternalForm){let{uiSchema:S,GetFormData:b}=e.modules.useInternalForm(j,a);G={uiSchema:S,GetFormData:b};}let D={};if(e.modules.useSchemaValidators){let{SetValidators:S}=e.modules.useSchemaValidators(v);D={SetValidators:S};}let{SetRule:k,SetRules:W}=ne(v,c,r),{SetStrategy:E}=re(v),B=oe(c),V=()=>{let S=B();s=S.steps,d=S.levelMap;},w=()=>{t||(t=true,Promise.resolve().then(()=>{if(V(),n){let{directNextMap:S,directPrevMap:b}=g(s.flat());l=S,o=b;}}).finally(()=>{t=false,n=false;}));};return {SetRule:(S,b,U,Y)=>{k(S,b,U,Y),n=true,w();},SetRules:(S,b,U,Y)=>{W(S,b,U,Y),n=true,w();},SetStrategy:E,SetTrace:R,usePlugin:y,SetValue:(S,b)=>{v(S).dependOn(()=>b);},GetValue:(S,b="value")=>v(S)[b],GetGroupByPath:I,notifyAll:async()=>{V(),await $();},GetAllDependency:()=>c,GetDependencyOrder:()=>s,historyExports:_,formExports:G,validatorExports:D,onError:N,onSuccess:M,onStart:A}}var J=new Map,me=(a,e,n)=>{try{if(typeof n.UITrigger.signalCreator!="function"||typeof n.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(J.has(a))throw Error("engineID repeated");let t=pe(e,{config:n.config||{useGreedy:!1},UITrigger:n.UITrigger,modules:n.modules||{},plugins:{}}),{SetRule:c,SetRules:r,SetStrategy:l,SetValue:o,GetValue:s,usePlugin:d,GetGroupByPath:i,notifyAll:u,SetTrace:f,GetAllDependency:p,GetDependencyOrder:P,historyExports:g,formExports:m,validatorExports:_,onError:N,onSuccess:C,onStart:M}=t,A={...{config:{SetRule:c,SetRules:r,SetStrategy:l,notifyAll:u,SetTrace:f,usePlugin:d},data:{SetValue:o,GetValue:s,GetGroupByPath:i},dependency:{GetAllDependency:p,GetDependencyOrder:P},hooks:{onError:N,onSuccess:C,onStart:M}}},z=n.modules;return z&&Object.keys(z).forEach(H=>{let y=H;if(y.startsWith("use")){let R=y.slice(3);y=R.charAt(0).toLowerCase()+R.slice(1);}H==="useHistory"&&g&&Object.keys(g).length>0&&(A[y]=g),H==="useInternalForm"&&Object.keys(m).length>0&&(A[y]=m),H==="useSchemaValidators"&&Object.keys(_).length>0&&(A[y]=_);}),J.set(a,A),A}catch(t){throw Error(t)}},Ze=()=>(a,e,n)=>Se(a,e,n),et=a=>{let e=J.get(a);if(e)return e;throw Error("[MeshFlow] Engine ID not found.")},tt=a=>{J.delete(a);},Se=me;exports.deleteEngine=tt;exports.useEngine=et;exports.useEngineManager=me;exports.useMeshFlow=Se;exports.useMeshFlowDefiner=Ze;
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var ne=class{computedRules=[];store={OR:(e,r)=>{let t,l,n=this.computedRules;for(let i=0;i<n.length;i++){let a=n[i],o=a.logic(e);if(o instanceof Promise)return (async()=>{let d=await o;if(a.entityId==="__base__"?l=d:d&&(t=a.value),typeof t>"u")for(let u=i+1;u<n.length;u++){let y=n[u],S=y.logic(e),f=S instanceof Promise?await S:S;if(y.entityId==="__base__"){l=f;continue}if(f){t=y.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}})();let s=o;if(a.entityId==="__base__"){l=s;continue}if(s){t=a.value;break}}return typeof t>"u"&&(t=l),{res:t,version:r}},PRIORITY:(e,r)=>{let t=null,l=this.computedRules;for(let n=0;n<l.length;n++){let a=l[n].logic(e);if(a instanceof Promise)return (async()=>{let o=await a;if(o!==void 0)return {res:o,version:r};for(let s=n+1;s<l.length;s++){let d=l[s].logic(e),u=d instanceof Promise?await d:d;if(u!==void 0)return {res:u,version:r}}return {res:void 0,version:r}})();if(a!==void 0)return {res:a,version:r}}return {res:t,version:r}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat().sort((r,t)=>t.priority-r.priority):this.computedRules=Array.from(e.values()).map(r=>Array.from(r)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,r){return this.CurrentStrategy(e,r)}},$=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,r,t){let l=()=>this.rules;this.strategy=new ne(l),this.path=t,this.isValue=r==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}updateInputValueRule(e){this.isValue&&this.setRule({priority:1,entityId:"__input_value__",logic:()=>e});}setDefaultRule(e){let r=new Set;r.add(e),this.rules.set(e.id,r);}setRules(e,r){r&&this.updateDeps(r);let t=++this.id,l={...e,entityId:t};for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[r,t]of e)this.deps.set(r,t);}setRule(e,r){if(r&&this.updateDeps(r),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,l={...e,entityId:t};if(e)for(let n of e.triggerPaths)this.rules.has(n)||this.rules.set(n,new Set),this.rules.get(n).add(l);return this.strategy.updateComputedRules(),()=>{for(let n of e.triggerPaths){let i=this.rules.get(n);i&&(i.delete(l),i.size===0&&(this.rules.delete(n),this.deps.delete(n)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let r=null;if(e.GetToken&&(r=e.GetToken()),this.pendingPromise&&this.promiseToken!==r&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let i=this.deps.get(e.triggerPath),a=e.GetValueByPath(e.triggerPath);if(typeof i=="object"||typeof a=="object")t=false;else {let o=Array.from(this.deps.keys());for(let s of o){let d=this.deps.get(s),u=e.GetValueByPath(s);if(d!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=r;let l=++this.version,n=this.strategy.evaluate(e,l);if(!(n instanceof Promise)){let{res:i,version:a}=n;return this.finalizeSync(i,a,e,r)}return this.pendingPromise=(async()=>{try{let{res:i,version:a}=await n;return this.finalizeSync(i,a,e,r)}catch(i){throw {path:this.path,error:i}}finally{this.promiseToken===r&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,r,t,l){return l!==this.promiseToken||r<this.version?this.cache:(this.cache=e,this.deps.forEach((n,i)=>{this.deps.set(i,t.GetValueByPath(i));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}},te=class{validators=[];defaultValidators=[];path="";constructor(e){this.path=e,this.SetDefaultValidators();}setValidators(e){this.validators.push(e);}SetDefaultValidators(){let e={logic:t=>t||typeof t=="number"?true:`${this.path} undefined`,condition:t=>!!t.required},r={logic:function(t){return t.length>this.options.maxLength?`Too long:${this.options.maxLength}`:true},condition:function(t){return typeof t.maxLength!="number"?false:(r.options={maxLength:t.maxLength},t.type==="input"&&t.hidden===false)},options:{}};this.defaultValidators.push(e),this.defaultValidators.push(r);}evaluate(e,r){let t=true,l=[...this.defaultValidators,...this.validators];for(let n of l){if(!n.condition(r))continue;let a=n.logic(e);if(typeof a!="boolean"){t=a;break}}return t}};var ae=(c={frameQuota:12})=>{let e=performance.now(),r=0,t=false,l=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),r=0,t=true;},shouldYield(){let n=performance.now();return r++,!!((r>=5||t)&&(r=0,n-e>c.frameQuota||l()))},async yieldToMain(){return new Promise(n=>{Pe(()=>{e=performance.now(),r=0,t&&(t=false),n();});})}}},Pe=c=>{let{port1:e,port2:r}=new MessageChannel;e.onmessage=c,r.postMessage(null);};function oe(c,e,r,t,l){let n=new Map,i=c.useGreedy,a=ae();return async(s,d)=>{let y=Symbol("token");n.set(s,y);let S=false;a.reset();let f=new Set,R=new Set,C=new Set(e.GetAllNextDependency(s));C.add(s);let E=new Map,N=new Map,w=new Set,V=e.GetPathToLevelMap(),_=V.get(s)??0,z=0,q=T=>{e.GetAllNextDependency(T).forEach(F=>{let A=V.get(F)||0;A>z&&(z=A);});};q(s),d.forEach(T=>{w.add(T);}),f.add(s);let W=performance.now();t.emit("flow:start",{path:s}),t.callOnStart({path:s});let H=false,J=30,Z=T=>{let{target:x,trigger:F}=T,A=false,O=false,k=r.GetRenderSchemaByPath(x),M=[],m=(p,g)=>{if(f.has(p)||R.has(p)||w.has(p))return;let D=0,v=V.get(p)??0;if(E.has(p))D=E.get(p)-1;else {if(v>_&&E.size>J){N.has(v)||N.set(v,new Set),N.get(v).add(p),t.emit("node:intercept",{path:p,type:7});return}let I=e.GetPrevDependency(p),B=0;for(let U of I){if(f.has(U))continue;(V.get(U)??0)>_&&B++;}D=B;}if(D<=0){let I=w.has(p),B=R.has(p);if(I||B){t.emit("node:intercept",{path:p,type:B?3:3.1});return}E.delete(p),w.add(p),t.emit("node:release",{path:p,type:g,detail:{path:x}});}else E.set(p,D);},b=(p=[])=>{if(n.get(s)!==y)return;if(p.length){let v={};for(let I of p){let B=(I.args||[]).reduce((U,ee)=>(U[ee]=k[ee],U),{});try{let U=I.fn(B);U&&typeof U=="object"&&Object.assign(v,U);}catch(U){}}for(let I in v)k[I]=v[I];A=true;}A&&l.flushPathSet.add(x),t.emit("node:success",{path:x}),f.add(x);let g=e.GetNextDependency(x);(A||O)&&e.GetAllNextDependency(x).forEach(I=>C.add(I));for(let v of g){if(f.has(v)){t.emit("node:intercept",{path:v,type:2});continue}if(R.has(v)||w.has(v)){t.emit("node:intercept",{path:v,type:R.has(v)?3:3.1});continue}if(A||O)m(v,1);else if(E.has(v))m(v,2);else {let B=V.get(v);N.has(B)||N.set(B,new Set);let U=N.get(B);U.has(v)||(U.add(v),t.emit("node:stagnate",{path:v,type:1}));}}R.delete(x),(async()=>{if(!S){let v=R.size,I=w.size;t.emit("flow:fire",{path:x,type:1,detail:{active:v,pending:I,blocked:E.size}}),h();}})();},G=p=>{t.emit("node:error",{path:x,error:p});let g=Symbol("abort");n.set(s,g),w.clear(),E.clear(),R.clear(),t.callOnError(p);},P=(p,g)=>{let D=false;p!==k[g]&&(k[g]=p,A=true,t.emit("node:bucket:success",{path:x,key:g,value:p}),g==="value"&&(D=true)),k.nodeBucket[g].isForceNotify()&&(O=true),(D||O)&&q(x);};t.emit("node:start",{path:x});try{let p=[];for(let g in k.nodeBucket){let D=k.nodeBucket[g];p.push(...D.getSideEffect());let v=D.evaluate({affectKey:g,triggerPath:F,GetRenderSchemaByPath:r.GetRenderSchemaByPath,GetValueByPath:I=>r.GetRenderSchemaByPath(I).value,GetToken:()=>y});if(v instanceof Promise){let I=v.then(B=>{n.get(s)===y&&P(B,g);});M.push(I);}else P(v,g);}if(M.length>0)return Promise.all(M).then(()=>{b(p);}).catch(G);b(p);return}catch(p){G(p);}},h=async()=>{if(n.get(s)!==y){S=false;return}S=true;let T=a.getIsFirstFrame(),x=0,F=()=>i&&T?30:1/0,A=0,O=F();try{for(;n.get(s)===y;){let k=A>=O,M=a.shouldYield();if(k||M){if(A>0&&(x++,(T||x%2===0)&&l.requestUpdate()),await a.yieldToMain(),n.get(s)!==y)break;A=0,T=a.getIsFirstFrame();}if(w.size>0&&R.size<5){for(let m of w){if(R.size>=5||A>=O)break;let b=V.get(m)??0,G=e.GetPrevDependency(m),P=G.length>1;if((!i||P)&&b>_){w.delete(m);let g=G.filter(D=>C.has(D)&&!f.has(D)).length;E.set(m,g||0),t.emit("node:intercept",{path:m,type:g>0?4:5,detail:{targetLevel:b,currentLevel:_,pendingParentsCount:g}});continue}if(w.delete(m),R.add(m),t.emit("node:processing",{path:m}),Z({target:m,trigger:s}),A++,A>=O||a.shouldYield())break}if(w.size>0)continue}if(A<O&&i&&E.size>0&&R.size<5){let m=!1,b=0;for(let[G,P]of E)if(P<=0){let p=V.get(G)??0,g=e.GetPrevDependency(G);if(p>_&&g.length>1)continue;if(E.delete(G),w.add(G),b++,m=!0,t.emit("node:release",{path:G,type:4}),b>=O)break}if(b>0)continue;if(m){if(a.shouldYield()&&(await a.yieldToMain(),n.get(s)!==y))break;continue}}if(R.size===0&&w.size===0){let m=new Set;for(let P of N.keys())m.add(P);for(let[P]of E){let p=V.get(P)??0;p>_&&m.add(p);}let b=Array.from(m).sort((P,p)=>P-p),G=b[0];if(b.length>0&&G<=z){let P=b[0];if(P<=z){_=P;let p=N.get(P);p&&(p.forEach(g=>w.add(g)),N.delete(P));for(let[g]of E)(V.get(g)??0)===P&&(E.delete(g),w.add(g),t.emit("node:release",{path:g,type:3,detail:{level:P}}));continue}}else {N.forEach((P,p)=>{P.forEach(g=>{f.add(g),t.emit("node:intercept",{path:g,type:6});});}),N.clear();for(let[P]of E)f.add(P),t.emit("node:intercept",{path:P,type:6});E.clear();break}}w.size>0&&R.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(S=false,R.size+E.size+w.size===0){if(n.get(s)===y&&!H){H=true,t.emit("flow:end",{type:1}),l.requestUpdate();let M=performance.now();t.emit("flow:success",{duration:(M-W).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:R.size}});}};h();}}function se(c,e,r,t,l,n){let i=ve(c),a=0,o=new Map,s=new Map,d=new Map,u=false,y=new Set,S=false,f=true,R=null,C=h=>{let T=o.get(h);return s.get(T)},E=h=>d.get(h),N=async()=>{let h=Array.from(y);y.clear();for(let T of h){let x=C(T);n.signalTrigger(x.dirtySignal);}},w=()=>{u||(u=true,requestAnimationFrame(()=>{try{for(;y.size>0;)N();}finally{u=false;}}));},V=()=>{let h=(T,x,F)=>{if(typeof T!="object"||T===null||Array.isArray(T)){if(F.length>0){let O=F[F.length-1];x[O]=C(F.join(".")).value;}return}let A=Object.getOwnPropertyNames(T);for(let O of A)F.push(O),h(T[O],T,F),F.pop();};return h(i,null,[]),i},Y=oe({useGreedy:e.useGreedy},r,{GetRenderSchemaByPath:C},l,{requestUpdate:w,flushPathSet:y}),_=async()=>(S&&R||(S=true,R=(async()=>{let h=r.GetDependencyOrder(),T=performance.now(),x=performance.now();try{for(let A=0;A<h.length;A++){let O=h[A];await Promise.all(O.map(async k=>{let M=C(k),m=!1;for(let b in M.nodeBucket){let G=await M.nodeBucket[b].evaluate({affectKey:b,triggerPath:void 0,GetRenderSchemaByPath:C,GetValueByPath:P=>C(P).value,isSameToken:()=>!0});if(b==="options"){let P=!1,p=M.value;for(let g of G)if(g.value==p){P=!0;break}P||(M.value=void 0,m=!0);}G!==M[b]&&(M[b]=G,m=!0);}m&&y.add(k);})),performance.now()-x>12&&(await new Promise(k=>requestAnimationFrame(k)),x=performance.now());}y.size>0&&w(),f=!1;let F=performance.now();l.emit("flow:success",{duration:(F-T).toFixed(2)+"ms"}),l.callOnSuccess();}catch(F){throw l.emit("node:error",{path:F.path,error:F.error}),l.callOnError(F),F}finally{S=false,R=null,f=false;}})()),R),z=h=>{if(f)return;if(!C(h))throw Error("Node undefined");performance.now();y.add(h),w();let F=r.GetNextDependency(h);q(F,h);};function q(h,T){Y(T,h);}let W=h=>{if(!h)throw Error("path error");let T=C(h);T.nodeBucket.value&&T.nodeBucket.value.updateInputValueRule(T.value);},H=(h,T="")=>{let x="name"in h?h.name:void 0,F=x?T===""?x:`${T}.${x}`:T,A=n.signalCreateor(),O=a++,k={getRenderSchema:b=>C(b)},M=(b,G)=>{let P=b(G),p=C(G.path),g=t.createHistoryAction([{path:G.path,value:p.value},{path:G.path,value:P}],D=>{let v=C(D.path);v.value=D.value,W(D.path),z(D.path);});p.value=P,t.pushIntoHistory(g),W(G.path),z(G.path);},m={...h,disabled:!!h.disabled,hidden:"hidden"in h?h.hidden:false,readonly:"readonly"in h?h.readonly:false,required:"required"in h?h.required:false,path:F,dirtySignal:A,uid:O,nodeBucket:{},validators:new te(F),theme:"secondary",dependOn:b=>M(b,{...k,path:F})};return h.type==="group"&&(delete m.nodeBucket,delete m.validators,m.children=h.children.map(b=>H(b,F)),d.set(m.path,m)),o.set(m.path,m.uid),s.set(m.uid,m),m};return {schema:H(c),GetFormData:()=>V(),GetRenderSchemaByPath:C,GetGroupByPath:E,notifyAll:_,convertToRenderSchema:H}}function ve(c,e={}){let r=n=>{if(n.type=="group")return {key:n.name||"",isGroup:true,val:n.children.reduce((i,a)=>[...i,r(a)],[])};if(n.type=="input"||n.type=="number"||n.type=="select"||n.type=="checkbox")return {key:n.name,isGroup:false,val:n.value};throw Error(`undefined type:${n.type}`)},t=(n,i)=>{if(i.isGroup){let a={};i.key===""?a=n:n[i.key]=a,i.val.forEach(o=>{t(a,o);});}else n[i.key]=i.val;},l=r(c);return t(e,l),e}var ie=(c,e,r)=>{let l=n=>{let i=r.triggerPaths.map(s=>n.GetValueByPath(s)),a=Object.create(null);return Object.defineProperty(a,"triggerTargets",{get:()=>i}),Object.defineProperty(a,"affectedTatget",{get:()=>n.GetRenderSchemaByPath(c)[e]}),r.logic({slot:a})};return {value:r.value,targetPath:c,triggerPaths:r.triggerPaths,priority:r.priority??10,logic:l}},le=(c,e,r)=>{if(!c)throw Error("");let t=c,l=(a,o)=>{e.has(a)||e.set(a,new Set),e.get(a).add(o),r.has(o)||r.set(o,new Set),r.get(o).add(a);};return {SetRule:(a,o,s,d={logic:u=>{}})=>{let u=t(o),y=ie(o,s,{...d,triggerPaths:[a]}),S=[a].map(f=>[f,t(f).value]);if(l(a,o),u.nodeBucket[s])u.nodeBucket[s].setRule(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRule(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();},SetRules:(a,o,s,d={logic:u=>{}})=>{let u=t(o);for(let f of a)l(f,o);let y=ie(o,s,{...d,triggerPaths:a}),S=a.map(f=>[f,t(f).value]);if(u.nodeBucket[s])u.nodeBucket[s].setRules(y,S),d.effect&&u.nodeBucket[s].setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]});else {let f=new $(u[s],s,o);f.setRules(y,S),d.effect&&f.setSideEffect({fn:d.effect,args:d.effectArgs?d.effectArgs:[s]}),u.nodeBucket[s]=f;}d.forceNotify&&u.nodeBucket[s].forceNotify();}}},ce=c=>{let e=c||void 0;if(!e)throw Error("");return {SetStrategy:(t,l,n)=>{e(t).nodeBucket[l].setStrategy(n);}}};var ue=c=>{let e=c||void 0;return {SetValidators:(t,l)=>{let n=e(t),i=(o,s,d)=>o(s,d),a=(o,s,d)=>o(s,d);if(!n.validators)throw Error("validator init error");n.validators.setValidators({logic:o=>i(l.logic,o,e),condition:typeof l.condition=="function"?o=>a(l.condition,o,e):()=>true});}}};function de(){let c=new Map,e=new Map,r=new Set,t=(i,a)=>{c.set(i,a);let o=e.get(i);o&&o(a);};return {SetTrace:(i,a)=>{e.set(i,a);let o=c.get(i)||"idle";return a(o),()=>{e.delete(i);}},useTrace:()=>({apply:a=>{a.on("flow:start",()=>{r.forEach(o=>t(o,"idle")),r.clear(),c.clear();}),a.on("node:release",({path:o,type:s})=>{(s==1||s==2)&&(r.add(o),t(o,"pending"));}),a.on("node:pending",({path:o})=>{r.add(o),(!c.has(o)||c.get(o)==="idle")&&t(o,"pending");}),a.on("node:start",({path:o})=>{r.add(o),t(o,"calculating");}),a.on("node:success",({path:o})=>{t(o,"calculated");}),a.on("node:intercept",({path:o,type:s})=>{s==3&&t(o,"calculating"),s==6&&t(o,"idle");}),a.on("node:stagnate",({path:o})=>{t(o,"pending");}),a.on("node:error",({path:o})=>t(o,"error"));}})}}function pe(c,e,r,t){let l=d=>{let u=c(),y=e(),S=new Set;return u.get(d)?.forEach(f=>S.add(f)),S.size===0?[]:Array.from(S).filter(f=>{let R=y.get(f)||new Set;return !Array.from(R).some(N=>S.has(N))})};return {GetNextDependency:d=>{let u=t();return Array.from(u.get(d)||[])},GetPrevDependency:d=>{let u=r();return Array.from(u.get(d)||[])},GetAllPrevDependency:d=>{let u=e();return Array.from(u.get(d)||[])},GetAllNextDependency:d=>{let u=c();return Array.from(u.get(d)||[])},rebuildDirectDependencyMaps:d=>{let u=new Map,y=new Map;for(let S of d){let f=l(S);u.set(S,new Set(f));for(let R of f)y.has(R)||y.set(R,new Set),y.get(R).add(S);}return {directNextMap:u,directPrevMap:y}}}}function fe(c){let e=t=>{let l=[],n=[],i=new Map,a=t.size,o=0,s=0;for(let[d,u]of t)u===0&&n.push(d);if(n.length===0&&a>0)throw Error("Circular dependency detected");for(;n.length>0;){l.push([...n]);let d=[];for(let u of n){o++,i.set(u,s);let y=c.get(u);if(y)for(let S of y){let f=t.get(S)-1;t.set(S,f),f===0&&d.push(S);}}n=d,s++;}if(o<a)throw Error("Circular dependency detected");return {steps:l,levelMap:i}};return ()=>{let t=new Map;for(let l of c.keys()){let n=Array.from(c.get(l)||[]);t.has(l)||t.set(l,0);for(let i of n){let a=t.get(i)||0;t.set(i,++a);}}return e(t)}}function ye(){let c=[],e=[],t={canRedo:()=>{},canUndo:()=>{}},l=()=>{if(!c.length)return;let u=c.pop();u?.undoAction(),o(u);},n=()=>{if(!e.length)return;let u=e.pop();u?.redoAction(),s(u,false);},i=u=>{t.canUndo=()=>u(c.length);},a=u=>{t.canRedo=()=>u(e.length);},o=u=>{e.push(u),e.length>100&&e.shift(),t.canRedo(),t.canUndo();},s=(u,y=true)=>{y&&(e.length=0),c.push(u),c.length>100&&c.shift(),t.canUndo(),t.canRedo();};return {Undo:l,Redo:n,PushIntoHistory:s,CreateHistoryAction:(u,y)=>{let[S,f]=u;return {undoAction:()=>y(S),redoAction:()=>y(f)}},initCanUndo:i,initCanRedo:a}}var Q=()=>{let c=[];return {on:e=>(c.push(e),()=>{let r=c.indexOf(e);r>-1&&c.splice(r,1);}),call:e=>c.forEach(r=>r(e))}};function he(){let{on:c,call:e}=Q();return {onError:c,callOnError:e}}function me(){let{on:c,call:e}=Q();return {onSuccess:c,callOnSuccess:e}}var ge=()=>{let c=new Set,e=new Map,r=(n,i)=>{e.get(n)?.forEach(a=>a(i));},t=(n,i)=>(e.has(n)||e.set(n,new Set),e.get(n).add(i),()=>e.get(n).delete(i));return {usePlugin:n=>{let i=new Set,a=(o,s)=>{let d=t(o,s);return i.add(d),d};return n.apply({on:a}),c.add(n),()=>{i.forEach(o=>o()),i.clear(),c.delete(n);}},emit:r}};function Se(){let{on:c,call:e}=Q();return {onStart:c,callOnStart:e}}function Te(c,e,r){let t=false,l=false,n=new Map,i=new Map,a=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetAllNextDependency:R,rebuildDirectDependencyMaps:C}=pe(()=>n,()=>i,()=>o,()=>a),{Undo:E,Redo:N,PushIntoHistory:w,CreateHistoryAction:V,initCanUndo:Y,initCanRedo:_}=ye(),{onError:z,callOnError:q}=he(),{onSuccess:W,callOnSuccess:H}=me(),{onStart:J,callOnStart:Z}=Se(),{emit:h,usePlugin:T}=ge(),{SetTrace:x,useTrace:F}=de(),A=F();T(A);let{schema:O,GetFormData:k,GetRenderSchemaByPath:M,GetGroupByPath:m,notifyAll:b,convertToRenderSchema:G}=se(c,{useGreedy:e.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:R,GetNextDependency:y,GetPrevDependency:S,GetAllPrevDependency:f,GetPathToLevelMap:()=>d},{pushIntoHistory:w,createHistoryAction:V},{callOnError:q,callOnSuccess:H,callOnStart:Z,emit:h},r),P=(K,L)=>{let j=m(K),re=G(L,K);return j.children.push(re),j.dirtySignal.value++,re},{SetRule:p,SetRules:g}=le(M,n,i),{SetStrategy:D}=ce(M),{SetValidators:v}=ue(M),I=fe(n),B=()=>{let K=I();s=K.steps,d=K.levelMap;};return {schema:O,SetRule:(...K)=>{p.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetRules:(...K)=>{g.apply(null,K),t=true,!l&&new Promise((L,j)=>{l=true,L();}).then(()=>{if(B(),t){let{directNextMap:L,directPrevMap:j}=C(s.flat());a=L,o=j;}}).finally(()=>{l=false,t=false;});},SetStrategy:D,SetValidators:v,SetTrace:x,usePlugin:T,SetValue:(K,L)=>{M(K).dependOn(()=>L);},GetValue:(K,L="value")=>M(K)[L],GetFormData:k,GetGroupByPath:m,notifyAll:async()=>{B(),await b();},AddNewSchema:P,GetAllDependency:()=>n,GetDependencyOrder:()=>s,Undo:E,Redo:N,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:W,onStart:J}}var X=new Map,Re=(c,e,r)=>{try{if(typeof r.UITrigger.signalCreateor!="function"||typeof r.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(X.has(c))throw Error("engineID repeated");let t=Te(e,r.config||{useGreedy:!1},r.UITrigger),{schema:l,GetFormData:n,SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,SetValue:d,GetValue:u,usePlugin:y,GetGroupByPath:S,notifyAll:f,SetTrace:R,GetAllDependency:C,GetDependencyOrder:E,AddNewSchema:N,Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_,onError:z,onSuccess:q,onStart:W}=t,H={config:{SetRule:i,SetRules:a,SetStrategy:o,SetValidators:s,notifyAll:f,SetTrace:R,usePlugin:y},data:{schema:l,GetFormData:n,AddNewSchema:N,SetValue:d,GetValue:u,GetGroupByPath:S},history:{Undo:w,Redo:V,initCanUndo:Y,initCanRedo:_},dependency:{GetAllDependency:C,GetDependencyOrder:E},hooks:{onError:z,onSuccess:q,onStart:W}};return X.set(c,H),H}catch(t){throw Error(t)}},pt=c=>{if(X.has(c))return X.get(c);throw Error("id undefined")},ft=c=>{X.delete(c);},yt=Re;export{ft as deleteEngine,pt as useEngine,Re as useEngineManager,yt as useMeshFlow};
|
|
1
|
+
var Z=class{computedRules=[];store={OR:(e,n)=>{let t,c,r=this.computedRules;for(let l=0;l<r.length;l++){let o=r[l],s=o.logic(e);if(s instanceof Promise)return (async()=>{let i=await s;if(o.entityId==="__base__"?c=i:i&&(t=i),typeof t>"u")for(let u=l+1;u<r.length;u++){let f=r[u],p=f.logic(e),P=p instanceof Promise?await p:p;if(f.entityId==="__base__"){c=P;continue}if(P){t=f.value;break}}return typeof t>"u"&&(t=c),{res:t,version:n}})();let d=s;if(o.entityId==="__base__"){c=d;continue}if(d){t=o.value;break}}return typeof t>"u"&&(t=c),{res:t,version:n}},PRIORITY:(e,n)=>{let t,c=this.computedRules;for(let r=0;r<c.length;r++){let o=c[r].logic(e);if(o instanceof Promise)return (async()=>{let s=await o;if(s!==void 0)return {res:s,version:n};for(let d=r+1;d<c.length;d++){let i=c[d].logic(e),u=i instanceof Promise?await i:i;if(u!==void 0)return {res:u,version:n}}return {res:void 0,version:n}})();if(o!==void 0)return {res:o,version:n}}return {res:t,version:n}}};CurrentStrategy=()=>{};CurrentStrategyType="PRIORITY";getRules=()=>{};constructor(e){this.getRules=e,this.CurrentStrategy=this.store.PRIORITY,this.updateComputedRules();}updateComputedRules(){let e=this.getRules();this.CurrentStrategyType==="PRIORITY"?this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat().sort((n,t)=>t.priority-n.priority):this.computedRules=Array.from(e.values()).map(n=>Array.from(n)).flat();}setStrategy(e){this.CurrentStrategy=this.store[e],this.updateComputedRules();}evaluate(e,n){return this.CurrentStrategy(e,n)}},X=class{path;strategy;contract;rules=new Map;isValue=false;id=0;cache=void 0;pendingPromise=null;version=0;deps=new Map;_forceNotify=false;promiseToken=null;effectArray=[];constructor(e,n,t){let c=()=>this.rules;this.strategy=new Z(c),this.path=t,this.isValue=n==="value",this.contract=this.inferType(e),this.cache=e,this.setRule({priority:0,entityId:"__base__",logic:()=>e});}forceNotify(){this._forceNotify=true;}isForceNotify(){return this._forceNotify}setStrategy(e){this.strategy.setStrategy(e);}setDefaultRule(e){let n=new Set;n.add(e),this.rules.set("defaultRules",n);}setRules(e,n){n&&this.updateDeps(n);let t=++this.id,c={...e,entityId:t};for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(c);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let l=this.rules.get(r);l&&(l.delete(c),l.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}updateDeps(e){for(let[n,t]of e)this.deps.set(n,t);}setRule(e,n){if(n&&this.updateDeps(n),typeof e.entityId=="string"){this.setDefaultRule(e);return}let t=++this.id,c={...e,entityId:t};if(e)for(let r of e.triggerPaths)this.rules.has(r)||this.rules.set(r,new Set),this.rules.get(r).add(c);return this.strategy.updateComputedRules(),()=>{for(let r of e.triggerPaths){let l=this.rules.get(r);l&&(l.delete(c),l.size===0&&(this.rules.delete(r),this.deps.delete(r)));}this.strategy.updateComputedRules();}}setSideEffect(e){this.effectArray.push(e);}getSideEffect(){return [...this.effectArray]}evaluate(e){let n=null;if(e.GetToken&&(n=e.GetToken()),this.pendingPromise&&this.promiseToken!==n&&(this.pendingPromise=null,this.promiseToken=null),this.pendingPromise)return this.pendingPromise;let t=false;if(typeof e.triggerPath=="string"){t=true;let l=this.deps.get(e.triggerPath),o=e.GetValueByPath(e.triggerPath);if(typeof l=="object"||typeof o=="object")t=false;else {let s=Array.from(this.deps.keys());for(let d of s){let i=this.deps.get(d),u=e.GetValueByPath(d);if(i!==u){t=false;break}}}}if(t)return this.cache;this.promiseToken=n;let c=++this.version,r=this.strategy.evaluate(e,c);if(!(r instanceof Promise)){let{res:l,version:o}=r;return this.finalizeSync(l,o,e,n)}return this.pendingPromise=(async()=>{try{let{res:l,version:o}=await r;return this.finalizeSync(l,o,e,n)}catch(l){throw {path:this.path,error:l}}finally{this.promiseToken===n&&(this.pendingPromise=null,this.promiseToken=null);}})(),this.pendingPromise}finalizeSync(e,n,t,c){return c!==this.promiseToken||n<this.version?this.cache:(this.cache=e,this.deps.forEach((r,l)=>{this.deps.set(l,t.GetValueByPath(l));}),e)}inferType(e){return Array.isArray(e)?"array":typeof e}};var te=(a,e,n)=>{let c=r=>{let l=n.triggerPaths.map(d=>r.GetValueByPath(d).value),o=Object.create(null);return Object.defineProperty(o,"triggerTargets",{get:()=>l||[]}),Object.defineProperty(o,"affectedTatget",{get:()=>r.GetRenderSchemaByPath(a)[e]}),n.logic({slot:o})};return {value:n.value,targetPath:a,triggerPaths:n.triggerPaths,priority:n.priority??10,logic:c}},ne=(a,e,n,t)=>{if(!a)throw Error("");let c=a,r=(s,d)=>{e.has(s)||e.set(s,new Set),e.get(s).add(d),n.has(d)||n.set(d,new Set),n.get(d).add(s);};return {SetRule:(s,d,i,u={logic:()=>{}})=>{let f=c(d),p=te(d,i,{...u,triggerPaths:[s]}),P=[s].map(g=>[g,c(g).state.value]);if(r(s,d),f.nodeBucket[i])f.nodeBucket[i].setRule(p,P),u.effect&&f.nodeBucket[i].setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]});else {let g=f.meta[i],m=new X(g,i,d);m.setRule(p,P),u.effect&&m.setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]}),f.nodeBucket[i]=m;}f.state[i]=f.meta[i],u.forceNotify&&f.nodeBucket[i].forceNotify();},SetRules:(s,d,i,u={logic:()=>{}})=>{let f=c(d);for(let g of s)r(g,d);let p=te(d,i,{...u,triggerPaths:s}),P=s.map(g=>[g,c(g).state.value]);if(f.nodeBucket[i])f.nodeBucket[i].setRules(p,P),u.effect&&f.nodeBucket[i].setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]});else {let g=f.meta[i],m=new X(g,i,d);m.setRules(p,P),u.effect&&m.setSideEffect({fn:u.effect,args:u.effectArgs?u.effectArgs:[i]}),f.nodeBucket[i]=m;}f.state[i]=f.meta[i],u.forceNotify&&f.nodeBucket[i].forceNotify();}}};var re=a=>{let e=a||void 0;if(!e)throw Error("");return {SetStrategy:(t,c,r)=>{e(t).nodeBucket[c].setStrategy(r);}}};function se(){let a=new Map,e=new Map,n=new Set,t=(l,o)=>{a.set(l,o);let s=e.get(l);s&&s(o);};return {SetTrace:(l,o)=>{e.set(l,o);let s=a.get(l)||"idle";return o(s),{cancel:()=>{e.delete(l);}}},useTrace:()=>({apply:o=>{o.on("flow:start",()=>{n.forEach(s=>t(s,"idle")),n.clear(),a.clear();}),o.on("node:release",({path:s,type:d})=>{(d==1||d==2)&&(n.add(s),t(s,"pending"));}),o.on("node:pending",({path:s})=>{n.add(s),(!a.has(s)||a.get(s)==="idle")&&t(s,"pending");}),o.on("node:start",({path:s})=>{n.add(s),t(s,"calculating");}),o.on("node:success",({path:s})=>{t(s,"calculated");}),o.on("node:intercept",({path:s,type:d})=>{d==3&&t(s,"calculating"),d==6&&t(s,"idle");}),o.on("node:stagnate",({path:s})=>{t(s,"pending");}),o.on("node:error",({path:s})=>t(s,"error"));}})}}function ae(a,e,n,t){let c=i=>{let u=a(),f=e(),p=new Set;return u.get(i)?.forEach(P=>p.add(P)),p.size===0?[]:Array.from(p).filter(P=>{let g=f.get(P)||new Set;return !Array.from(g).some(N=>p.has(N))})};return {GetNextDependency:i=>{let u=t();return Array.from(u.get(i)||[])},GetPrevDependency:i=>{let u=n();return Array.from(u.get(i)||[])},GetAllPrevDependency:i=>{let u=e();return Array.from(u.get(i)||[])},GetAllNextDependency:i=>{let u=a();return Array.from(u.get(i)||[])},rebuildDirectDependencyMaps:i=>{let u=new Map,f=new Map;for(let p of i){let P=c(p);u.set(p,new Set(P));for(let g of P)f.has(g)||f.set(g,new Set),f.get(g).add(p);}return {directNextMap:u,directPrevMap:f}}}}function oe(a){let e=t=>{let c=[],r=[],l=new Map,o=t.size,s=0,d=0;for(let[i,u]of t)u===0&&r.push(i);if(r.length===0&&o>0)throw Error("Circular dependency detected");for(;r.length>0;){c.push([...r]);let i=[];for(let u of r){s++,l.set(u,d);let f=a.get(u);if(f)for(let p of f){let P=t.get(p)-1;t.set(p,P),P===0&&i.push(p);}}r=i,d++;}if(s<o)throw Error("Circular dependency detected");return {steps:c,levelMap:l}};return ()=>{let t=new Map;for(let c of a.keys()){let r=Array.from(a.get(c)||[]);t.has(c)||t.set(c,0);for(let l of r){let o=t.get(l)||0;t.set(l,++o);}}return e(t)}}var q=()=>{let a=[];return {on:e=>(a.push(e),()=>{let n=a.indexOf(e);n>-1&&a.splice(n,1);}),call:e=>a.forEach(n=>{n(e);})}};function ie(){let{on:a,call:e}=q();return {onError:a,callOnError:e}}function le(){let{on:a,call:e}=q();return {onSuccess:a,callOnSuccess:e}}var ce=()=>{let a=new Set,e=new Map,n=(r,l)=>{e.get(r)?.forEach(o=>o(l));},t=(r,l)=>(e.has(r)||e.set(r,new Set),e.get(r).add(l),()=>e.get(r).delete(l));return {usePlugin:r=>{let l=new Set,o=(s,d)=>{let i=t(s,d);return l.add(i),i};return r.apply({on:o}),a.add(r),()=>{l.forEach(s=>s()),l.clear(),a.delete(r);}},emit:n}};function ue(){let{on:a,call:e}=q();return {onStart:a,callOnStart:e}}var de=(a={frameQuota:12})=>{let e=performance.now(),n=0,t=false,c=()=>!!navigator?.scheduling?.isInputPending?.({includeContinuous:true});return {getIsFirstFrame:()=>t,reset(){e=performance.now(),n=0,t=true;},shouldYield(){let r=performance.now();return n++,!!((n>=5||t)&&(n=0,r-e>a.frameQuota||c()))},async yieldToMain(){return new Promise(r=>{Pe(()=>{e=performance.now(),n=0,t&&(t=false),r();});})}}},Pe=a=>{let{port1:e,port2:n}=new MessageChannel;e.onmessage=a,n.postMessage(null);};function fe(a,e,n,t,c){let r=new Map,l=a.useGreedy,o=de();return async(d,i)=>{let f=Symbol("token"),p=d||"__NOTIFY_ALL__";r.set(p,f);let P=false;o.reset();let g=new Set,m=new Set,_=new Set;i.forEach(I=>{_.add(I),e.GetAllNextDependency(I).forEach(v=>_.add(v));});let N=new Map,C=new Map,M=new Set,L=e.GetPathToLevelMap(),A=0,z=0,H=I=>{e.GetAllNextDependency(I).forEach($=>{let G=L.get($)||0;G>z&&(z=G);});};d?(A=L.get(d)??0,H(d),g.add(d)):i.forEach(I=>H(I)),i.forEach(I=>{M.add(I);});let y=performance.now();t.emit("flow:start",{path:p}),t.callOnStart({path:p});let R=false,O=30,Q=I=>{let{target:v,trigger:$}=I,G=false,D=false,k=n.GetNodeByPath(v),W=[],E=(h,T)=>{if(g.has(h)||m.has(h)||M.has(h))return;let K=0,x=L.get(h)??0;if(N.has(h))K=N.get(h)-1;else {if(x>A&&N.size>O){C.has(x)||C.set(x,new Set),C.get(x).add(h),t.emit("node:intercept",{path:h,type:7});return}let F=e.GetPrevDependency(h),S=0;for(let b of F){if(g.has(b))continue;(L.get(b)??0)>A&&S++;}K=S;}if(K<=0){let F=M.has(h),S=m.has(h);if(F||S){t.emit("node:intercept",{path:h,type:S?3:3.1});return}N.delete(h),M.add(h),t.emit("node:release",{path:h,type:T,detail:{path:v}});}else N.set(h,K);},B=(h=[])=>{if(r.get(p)!==f)return;if(h.length){let x={},F=k.proxy;for(let S of h){let b=(S.args||[]).reduce((U,Y)=>(U[Y]=F[Y],U),{});try{let U=S.fn(b);U&&typeof U=="object"&&Object.assign(x,U);}catch(U){}}for(let S in x)if(S in k.state)k.state[S]=x[S];else throw {error:`wrong effect in ${k.path}`};G=true;}G&&c.flushPathSet.add(v),t.emit("node:success",{path:v}),g.add(v);let T=e.GetNextDependency(v);(G||D)&&e.GetAllNextDependency(v).forEach(F=>_.add(F));for(let x of T){if(g.has(x)){t.emit("node:intercept",{path:x,type:2});continue}if(m.has(x)||M.has(x)){t.emit("node:intercept",{path:x,type:m.has(x)?3:3.1});continue}if(G||D)E(x,1);else if(N.has(x))E(x,2);else {let S=L.get(x);C.has(S)||C.set(S,new Set);let b=C.get(S);b.has(x)||(b.add(x),t.emit("node:stagnate",{path:x,type:1}));}}m.delete(v),(async()=>{if(!P){let x=m.size,F=M.size;t.emit("flow:fire",{path:v,type:1,detail:{active:x,pending:F,blocked:N.size}}),j();}})();},V=h=>{t.emit("node:error",{path:v,error:h});let T=Symbol("abort");r.set(p,T),M.clear(),N.clear(),m.clear(),t.callOnError(h);},w=(h,T)=>{let K=false;h!==k[T]&&(k[T]=h,k.state[T]=h,G=true,t.emit("node:bucket:success",{path:v,key:T,value:h}),T==="value"&&(K=true)),k.nodeBucket[T].isForceNotify()&&(D=true),(K||D)&&H(v);};t.emit("node:start",{path:v});try{let h=[];for(let T in k.nodeBucket){let K=k.nodeBucket[T];h.push(...K.getSideEffect());let x=K.evaluate({affectKey:T,triggerPath:$,GetRenderSchemaByPath:F=>n.GetNodeByPath(F).proxy,GetValueByPath:F=>n.GetNodeByPath(F).state,GetToken:()=>f});if(x instanceof Promise){let F=x.then(S=>{r.get(p)===f&&w(S,T);});W.push(F);}else w(x,T);}if(W.length>0)return Promise.all(W).then(()=>{B(h);}).catch(V);B(h);return}catch(h){V(h);}},j=async()=>{if(r.get(p)!==f){P=false;return}P=true;let I=o.getIsFirstFrame(),v=0,$=()=>l&&I?30:1/0,G=0,D=$();try{for(;r.get(p)===f;){let k=G>=D,W=o.shouldYield();if(k||W){if(G>0&&(v++,(I||v%2===0)&&c.requestUpdate()),await o.yieldToMain(),r.get(p)!==f)break;G=0,I=o.getIsFirstFrame();}if(M.size>0&&m.size<5){for(let E of M){if(m.size>=5||G>=D)break;let B=L.get(E)??0,V=e.GetPrevDependency(E),w=V.length>1;if((!l||w)&&B>A){M.delete(E);let T=V.filter(K=>_.has(K)&&!g.has(K)).length;N.set(E,T||0),t.emit("node:intercept",{path:E,type:T>0?4:5,detail:{targetLevel:B,currentLevel:A,pendingParentsCount:T}});continue}if(M.delete(E),m.add(E),t.emit("node:processing",{path:E}),Q({target:E,trigger:d}),G++,G>=D||o.shouldYield())break}if(M.size>0)continue}if(G<D&&l&&N.size>0&&m.size<5){let E=!1,B=0;for(let[V,w]of N)if(w<=0){let h=L.get(V)??0,T=e.GetPrevDependency(V);if(h>A&&T.length>1)continue;if(N.delete(V),M.add(V),B++,E=!0,t.emit("node:release",{path:V,type:4}),B>=D)break}if(B>0)continue;if(E){if(o.shouldYield()&&(await o.yieldToMain(),r.get(p)!==f))break;continue}}if(m.size===0&&M.size===0){let E=new Set;for(let w of C.keys())E.add(w);for(let[w]of N){let h=L.get(w)??0;h>A&&E.add(h);}let B=Array.from(E).sort((w,h)=>w-h),V=B[0];if(B.length>0&&V<=z){let w=B[0];if(w<=z){A=w;let h=C.get(w);h&&(h.forEach(T=>M.add(T)),C.delete(w));for(let[T]of N)(L.get(T)??0)===w&&(N.delete(T),M.add(T),t.emit("node:release",{path:T,type:3,detail:{level:w}}));continue}}else {C.forEach((w,h)=>{w.forEach(T=>{g.add(T),t.emit("node:intercept",{path:T,type:6});});}),C.clear();for(let[w]of N)g.add(w),t.emit("node:intercept",{path:w,type:6});N.clear();break}}M.size>0&&m.size>=5&&t.emit("flow:wait",{type:2});break}}finally{if(P=false,m.size+N.size+M.size===0){if(r.get(p)===f&&!R){R=true,t.emit("flow:end",{type:1}),c.requestUpdate();let W=performance.now();t.emit("flow:success",{duration:(W-y).toFixed(2)+"ms"}),Promise.resolve().then(()=>{t.callOnSuccess();});}}else t.emit("flow:wait",{type:1,detail:{nums:m.size}});}};j();}}function ee(a){let{path:e,uid:n,type:t,meta:c,dirtySignal:r,triggerUI:l,state:o}=a,s=null,i={path:e,uid:n,type:t,meta:c,dirtySignal:r,createView:(u={})=>{let f=new Proxy(u,{get(p,P){let g=P;return g in a.state?a.state[g]:g in a?a[g]:c&&g in c?c[g]:Reflect.get(p,P)},set(p,P,g){let m=P;return m in a.state?(a.state[m]=g,l(),true):Reflect.set(p,P,g)},ownKeys(p){let P=new Set([...Reflect.ownKeys(p),...Object.keys(o||{}),...Object.keys(c||{})]);return Array.from(P)},getOwnPropertyDescriptor(p,P){return o&&P in o||c&&P in c?{enumerable:true,configurable:true}:Reflect.getOwnPropertyDescriptor(p,P)}});return s=f,f}};return "children"in a?{...i,children:a.children}:{...i,state:a.state,nodeBucket:a.nodeBucket,notifyKeys:a.notifyKeys,dependOn:a.dependOn,get proxy(){return s}}}function ye(a,e,n,t,c,r){let l=0,o=new Map,s=new Map,d=new Map,i=false,u=new Set,g=async()=>{let y=Array.from(u);u.clear();for(let R of y){let O=M(R);r.signalTrigger(O.dirtySignal);}},m=()=>{i||(i=true,requestAnimationFrame(()=>{try{for(;u.size>0;)g();}finally{i=false;}}));},_=fe({useGreedy:e.useGreedy},n,{GetNodeByPath:M},c,{requestUpdate:m,flushPathSet:u}),N=y=>{if(o.has(y.path))throw new Error(`[MeshFlow] Duplicate Path: ${String(y.path)}`);let R=++l,O={path:y.path,getNode:I=>M(I)},Q=I=>{let v=I({...O}),$=M(y.path);if(t.createHistoryAction&&t.pushIntoHistory){let G=t.createHistoryAction([{path:y.path,value:$.state.value},{path:y.path,value:v}],D=>{let k=M(D.path);k.state.value=D.value,A(D.path);});t.pushIntoHistory(G);}$.state.value=v,A(y.path);},j=ee({uid:R,path:y.path,state:y.state,meta:y.meta,nodeBucket:y.nodeBucket,dirtySignal:y.dirtySignal,notifyKeys:y.notifyKeys,dependOn:Q,triggerUI:()=>r.signalTrigger(y.dirtySignal)});return o.set(j.path,R),s.set(R,j),j},C=y=>{if(o.has(y.path))throw new Error(`[MeshFlow] Duplicate Path: ${String(y.path)}`);let R=++l,O=ee({uid:R,path:y.path,state:{},meta:y,nodeBucket:{},children:y.children});return o.set(O.path,R),d.set(R,O),O};function M(y){let R=o.get(y),O=s.get(R);if(!O)throw Error("wrong ID");return O}function L(y){let R=o.get(y);return d.get(R)}let A=y=>{if(!M(y))throw Error("Node undefined");u.add(y),m();let O=n.GetNextDependency(y);z(O,y);};function z(y,R){_(R,y);}return {registerNode:N,registerGroupNode:C,GetNodeByPath:M,GetGroupByPath:L,notify:A,notifyAll:async()=>{Promise.resolve().then(async()=>{let y=n.GetDependencyOrder();if(!y||y.length===0)return;let R=y[0];try{_(null,R);}catch(O){throw c.callOnError(O),O}finally{m();}});},UITrigger:r,UidToNodeMap:s}}function pe(a,e){let n=false,t=false,c=new Map,r=new Map,l=new Map,o=new Map,s=[],d=new Map,{GetNextDependency:u,GetPrevDependency:f,GetAllPrevDependency:p,GetAllNextDependency:P,rebuildDirectDependencyMaps:g}=ae(()=>c,()=>r,()=>o,()=>l),m={},_={};if(e.modules.useHistory){let{Undo:S,Redo:b,PushIntoHistory:U,CreateHistoryAction:Y,initCanUndo:he,initCanRedo:ge}=e.modules.useHistory();m.pushIntoHistory=U,m.createHistoryAction=Y,_={Undo:S,Redo:b,initCanUndo:he,initCanRedo:ge};}let{onError:N,callOnError:C}=ie(),{onSuccess:M,callOnSuccess:L}=le(),{onStart:A,callOnStart:z}=ue(),{emit:H,usePlugin:y}=ce(),{SetTrace:R,useTrace:O}=se(),Q=O();y(Q);let j=ye(a,{useGreedy:e.config.useGreedy},{GetDependencyOrder:()=>s,GetAllNextDependency:P,GetNextDependency:u,GetPrevDependency:f,GetAllPrevDependency:p,GetPathToLevelMap:()=>d},m,{callOnError:C,callOnSuccess:L,callOnStart:z,emit:H},e.UITrigger),{GetGroupByPath:I,GetNodeByPath:v,notifyAll:$}=j,G={};if(e.modules.useInternalForm){let{uiSchema:S,GetFormData:b}=e.modules.useInternalForm(j,a);G={uiSchema:S,GetFormData:b};}let D={};if(e.modules.useSchemaValidators){let{SetValidators:S}=e.modules.useSchemaValidators(v);D={SetValidators:S};}let{SetRule:k,SetRules:W}=ne(v,c,r),{SetStrategy:E}=re(v),B=oe(c),V=()=>{let S=B();s=S.steps,d=S.levelMap;},w=()=>{t||(t=true,Promise.resolve().then(()=>{if(V(),n){let{directNextMap:S,directPrevMap:b}=g(s.flat());l=S,o=b;}}).finally(()=>{t=false,n=false;}));};return {SetRule:(S,b,U,Y)=>{k(S,b,U,Y),n=true,w();},SetRules:(S,b,U,Y)=>{W(S,b,U,Y),n=true,w();},SetStrategy:E,SetTrace:R,usePlugin:y,SetValue:(S,b)=>{v(S).dependOn(()=>b);},GetValue:(S,b="value")=>v(S)[b],GetGroupByPath:I,notifyAll:async()=>{V(),await $();},GetAllDependency:()=>c,GetDependencyOrder:()=>s,historyExports:_,formExports:G,validatorExports:D,onError:N,onSuccess:M,onStart:A}}var J=new Map,me=(a,e,n)=>{try{if(typeof n.UITrigger.signalCreator!="function"||typeof n.UITrigger.signalTrigger!="function")throw Error("ui trigger undefined");if(J.has(a))throw Error("engineID repeated");let t=pe(e,{config:n.config||{useGreedy:!1},UITrigger:n.UITrigger,modules:n.modules||{},plugins:{}}),{SetRule:c,SetRules:r,SetStrategy:l,SetValue:o,GetValue:s,usePlugin:d,GetGroupByPath:i,notifyAll:u,SetTrace:f,GetAllDependency:p,GetDependencyOrder:P,historyExports:g,formExports:m,validatorExports:_,onError:N,onSuccess:C,onStart:M}=t,A={...{config:{SetRule:c,SetRules:r,SetStrategy:l,notifyAll:u,SetTrace:f,usePlugin:d},data:{SetValue:o,GetValue:s,GetGroupByPath:i},dependency:{GetAllDependency:p,GetDependencyOrder:P},hooks:{onError:N,onSuccess:C,onStart:M}}},z=n.modules;return z&&Object.keys(z).forEach(H=>{let y=H;if(y.startsWith("use")){let R=y.slice(3);y=R.charAt(0).toLowerCase()+R.slice(1);}H==="useHistory"&&g&&Object.keys(g).length>0&&(A[y]=g),H==="useInternalForm"&&Object.keys(m).length>0&&(A[y]=m),H==="useSchemaValidators"&&Object.keys(_).length>0&&(A[y]=_);}),J.set(a,A),A}catch(t){throw Error(t)}},Ze=()=>(a,e,n)=>Se(a,e,n),et=a=>{let e=J.get(a);if(e)return e;throw Error("[MeshFlow] Engine ID not found.")},tt=a=>{J.delete(a);},Se=me;export{tt as deleteEngine,et as useEngine,me as useEngineManager,Se as useMeshFlow,Ze as useMeshFlowDefiner};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meshflow/core",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A logic orchestration engine utilizing topological scheduling and watermark control to resolve asynchronous race conditions in complex dependency linkages.”",
|
|
5
5
|
"main": "./index.cjs",
|
|
6
6
|
"module": "./index.js",
|