UNPKG

picotx

Version:

A minimal library to create atomic transaction for any function

2 lines 1.52 kB
"use strict";var m=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var P=(o,n)=>{for(var t in n)m(o,t,{get:n[t],enumerable:!0})},R=(o,n,t,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of f(n))!h.call(o,r)&&r!==t&&m(o,r,{get:()=>n[r],enumerable:!(e=A(n,r))||e.enumerable});return o};var d=o=>R(m({},"__esModule",{value:!0}),o);var i=(o,n,t)=>new Promise((e,r)=>{var c=a=>{try{x(t.next(a))}catch(p){r(p)}},C=a=>{try{x(t.throw(a))}catch(p){r(p)}},x=a=>a.done?e(a.value):Promise.resolve(a.value).then(c,C);x((t=t.apply(o,n)).next())});var E={};P(E,{atomic:()=>y,atomicExplicit:()=>g,transaction:()=>w});module.exports=d(E);var s=[];function T(){return s[s.length-1]}function l(o){s.push(o)}function u(){s.pop()}function y(o,n){return(...t)=>i(this,null,function*(){let e=T();if(!e)throw new Error("Cannot find current transaction context");let r=yield o(...t);return e.compensations.push(()=>n(r,...t)),r})}function g(o,n){return t=>(...e)=>i(this,null,function*(){let r=yield o(...e);return t.compensations.push(()=>n(r,...e)),r})}function w(o){return i(this,null,function*(){let n={compensations:[]};l(n);try{let t;return o.length>0?t=yield o(n):t=yield o(),u(),t}catch(t){let e=[];for(let r of n.compensations.reverse())try{yield r()}catch(c){e.push(c)}throw u(),e.length>0?new AggregateError(e,"One or more rollbacks failed"):t}})}0&&(module.exports={atomic,atomicExplicit,transaction}); //# sourceMappingURL=index.cjs.map