-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathCacheMap.ts
More file actions
42 lines (32 loc) · 813 Bytes
/
CacheMap.ts
File metadata and controls
42 lines (32 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type React from 'react';
// Firefox has low performance of map.
class CacheMap {
maps: Record<string, number>;
// Used for cache key
// `useMemo` no need to update if `id` not change
id: number = 0;
diffRecords = new Map<React.Key, number>();
constructor() {
this.maps = Object.create(null);
}
set(key: React.Key, value: number) {
// Record prev value
this.diffRecords.set(key, this.maps[key as string]);
this.maps[key as string] = value;
this.id += 1;
}
get(key: React.Key) {
return this.maps[key as string];
}
/**
* CacheMap will record the key changed.
* To help to know what's update in the next render.
*/
resetRecord() {
this.diffRecords.clear();
}
getRecord() {
return this.diffRecords;
}
}
export default CacheMap;