您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.d.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. declare namespace mem {
  2. interface CacheStorage<KeyType extends unknown, ValueType extends unknown> {
  3. has(key: KeyType): boolean;
  4. get(key: KeyType): ValueType | undefined;
  5. set(key: KeyType, value: ValueType): void;
  6. delete(key: KeyType): void;
  7. clear?: () => void;
  8. }
  9. interface Options<
  10. ArgumentsType extends unknown[],
  11. CacheKeyType extends unknown,
  12. ReturnType extends unknown
  13. > {
  14. /**
  15. Milliseconds until the cache expires.
  16. @default Infinity
  17. */
  18. readonly maxAge?: number;
  19. /**
  20. Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
  21. You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
  22. */
  23. readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType;
  24. /**
  25. Use a different cache storage. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.
  26. @default new Map()
  27. */
  28. readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>;
  29. /**
  30. Cache rejected promises.
  31. @default false
  32. */
  33. readonly cachePromiseRejection?: boolean;
  34. }
  35. }
  36. declare const mem: {
  37. /**
  38. [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
  39. @param fn - Function to be memoized.
  40. @example
  41. ```
  42. import mem = require('mem');
  43. let i = 0;
  44. const counter = () => ++i;
  45. const memoized = mem(counter);
  46. memoized('foo');
  47. //=> 1
  48. // Cached as it's the same arguments
  49. memoized('foo');
  50. //=> 1
  51. // Not cached anymore as the arguments changed
  52. memoized('bar');
  53. //=> 2
  54. memoized('bar');
  55. //=> 2
  56. ```
  57. */
  58. <
  59. ArgumentsType extends unknown[],
  60. ReturnType extends unknown,
  61. CacheKeyType extends unknown
  62. >(
  63. fn: (...arguments: ArgumentsType) => ReturnType,
  64. options?: mem.Options<ArgumentsType, CacheKeyType, ReturnType>
  65. ): (...arguments: ArgumentsType) => ReturnType;
  66. /**
  67. Clear all cached data of a memoized function.
  68. @param fn - Memoized function.
  69. */
  70. clear<ArgumentsType extends unknown[], ReturnType extends unknown>(
  71. fn: (...arguments: ArgumentsType) => ReturnType
  72. ): void;
  73. // TODO: Remove this for the next major release
  74. default: typeof mem;
  75. };
  76. export = mem;