Skip to content

Commit 3e8c386

Browse files
authored
🤖 Merge PR DefinitelyTyped#72379 fix(timed-cache): add type: module, add callback option, jsdoc by @hkleungai
1 parent 3b3491a commit 3e8c386

File tree

4 files changed

+84
-21
lines changed

4 files changed

+84
-21
lines changed

‎attw.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@
335335
"tasker-js-runner",
336336
"tern",
337337
"throttle-debounce",
338-
"timed-cache",
339338
"tryghost__content-api",
340339
"typography",
341340
"uikit",
Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
export interface CacheOptions {
2-
defaultTtl: number;
2+
/**
3+
* Specify what default value you would like the TTL to have when creating the storage
4+
* @default 60 * 1000
5+
*/
6+
defaultTtl?: number | undefined;
37
}
48

5-
export interface PutOptions {
6-
ttl: number;
9+
export interface PutOptions<T = any> {
10+
/**
11+
* You can customize the time-to-live value of a key/value pair at insertion time
12+
*/
13+
ttl?: number | undefined;
14+
15+
/**
16+
* Define a callback for each inserted key/value pair to be informed when it is actually evicted from the storage
17+
*/
18+
callback?: ((key: Key, value: T) => void) | undefined;
719
}
820

921
export type Key = string | object;
1022

1123
export default class Cache<T> {
1224
constructor(options?: CacheOptions);
13-
25+
/**
26+
* Clears the internal cache.
27+
*/
1428
clear(): void;
1529

30+
/**
31+
* Returns a cached value associated with the given key if it exists,
32+
* returns an undefined value otherwise.
33+
*/
1634
get(key: Key): T | undefined;
1735

18-
put(key: Key, value: T, options?: PutOptions): void;
36+
/**
37+
* Puts a key/value pair into the cache storage.
38+
*/
39+
put(key: Key, value: T, options?: PutOptions<T>): void;
1940

41+
/**
42+
* Clears the cache entry associated with the given `key`.
43+
*/
2044
remove(key: Key): void;
2145

46+
/**
47+
* Returns the size of the cache object in
48+
* terms of referenced elements.
49+
*/
2250
size(): number;
2351
}

‎types/timed-cache/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"private": true,
33
"name": "@types/timed-cache",
44
"version": "2.0.9999",
5+
"type": "module",
56
"projects": [
67
"https://github.com/HQarroum/timed-cache#readme"
78
],
Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
11
import Cache, { CacheOptions } from "timed-cache";
22

3-
const option: CacheOptions = { defaultTtl: 5000 };
4-
const cache = new Cache<string>(option); // $ExpectType Cache<string>
5-
cache.put("key", "value", { ttl: 1000 }); // $ExpectType void
6-
cache.get("key"); // $ExpectType string | undefined
7-
8-
interface Foo {
9-
thing: string;
3+
// constructor
4+
{
5+
new Cache<string>();
6+
new Cache<string>({});
7+
new Cache<string>({ defaultTtl: 5000 });
108
}
119

12-
const cache2 = new Cache<Foo>();
13-
cache2.put("key", { thing: "foo" }); // $ExpectType void
14-
cache2.get("key"); // $Expect Type Foo
10+
// Method
11+
12+
{
13+
interface Foo {
14+
thing: string;
15+
}
16+
17+
const cache = new Cache<Foo>();
18+
19+
// $ExpectType void
20+
cache.clear();
1521

16-
const cache3 = new Cache<Foo>();
17-
const objKey = { what: "ever" };
18-
cache3.put(objKey, { thing: "foo" }); // $ExpectType void
19-
cache3.get(objKey); // $Expect Type Foo
20-
cache3.remove(objKey); // $Expect Type void
22+
// $ExpectType Foo | undefined
23+
cache.get("key");
24+
// $ExpectType Foo | undefined
25+
cache.get({ key: "val" });
26+
27+
// $ExpectType void
28+
cache.put("key", { thing: "foo" }, {
29+
ttl: 50,
30+
callback: (key, value) => {
31+
// $ExpectType Key
32+
key;
33+
// $ExpectType Foo
34+
value;
35+
},
36+
});
37+
// $ExpectType void
38+
cache.put({ key: "val" }, { thing: "foo" }, {
39+
ttl: 50,
40+
callback: (key, value) => {
41+
// $ExpectType Key
42+
key;
43+
// $ExpectType Foo
44+
value;
45+
},
46+
});
47+
48+
// $Expect Type void
49+
cache.remove("key");
50+
// $Expect Type void
51+
cache.remove({ key: "val" });
52+
53+
// $ExpectType number
54+
cache.size();
55+
}

0 commit comments

Comments
 (0)