lizaui 9.0.45 → 9.0.46

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.
@@ -166,16 +166,28 @@ function mimicFunction(to, from, { ignoreNonConfigurable = false } = {}) {
166
166
  changeToString(to, from, name);
167
167
  return to;
168
168
  }
169
+ const maxTimeoutValue = 2147483647;
169
170
  const cacheStore = /* @__PURE__ */ new WeakMap();
170
171
  const cacheTimerStore = /* @__PURE__ */ new WeakMap();
172
+ const cacheKeyStore = /* @__PURE__ */ new WeakMap();
173
+ function getValidCacheItem(cache, key) {
174
+ const item = cache.get(key);
175
+ if (!item) {
176
+ return void 0;
177
+ }
178
+ if (item.maxAge <= Date.now()) {
179
+ cache.delete(key);
180
+ return void 0;
181
+ }
182
+ return item;
183
+ }
171
184
  function memoize(function_, { cacheKey, cache = /* @__PURE__ */ new Map(), maxAge } = {}) {
172
185
  if (maxAge === 0) {
173
186
  return function_;
174
187
  }
175
- if (typeof maxAge === "number") {
176
- const maxSetIntervalValue = 2147483647;
177
- if (maxAge > maxSetIntervalValue) {
178
- throw new TypeError(`The \`maxAge\` option cannot exceed ${maxSetIntervalValue}.`);
188
+ if (typeof maxAge === "number" && Number.isFinite(maxAge)) {
189
+ if (maxAge > maxTimeoutValue) {
190
+ throw new TypeError(`The \`maxAge\` option cannot exceed ${maxTimeoutValue}.`);
179
191
  }
180
192
  if (maxAge < 0) {
181
193
  throw new TypeError("The `maxAge` option should not be a negative number.");
@@ -183,24 +195,36 @@ function memoize(function_, { cacheKey, cache = /* @__PURE__ */ new Map(), maxAg
183
195
  }
184
196
  const memoized = function(...arguments_) {
185
197
  const key = cacheKey ? cacheKey(arguments_) : arguments_[0];
186
- const cacheItem = cache.get(key);
198
+ const cacheItem = getValidCacheItem(cache, key);
187
199
  if (cacheItem) {
188
200
  return cacheItem.data;
189
201
  }
190
202
  const result = function_.apply(this, arguments_);
191
203
  const computedMaxAge = typeof maxAge === "function" ? maxAge(...arguments_) : maxAge;
204
+ if (computedMaxAge !== void 0 && computedMaxAge !== Number.POSITIVE_INFINITY) {
205
+ if (!Number.isFinite(computedMaxAge)) {
206
+ throw new TypeError("The `maxAge` function must return a finite number, `0`, or `Infinity`.");
207
+ }
208
+ if (computedMaxAge <= 0) {
209
+ return result;
210
+ }
211
+ if (computedMaxAge > maxTimeoutValue) {
212
+ throw new TypeError(`The \`maxAge\` function result cannot exceed ${maxTimeoutValue}.`);
213
+ }
214
+ }
192
215
  cache.set(key, {
193
216
  data: result,
194
- maxAge: computedMaxAge ? Date.now() + computedMaxAge : Number.POSITIVE_INFINITY
217
+ maxAge: computedMaxAge === void 0 || computedMaxAge === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : Date.now() + computedMaxAge
195
218
  });
196
- if (computedMaxAge && computedMaxAge > 0 && computedMaxAge !== Number.POSITIVE_INFINITY) {
219
+ if (computedMaxAge !== void 0 && computedMaxAge !== Number.POSITIVE_INFINITY) {
197
220
  const timer = setTimeout(() => {
198
221
  cache.delete(key);
222
+ cacheTimerStore.get(memoized)?.delete(timer);
199
223
  }, computedMaxAge);
200
224
  timer.unref?.();
201
- const timers = cacheTimerStore.get(function_) ?? /* @__PURE__ */ new Set();
225
+ const timers = cacheTimerStore.get(memoized) ?? /* @__PURE__ */ new Set();
202
226
  timers.add(timer);
203
- cacheTimerStore.set(function_, timers);
227
+ cacheTimerStore.set(memoized, timers);
204
228
  }
205
229
  return result;
206
230
  };
@@ -208,6 +232,7 @@ function memoize(function_, { cacheKey, cache = /* @__PURE__ */ new Map(), maxAg
208
232
  ignoreNonConfigurable: true
209
233
  });
210
234
  cacheStore.set(memoized, cache);
235
+ cacheKeyStore.set(memoized, cacheKey ?? ((arguments_) => arguments_[0]));
211
236
  return memoized;
212
237
  }
213
238
  function isString(el) {