@@ -2,22 +2,29 @@ import { RedisArgument, RespVersions } from "../..";
2
2
import { RedisVariadicArgument } from "../commands/generic-transformers" ;
3
3
4
4
export interface CommandParser {
5
- redisArgs : Array < RedisArgument > ;
5
+ redisArgs : ReadonlyArray < RedisArgument > ;
6
+ keys : ReadonlyArray < RedisArgument > ;
7
+ firstKey : RedisArgument | undefined ;
6
8
respVersion : RespVersions ;
7
9
preserve : unknown ;
10
+ cachable : boolean ;
8
11
9
12
push : ( arg : RedisArgument ) => unknown ;
10
13
pushVariadic : ( vals : RedisVariadicArgument ) => unknown ;
14
+ pushVariadicNumber : ( vals : number | Array < number > ) => unknown ;
11
15
pushKey : ( key : RedisArgument ) => unknown ; // normal push of keys
12
16
pushKeys : ( keys : RedisVariadicArgument ) => unknown ; // push multiple keys at a time
17
+ pushKeysLength : ( keys : RedisVariadicArgument ) => unknown ; // push multiple keys at a time
13
18
setCachable : ( ) => unknown ;
14
19
setPreserve : ( val : unknown ) => unknown ;
15
20
}
16
21
17
- export abstract class AbstractCommandParser implements CommandParser {
22
+ export class BasicCommandParser implements CommandParser {
18
23
#redisArgs: Array < RedisArgument > = [ ] ;
24
+ #keys: Array < RedisArgument > = [ ] ;
19
25
#respVersion: RespVersions ;
20
26
#preserve: unknown ;
27
+ #cachable: boolean = false ;
21
28
22
29
constructor ( respVersion : RespVersions = 2 ) {
23
30
this . #respVersion = respVersion ;
@@ -27,6 +34,14 @@ export abstract class AbstractCommandParser implements CommandParser {
27
34
return this . #redisArgs;
28
35
}
29
36
37
+ get keys ( ) {
38
+ return this . #keys;
39
+ }
40
+
41
+ get firstKey ( ) {
42
+ return this . #keys. length != 0 ? this . #keys[ 0 ] : undefined ;
43
+ }
44
+
30
45
get respVersion ( ) {
31
46
return this . #respVersion;
32
47
}
@@ -35,9 +50,12 @@ export abstract class AbstractCommandParser implements CommandParser {
35
50
return this . #preserve;
36
51
}
37
52
53
+ get cachable ( ) {
54
+ return this . #cachable
55
+ }
56
+
38
57
push ( arg : RedisArgument ) {
39
58
this . #redisArgs. push ( arg ) ;
40
-
41
59
} ;
42
60
43
61
pushVariadic ( vals : RedisVariadicArgument ) {
@@ -50,14 +68,36 @@ export abstract class AbstractCommandParser implements CommandParser {
50
68
}
51
69
}
52
70
71
+ pushVariadicNumber ( vals : number | number [ ] ) {
72
+ if ( Array . isArray ( vals ) ) {
73
+ for ( const val of vals ) {
74
+ this . push ( val . toString ( ) ) ;
75
+ }
76
+ } else {
77
+ this . push ( vals . toString ( ) ) ;
78
+ }
79
+ }
80
+
53
81
pushKey ( key : RedisArgument ) {
82
+ this . #keys. push ( key ) ;
54
83
this . #redisArgs. push ( key ) ;
55
84
} ;
56
85
86
+ pushKeysLength ( keys : RedisVariadicArgument ) {
87
+ if ( Array . isArray ( keys ) ) {
88
+ this . #redisArgs. push ( keys . length . toString ( ) ) ;
89
+ } else {
90
+ this . #redisArgs. push ( '1' ) ;
91
+ }
92
+ this . pushKeys ( keys ) ;
93
+ }
94
+
57
95
pushKeys ( keys : RedisVariadicArgument ) {
58
96
if ( Array . isArray ( keys ) ) {
97
+ this . #keys. push ( ...keys ) ;
59
98
this . #redisArgs. push ( ...keys ) ;
60
99
} else {
100
+ this . #keys. push ( keys ) ;
61
101
this . #redisArgs. push ( keys ) ;
62
102
}
63
103
}
@@ -66,27 +106,7 @@ export abstract class AbstractCommandParser implements CommandParser {
66
106
this . #preserve = val ;
67
107
}
68
108
69
- setCachable ( ) { } ;
70
- }
71
-
72
- /* Note: I do it this way, where BasicCommandParser extends Abstract without any changes,
73
- and CachedCommandParser extends Abstract with changes, to enable them to be easily
74
- distinguishable at runtime. If Cached extended Basic, then Cached would also be a Basic,
75
- thereby making them harder to distinguish.
76
- */
77
- export class BasicCommandParser extends AbstractCommandParser { } ;
78
-
79
- export interface ClusterCommandParser extends CommandParser {
80
- firstKey : RedisArgument | undefined ;
81
- }
82
-
83
- export class BasicClusterCommandParser extends BasicCommandParser implements ClusterCommandParser {
84
- firstKey : RedisArgument | undefined ;
85
-
86
- override pushKey ( key : RedisArgument ) : void {
87
- if ( ! this . firstKey ) {
88
- this . firstKey = key ;
89
- }
90
- super . pushKey ( key ) ;
91
- }
109
+ setCachable ( ) {
110
+ this . #cachable = true ;
111
+ } ;
92
112
}
0 commit comments