@node-in-layers/core
Version:
The core library for the Node In Layers rapid web development framework.
45 lines • 1.51 kB
JavaScript
import { v4 } from 'uuid';
import AsyncLock from 'async-lock';
const wrap = (fn) => {
return (...args) => fn(...args);
};
const promiseWrap = (fn) => {
return (...args) => Promise.resolve().then(() => fn(...args));
};
const memoizeValueSync = (method) => {
/* eslint-disable functional/no-let */
let value = undefined;
let called = false;
return (...args) => {
if (!called) {
value = method(...args);
// This is very important that it goes afterwards. Sometimes this will throw an exception.
// and if one caller happened to catch it and move on, then this will never throw again, which
// likely means that we won't know what the heck happened.
called = true;
}
return value;
};
/* eslint-enable functional/no-let */
};
const memoizeValue = (method) => {
const key = v4();
const lock = new AsyncLock();
/* eslint-disable functional/no-let */
let value = undefined;
let called = false;
return async (...args) => {
return lock.acquire(key, async () => {
if (!called) {
value = await method(...args);
// Read above about this.
// eslint-disable-next-line require-atomic-updates
called = true;
}
return value;
});
};
/* eslint-enable functional/no-let */
};
export { wrap, promiseWrap, memoizeValue, memoizeValueSync };
//# sourceMappingURL=utils.js.map