picotx
Version:
A minimal library to create atomic transaction for any function
2 lines • 1.01 kB
JavaScript
var i=(o,e,t)=>new Promise((n,r)=>{var c=a=>{try{x(t.next(a))}catch(p){r(p)}},l=a=>{try{x(t.throw(a))}catch(p){r(p)}},x=a=>a.done?n(a.value):Promise.resolve(a.value).then(c,l);x((t=t.apply(o,e)).next())});var s=[];function u(){return s[s.length-1]}function T(o){s.push(o)}function m(){s.pop()}function P(o,e){return(...t)=>i(this,null,function*(){let n=u();if(!n)throw new Error("Cannot find current transaction context");let r=yield o(...t);return n.compensations.push(()=>e(r,...t)),r})}function R(o,e){return t=>(...n)=>i(this,null,function*(){let r=yield o(...n);return t.compensations.push(()=>e(r,...n)),r})}function E(o){return i(this,null,function*(){let e={compensations:[]};T(e);try{let t;return o.length>0?t=yield o(e):t=yield o(),m(),t}catch(t){let n=[];for(let r of e.compensations.reverse())try{yield r()}catch(c){n.push(c)}throw m(),n.length>0?new AggregateError(n,"One or more rollbacks failed"):t}})}export{P as atomic,R as atomicExplicit,E as transaction};
//# sourceMappingURL=index.js.map