source: trip-planner-front/node_modules/mem/dist/index.d.ts@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[6a3a178]1declare type AnyFunction = (...arguments_: any) => any;
2interface CacheStorageContent<ValueType> {
3 data: ValueType;
4 maxAge: number;
5}
6interface CacheStorage<KeyType, ValueType> {
7 has: (key: KeyType) => boolean;
8 get: (key: KeyType) => CacheStorageContent<ValueType> | undefined;
9 set: (key: KeyType, value: CacheStorageContent<ValueType>) => void;
10 delete: (key: KeyType) => void;
11 clear?: () => void;
12}
13interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {
14 /**
15 Milliseconds until the cache expires.
16
17 @default Infinity
18 */
19 readonly maxAge?: number;
20 /**
21 Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
22
23 A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
24
25 You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
26
27 ```
28 import mem = require('mem');
29
30 mem(function_, {cacheKey: JSON.stringify});
31 ```
32
33 Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
34
35 ```
36 import mem = require('mem');
37 import serializeJavascript = require('serialize-javascript');
38
39 mem(function_, {cacheKey: serializeJavascript});
40 ```
41
42 @default arguments_ => arguments_[0]
43 @example arguments_ => JSON.stringify(arguments_)
44 */
45 readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
46 /**
47 Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.
48
49 @default new Map()
50 @example new WeakMap()
51 */
52 readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
53}
54/**
55[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.
56
57@param fn - Function to be memoized.
58
59@example
60```
61import mem = require('mem');
62
63let i = 0;
64const counter = () => ++i;
65const memoized = mem(counter);
66
67memoized('foo');
68//=> 1
69
70// Cached as it's the same arguments
71memoized('foo');
72//=> 1
73
74// Not cached anymore as the arguments changed
75memoized('bar');
76//=> 2
77
78memoized('bar');
79//=> 2
80```
81*/
82declare const mem: {
83 <FunctionToMemoize extends AnyFunction, CacheKeyType>(fn: FunctionToMemoize, { cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>): FunctionToMemoize;
84 /**
85 @returns A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
86
87 @example
88 ```
89 import mem = require('mem');
90
91 class Example {
92 index = 0
93
94 @mem.decorator()
95 counter() {
96 return ++this.index;
97 }
98 }
99
100 class ExampleWithOptions {
101 index = 0
102
103 @mem.decorator({maxAge: 1000})
104 counter() {
105 return ++this.index;
106 }
107 }
108 ```
109 */
110 decorator<FunctionToMemoize_1 extends AnyFunction, CacheKeyType_1>(options?: Options<FunctionToMemoize_1, CacheKeyType_1>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
111 /**
112 Clear all cached data of a memoized function.
113
114 @param fn - Memoized function.
115 */
116 clear(fn: AnyFunction): void;
117};
118export = mem;
Note: See TracBrowser for help on using the repository browser.