UNPKG

@solidgrounds/core

Version:
52 lines 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retryUntilNoAsyncErrors = exports.handleAsyncError = exports.AsyncError = void 0; const tslib_1 = require("tslib"); /** * Trivially is mainly synchronous. * * In compile step all async function will be resolved. Because of conflicts * function that are not async can throw an AsyncError if the try to use something asynchronous * within retryUntilNoAsyncErrors everything will be called again until all async errors are resolved. */ const GlobalInvokeStack_1 = require("../GlobalInvokeStack"); class AsyncError extends Error { constructor(pending, retry) { super('Asynchronous function should be resoled inside ServiceContainerFactory\n' + GlobalInvokeStack_1.GlobalInvokeStack.printStack()); this.pending = pending; this.retry = retry; Object.setPrototypeOf(this, AsyncError.prototype); this.name = this.constructor.name; } } exports.AsyncError = AsyncError; /** * Catches async error, and try to resolve the promise. */ const handleAsyncError = (e) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (e instanceof AsyncError) { yield e.pending.catch(exports.handleAsyncError); return yield exports.retryUntilNoAsyncErrors(e.retry); } return Promise.reject(e); }); exports.handleAsyncError = handleAsyncError; /** * This function will be called during compile time. Retry everything until all async service functions are resolved. */ const retryUntilNoAsyncErrors = (callback, ...args) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { let retry = false; do { retry = false; try { yield callback(...args); } catch (e) { retry = true; yield exports.handleAsyncError(e); } } while (retry); }); exports.retryUntilNoAsyncErrors = retryUntilNoAsyncErrors; //# sourceMappingURL=AsyncError.js.map