Skip to content

Commit 1e18ff6

Browse files
committed
perf(ts)!: move multi() away from RedisClient
RedisClientMultiCommandType is slow to typecheck because of its recursive nature and the fact that it has many ( around 700 ) methods. Most of the users dont use multi(), so there is no point to incur the ts costs to them. One possible solution is to move the multi() method away from the main RedisClient, thus typechecking will not apply to RedisClientMultiCommandType. This is a breaking change. Usage before: import { createClient } from 'redis'; const client = createClient(); cont result = await client.multi() .set('foo', 3) .get('bar') .execTyped(); Usage after: import { createClient, multi } from 'redis'; const client = createClient(); cont result = await multi(client) .set('foo', 3) .get('bar') .execTyped();
1 parent 6eed1ee commit 1e18ff6

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

packages/client/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export { VerbatimString } from './lib/RESP/verbatim-string';
1212
export { defineScript } from './lib/lua-script';
1313
export * from './lib/errors';
1414

15+
import { RedisFunctions, RedisModules, RedisScripts, RespVersions, TypeMapping } from './lib/RESP/types';
1516
import RedisClient, { RedisClientOptions, RedisClientType } from './lib/client';
17+
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './lib/client/multi-command';
1618
export { RedisClientOptions, RedisClientType };
1719
export const createClient = RedisClient.create;
1820
export { CommandParser } from './lib/client/parser';
@@ -37,3 +39,13 @@ export { REDIS_FLUSH_MODES } from './lib/commands/FLUSHALL';
3739

3840
export { BasicClientSideCache, BasicPooledClientSideCache } from './lib/client/cache';
3941

42+
43+
44+
export const MULTI = < M extends RedisModules, F extends RedisFunctions, S extends RedisScripts, RESP extends RespVersions, TYPE_MAPPING extends TypeMapping >(client: RedisClientType) => {
45+
type Multi = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
46+
return new ((this as any).Multi as Multi)(
47+
client._executeMulti.bind(client),
48+
client._executePipeline.bind(client),
49+
client._commandOptions?.typeMapping
50+
);
51+
}

packages/client/lib/client/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { URL } from 'node:url';
99
import { TcpSocketConnectOpts } from 'node:net';
1010
import { PUBSUB_TYPE, PubSubType, PubSubListener, PubSubTypeListeners, ChannelListeners } from './pub-sub';
1111
import { Command, CommandSignature, TypeMapping, CommanderConfig, RedisFunction, RedisFunctions, RedisModules, RedisScript, RedisScripts, ReplyUnion, RespVersions, RedisArgument, ReplyWithTypeMapping, SimpleStringReply, TransformReply, CommandArguments } from '../RESP/types';
12-
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
1312
import { RedisMultiQueuedCommand } from '../multi-command';
1413
import HELLO, { HelloOptions } from '../commands/HELLO';
1514
import { ScanOptions, ScanCommonOptions } from '../commands/SCAN';
@@ -21,6 +20,7 @@ import { BasicCommandParser, CommandParser } from './parser';
2120
import SingleEntryCache from '../single-entry-cache';
2221
import { version } from '../../package.json'
2322
import EnterpriseMaintenanceManager, { MaintenanceUpdate, MovingEndpointType } from './enterprise-maintenance-manager';
23+
import RedisClientMultiCommand from './multi-command';
2424

2525
export interface RedisClientOptions<
2626
M extends RedisModules = RedisModules,
@@ -435,7 +435,7 @@ export default class RedisClient<
435435
#selectedDB = 0;
436436
#monitorCallback?: MonitorCallback<TYPE_MAPPING>;
437437
private _self = this;
438-
private _commandOptions?: CommandOptions<TYPE_MAPPING>;
438+
_commandOptions?: CommandOptions<TYPE_MAPPING>;
439439
// flag used to annotate that the client
440440
// was in a watch transaction when
441441
// a topology change occured
@@ -1315,17 +1315,6 @@ export default class RedisClient<
13151315
return execResult as Array<unknown>;
13161316
}
13171317

1318-
MULTI() {
1319-
type Multi = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
1320-
return new ((this as any).Multi as Multi)(
1321-
this._executeMulti.bind(this),
1322-
this._executePipeline.bind(this),
1323-
this._commandOptions?.typeMapping
1324-
);
1325-
}
1326-
1327-
multi = this.MULTI;
1328-
13291318
async* scanIterator(
13301319
this: RedisClientType<M, F, S, RESP, TYPE_MAPPING>,
13311320
options?: ScanOptions & ScanIteratorOptions

0 commit comments

Comments
 (0)