UNPKG

@dojoengine/sdk

Version:

Dojo SDK: Build onchain and provable apps faster

2 lines 2.9 kB
import{q as f}from"./chunk-OFVRBHAO.js";import{applyPatches as p,enablePatches as E,produceWithPatches as g}from"immer";import{subscribeWithSelector as u}from"zustand/middleware";import{immer as S}from"zustand/middleware/immer";E();function m(y,c){return y.entityId===c.entityId&&JSON.stringify(y.models)===JSON.stringify(c.models)}function D(y){let c=y(u(S((r,d)=>({entities:{},historical_entities:{},pendingTransactions:{},setEntities:t=>{r(i=>{for(let e of t)i.entities[e.entityId]=e})},mergeEntities:t=>{r(i=>{for(let e of t)if(e.entityId&&e.models){let s=i.entities[e.entityId];if(!s){i.entities[e.entityId]=e;continue}let n=Object.assign({},s.models);for(let[o,a]of Object.entries(e.models)){let l=o;l in n||(n[l]={}),n[l]=f(n[l],a)}i.entities[e.entityId]={...s,...e,models:n}}})},updateEntity:t=>{r(i=>{if(t.entityId&&t.models){let e=i.entities[t.entityId];if(!e){i.entities[t.entityId]=t;return}let s=Object.assign({},e.models);for(let[n,o]of Object.entries(t.models)){let a=n;a in s||(s[a]={}),s[a]=f(s[a],o)}i.entities[t.entityId]={...e,...t,models:s}}})},setHistoricalEntities:t=>{r(i=>{for(let e of t){let s=i.historical_entities[e.entityId];if(!s||s.length===0){i.historical_entities[e.entityId]=[e];continue}let n=s[s.length-1];m(e,n)||s.push(e)}})},mergeHistoricalEntities:t=>{r(i=>{for(let e of t)if(e.entityId&&e.models){let s=i.historical_entities[e.entityId];if(!s||s.length===0){i.historical_entities[e.entityId]=[e];continue}let n=s[s.length-1];m(e,n)||s.push(e)}})},updateHistoricalEntity:t=>{r(i=>{if(t.entityId){let e=i.historical_entities[t.entityId];if(!e||e.length===0)i.historical_entities[t.entityId]=[t];else{let s=e[e.length-1];m(t,s)||e.push(t)}}})},applyOptimisticUpdate:(t,i)=>{let e=d(),[s,n,o]=g(e,a=>{i(a)});r(()=>s),r(a=>{a.pendingTransactions[t]={transactionId:t,patches:n,inversePatches:o}})},revertOptimisticUpdate:t=>{let i=d().pendingTransactions[t];i&&(r(e=>p(e,i.inversePatches)),r(e=>{delete e.pendingTransactions[t]}))},confirmTransaction:t=>{r(i=>{delete i.pendingTransactions[t]})},subscribeToEntity:(t,i)=>c.subscribe(e=>{let s=e.entities[t];i(s)}),waitForEntityChange:(t,i,e=6e3)=>new Promise((s,n)=>{let o=c.subscribe(l=>{let T=l.entities[t];i(T)&&(clearTimeout(a),o(),s(T))}),a=setTimeout(()=>{o(),n(new Error(`waitForEntityChange: Timeout of ${e}ms exceeded`))},e)}),getEntity:t=>d().entities[t],getEntities:t=>{let i=Object.values(d().entities);return t?i.filter(t):i},getEntitiesByModel:(t,i)=>d().getEntities(e=>!!e.models[t]?.[i]),getHistoricalEntities:t=>d().historical_entities[t]||[],getEntityAtIndex:(t,i)=>{let e=d().historical_entities[t];if(!(!e||i<0||i>=e.length))return e[i]},clearHistoricalEntities:t=>{r(i=>{t?delete i.historical_entities[t]:i.historical_entities={}})},resetStore:()=>{r(t=>{t.entities={},t.historical_entities={},t.pendingTransactions={}})}}))));return c}export{D as a}; //# sourceMappingURL=chunk-HVD3TZOF.js.map