UNPKG

@daysnap/utils

Version:
23 lines (19 loc) 1.18 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/createWithLoading.ts function createWithLoading(showLoading, defaultOptions) { return function withLoading(fn, options = defaultOptions) { return async (...params) => { if (params.length > fn.length) { options = params.pop(); } const toast = options ? showLoading(options) : null; try { return await fn(...params); } catch (error) { throw error; } finally { _optionalChain([toast, 'optionalAccess', _ => _.close, 'call', _2 => _2()]); } }; }; } exports.createWithLoading = createWithLoading;