File tree 4 files changed +56
-0
lines changed
4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ .DS_Store
2
+ ! .vscode /settings.json
Original file line number Diff line number Diff line change
1
+ {
2
+ "deno.enable" : true ,
3
+ "deno.lint" : true ,
4
+ "deno.unstable" : false
5
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @copyright Copyright (c) 2022 Adam Josefus
3
+ */
4
+
5
+ type GeneratorType < T > = ( ) => T ;
6
+
7
+ type LoadOnlyEntryType < T > = [ key : string ] ;
8
+ type LoadAndGenerateEntryType < T > = [ key : string , generator : GeneratorType < T > ] ;
9
+ type LoadEntryType < T > = LoadOnlyEntryType < T > | LoadAndGenerateEntryType < T > ;
10
+
11
+
12
+ export class Cache < T > {
13
+
14
+ readonly #storage: Map < string , T > = new Map ( ) ;
15
+
16
+ load < E extends LoadEntryType < T > > ( ...args : E ) : E extends LoadAndGenerateEntryType < T > ? T : T | undefined {
17
+ const [ key , generator ] = args ;
18
+
19
+ if ( this . #storage. has ( key ) ) {
20
+ return this . #storage. get ( key ) ! ;
21
+ }
22
+
23
+ if ( generator ) {
24
+ const value = generator ( ) ;
25
+ this . save ( key , value ) ;
26
+
27
+ return value ;
28
+ }
29
+
30
+ // deno-lint-ignore no-explicit-any
31
+ return undefined as any ;
32
+ }
33
+
34
+
35
+ save ( key : string , value : T ) : void {
36
+ this . #storage. set ( key , value ) ;
37
+ }
38
+
39
+
40
+ has ( key : string ) : boolean {
41
+ return this . #storage. has ( key ) ;
42
+ }
43
+
44
+
45
+ delete ( key : string ) : void {
46
+ this . #storage. delete ( key ) ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ export { Cache } from './Cache/Cache.ts' ;
You can’t perform that action at this time.
0 commit comments