@@ -2,22 +2,29 @@ import { RedisArgument, RespVersions } from "../..";
22import { RedisVariadicArgument } from "../commands/generic-transformers" ;
33
44export interface CommandParser {
5- redisArgs : Array < RedisArgument > ;
5+ redisArgs : ReadonlyArray < RedisArgument > ;
6+ keys : ReadonlyArray < RedisArgument > ;
7+ firstKey : RedisArgument | undefined ;
68 respVersion : RespVersions ;
79 preserve : unknown ;
10+ cachable : boolean ;
811
912 push : ( arg : RedisArgument ) => unknown ;
1013 pushVariadic : ( vals : RedisVariadicArgument ) => unknown ;
14+ pushVariadicNumber : ( vals : number | Array < number > ) => unknown ;
1115 pushKey : ( key : RedisArgument ) => unknown ; // normal push of keys
1216 pushKeys : ( keys : RedisVariadicArgument ) => unknown ; // push multiple keys at a time
17+ pushKeysLength : ( keys : RedisVariadicArgument ) => unknown ; // push multiple keys at a time
1318 setCachable : ( ) => unknown ;
1419 setPreserve : ( val : unknown ) => unknown ;
1520}
1621
17- export abstract class AbstractCommandParser implements CommandParser {
22+ export class BasicCommandParser implements CommandParser {
1823 #redisArgs: Array < RedisArgument > = [ ] ;
24+ #keys: Array < RedisArgument > = [ ] ;
1925 #respVersion: RespVersions ;
2026 #preserve: unknown ;
27+ #cachable: boolean = false ;
2128
2229 constructor ( respVersion : RespVersions = 2 ) {
2330 this . #respVersion = respVersion ;
@@ -27,6 +34,14 @@ export abstract class AbstractCommandParser implements CommandParser {
2734 return this . #redisArgs;
2835 }
2936
37+ get keys ( ) {
38+ return this . #keys;
39+ }
40+
41+ get firstKey ( ) {
42+ return this . #keys. length != 0 ? this . #keys[ 0 ] : undefined ;
43+ }
44+
3045 get respVersion ( ) {
3146 return this . #respVersion;
3247 }
@@ -35,9 +50,12 @@ export abstract class AbstractCommandParser implements CommandParser {
3550 return this . #preserve;
3651 }
3752
53+ get cachable ( ) {
54+ return this . #cachable
55+ }
56+
3857 push ( arg : RedisArgument ) {
3958 this . #redisArgs. push ( arg ) ;
40-
4159 } ;
4260
4361 pushVariadic ( vals : RedisVariadicArgument ) {
@@ -50,14 +68,36 @@ export abstract class AbstractCommandParser implements CommandParser {
5068 }
5169 }
5270
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+
5381 pushKey ( key : RedisArgument ) {
82+ this . #keys. push ( key ) ;
5483 this . #redisArgs. push ( key ) ;
5584 } ;
5685
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+
5795 pushKeys ( keys : RedisVariadicArgument ) {
5896 if ( Array . isArray ( keys ) ) {
97+ this . #keys. push ( ...keys ) ;
5998 this . #redisArgs. push ( ...keys ) ;
6099 } else {
100+ this . #keys. push ( keys ) ;
61101 this . #redisArgs. push ( keys ) ;
62102 }
63103 }
@@ -66,27 +106,7 @@ export abstract class AbstractCommandParser implements CommandParser {
66106 this . #preserve = val ;
67107 }
68108
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+ } ;
92112}
0 commit comments