@openstax/ts-utils 1.20.2 → 1.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/misc/helpers.d.ts +4 -0
- package/dist/cjs/misc/helpers.js +13 -1
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/misc/helpers.d.ts +4 -0
- package/dist/esm/misc/helpers.js +13 -1
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -80,6 +80,8 @@ export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predica
|
|
|
80
80
|
/**
|
|
81
81
|
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
82
82
|
*
|
|
83
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
84
|
+
*
|
|
83
85
|
* eg:
|
|
84
86
|
* const heavyFunction = () => 'hello';
|
|
85
87
|
* const butOnlyOnce = once(() => 'hello');
|
|
@@ -90,6 +92,8 @@ export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predica
|
|
|
90
92
|
export declare const once: <F extends (...args: any[]) => any>(fn: F) => F;
|
|
91
93
|
/**
|
|
92
94
|
* memoizes a function with any number of arguments
|
|
95
|
+
*
|
|
96
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
93
97
|
*/
|
|
94
98
|
export declare const memoize: <F extends (...args: any[]) => any>(fn: F) => F;
|
|
95
99
|
/**
|
package/dist/cjs/misc/helpers.js
CHANGED
|
@@ -107,6 +107,8 @@ exports.mapFind = mapFind;
|
|
|
107
107
|
/**
|
|
108
108
|
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
109
109
|
*
|
|
110
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
111
|
+
*
|
|
110
112
|
* eg:
|
|
111
113
|
* const heavyFunction = () => 'hello';
|
|
112
114
|
* const butOnlyOnce = once(() => 'hello');
|
|
@@ -133,6 +135,8 @@ const once = (fn) => {
|
|
|
133
135
|
exports.once = once;
|
|
134
136
|
/**
|
|
135
137
|
* memoizes a function with any number of arguments
|
|
138
|
+
*
|
|
139
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
136
140
|
*/
|
|
137
141
|
const memoize = (fn) => {
|
|
138
142
|
const cache = {};
|
|
@@ -155,7 +159,15 @@ const memoize = (fn) => {
|
|
|
155
159
|
};
|
|
156
160
|
return ((...args) => {
|
|
157
161
|
const thisCache = resolveCache(cache, args);
|
|
158
|
-
|
|
162
|
+
if ('result' in thisCache) {
|
|
163
|
+
return thisCache.result;
|
|
164
|
+
}
|
|
165
|
+
thisCache.result = fn(...args);
|
|
166
|
+
if (typeof thisCache.result === 'object' && thisCache.result instanceof Promise) {
|
|
167
|
+
// Clear the result when possible but do not return a Promise that resolves to the initialValue
|
|
168
|
+
thisCache.result.catch(() => delete thisCache.result);
|
|
169
|
+
}
|
|
170
|
+
return thisCache.result;
|
|
159
171
|
});
|
|
160
172
|
};
|
|
161
173
|
exports.memoize = memoize;
|