UNPKG

@itsmworkbench/utils

Version:

The usual utility functions

62 lines (61 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.futureCacheLogString = exports.futureCacheLog = exports.futureCacheConsoleLog = exports.getOrUpdateFromPromiseCache = void 0; const listeners_1 = require("./listeners"); function getOrUpdateFromPromiseCache(engine, context, name1, name2, raw) { const { listeners, cache } = engine; const thisCache = cache[name1] || {}; const saved = thisCache[name2]; if (saved) { (0, listeners_1.callListeners)(listeners, 'duplicateCall', l => l.duplicateCall?.(context)); return saved; } (0, listeners_1.callListeners)(listeners, 'callingLoad', l => l.callingLoad?.(context)); const result = raw().then(res => { (0, listeners_1.callListeners)(listeners, 'loadArrived', l => l.loadArrived?.(context, res)); return res; }).catch(e => { (0, listeners_1.callListeners)(listeners, 'loadError', l => l.loadError?.(context, e)); return undefined; }).finally(() => { delete thisCache[name2]; }); thisCache[name2] = result; cache[name1] = thisCache; return result; } exports.getOrUpdateFromPromiseCache = getOrUpdateFromPromiseCache; function futureCacheConsoleLog(title) { return { duplicateCall: (context) => console.log(title, `duplicateCall`, context), callingLoad: (context) => console.log(title, `callingLoad`, context), loadAborted: (context) => console.log(title, `loadAborted`, context), loadArrived: (context, result) => console.log(title, `loadArrived`, context, result), loadAbandoned: (context, reason) => console.log(title, `loadAbandoned`, context, reason), loadError: (context, e) => console.log(title, `loadError`, context, e), info: (context, t, msg) => console.log(title, `${t}: ${JSON.stringify(context)} ${msg}`) }; } exports.futureCacheConsoleLog = futureCacheConsoleLog; function futureCacheLog(array) { return { duplicateCall: (context) => array.push({ context, title: 'duplicateCall' }), callingLoad: (context) => array.push({ context, title: 'callingLoad' }), loadAborted: (context) => array.push({ context, title: 'loadAborted' }), loadArrived: (context, t) => array.push({ context, title: 'loadArrived', t }), loadAbandoned: (context, reason) => array.push({ context, title: 'loadAbandoned', msg: reason }), loadError: (context, e) => array.push({ context, title: 'loadError', msg: e }), info: (context, title, msg) => array.push({ context, title, msg }) }; } exports.futureCacheLog = futureCacheLog; function futureCacheLogString(array) { return { duplicateCall: (context) => array.push(`duplicateCall: ${JSON.stringify(context)}`), callingLoad: (context) => array.push(`callingLoad: ${JSON.stringify(context)}`), loadAborted: (context) => array.push(`loadAborted: ${JSON.stringify(context)}`), loadArrived: (context, result) => array.push(`loadArrived: ${JSON.stringify(context)} ${JSON.stringify(result)}`), loadAbandoned: (context, reason) => array.push(`loadAbandoned: ${JSON.stringify(context)} ${reason}`), loadError: (context, e) => array.push(`loadError: ${JSON.stringify(context)} ${e}`), info: (context, title, msg) => array.push(`${title}: ${JSON.stringify(context)} ${msg}`) }; } exports.futureCacheLogString = futureCacheLogString;