UNPKG

recoverabletrycatch

Version:

This is a little package that brings into play a simple API similar to the `try-catch-finally` syntax to retrieve the main computation after any error was handled.

3 lines (2 loc) 3.89 kB
"use strict";(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.recoverabletrycatch={}))})(void 0,function(a){'use strict';var k=Number.isNaN;function b(a){return"function"==typeof a}function*c(){}function d(a){return b(a)&&a instanceof c.constructor}async function*e(){}function f(a){return b(a)&&a instanceof e.constructor}function g(){const a=this[l]();let c,d;const e={retry:!1,recover:!1,restart:!1},f={value:0},i=()=>{},j=(a=1)=>{if("number"!=typeof a||k(a))throw new TypeError(`${a} is not a valid argument for the retry function`);f.value=a,e.retry=!0},o=a=>(d=a,e.recover=!0),p=()=>e.restart=!0;do{try{c=a.next(d)}catch(a){return(this[m]({error:a,isRecoverable:!1},{retry:i,recover:i,restart:p}),e.restart)?g.call(this):void(this[n]&&this[n]())}const k=b(c.value)?c.value:()=>c.value;try{d=k()}catch(a){const b=h.call(this,{flowFlags:e,retryNTimes:f},{expr:k,error:a,retryFn:j,recoverFn:o,restartFn:p});if(e.restart)return g.call(this);if(e.recover){e.recover=!1,f.value=0;continue}if(e.retry){e.retry=!1,d=b,f.value=0;continue}return void(this[n]&&this[n]())}}while(!c.done);this[n]&&this[n]()}function h({flowFlags:a,retryNTimes:b},{expr:c,error:d,retryFn:e,recoverFn:f,restartFn:g}){if((this[m]({error:d,isRecoverable:!0},{retry:e,recover:f,restart:g}),!a.restart)&&!a.recover){if(a.retry&&b.value){let d=null,i=null;for(;0<b.value;){try{i=c(),d=null;break}catch(a){d=a}b.value--}return d?h.call(this,{flowFlags:a,retryNTimes:b},{expr:c,error:d,retryFn:e,recoverFn:f,restartFn:g}):i}return a.restart=!1,a.retry=!1,void(a.recover=!1)}}async function i(){const a=this[p]();let c,d;const e={retry:!1,recover:!1,restart:!1},f={value:0},g=()=>{},h=(a=1)=>{if("number"!=typeof a||k(a))throw new TypeError(`${a} is not a valid argument for the retry function`);f.value=a,e.retry=!0},l=a=>(d=a,e.recover=!0),m=()=>e.restart=!0;do{try{c=await a.next(d)}catch(a){return(this[q]({error:a,isRecoverable:!1},{retry:g,recover:g,restart:m}),e.restart)?await i.call(this):void(this[r]&&this[r]())}const k=b(c.value)?c.value:()=>c.value;try{d=await k()}catch(a){const b=await j.call(this,{flowFlags:e,retryNTimes:f},{expr:k,error:a,retryFn:h,recoverFn:l,restartFn:m});if(e.restart)return await i.call(this);if(e.recover){e.recover=!1,f.value=0;continue}if(e.retry){e.retry=!1,d=b,f.value=0;continue}return void(this[r]&&this[r]())}}while(!c.done);this[r]&&this[r]()}async function j({flowFlags:a,retryNTimes:b},{expr:c,error:d,retryFn:e,recoverFn:f,restartFn:g}){if((this[q]({error:d,isRecoverable:!0},{retry:e,recover:f,restart:g}),!a.restart)&&!a.recover){if(a.retry&&b.value){let d=null,h=null;for(;0<b.value;){try{h=await c(),d=null;break}catch(a){d=a}b.value--}return d?await j.call(this,{flowFlags:a,retryNTimes:b},{expr:c,error:d,retryFn:e,recoverFn:f,restartFn:g}):h}return a.restart=!1,a.retry=!1,void(a.recover=!1)}}const l=Symbol("task"),m=Symbol("catcher"),n=Symbol("finally"),o=function a(b){if(!d(b))throw TypeError(`${b} is not a generator function`);const c=Object.create(a.prototype);return c[l]=b,c};o.prototype.catch=function(a){if(!b(a))throw TypeError(`${a} is not a function`);this[m]=a;const c=()=>g.call(this);return{finally:a=>{if(!b(a))throw TypeError(`${a} is not a function`);return this[n]=a,{try:c}},try:c}};const p=Symbol("taskAsync"),q=Symbol("catcher"),r=Symbol("finally"),s=function a(b){if(!f(b))throw TypeError(`${b} is not an async generator function`);const c=Object.create(a.prototype);return c[p]=b,c};s.prototype.catch=function(a){if(!b(a))throw TypeError(`${a} is not a function`);this[q]=a;const c=async()=>await i.call(this);return{finally:a=>{if(!b(a))throw TypeError(`${a} is not a function`);return this[r]=a,{try:c}},try:c}},a.performAsync=s,a.performSync=o,Object.defineProperty(a,"__esModule",{value:!0})}); //# sourceMappingURL=index.js.map