-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathdefault_client.ts
More file actions
57 lines (51 loc) · 1.49 KB
/
default_client.ts
File metadata and controls
57 lines (51 loc) · 1.49 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { Client } from "./client.ts";
import type {
DefaultPubSubMessageType,
PubSubMessageType,
RedisSubscription,
SubscribeCommand,
} from "./subscription.ts";
import type { Connection, SendCommandOptions } from "./connection.ts";
import { DefaultRedisSubscription } from "./default_subscription.ts";
import type { RedisReply, RedisValue } from "./protocol/shared/types.ts";
export function createDefaultClient(connection: Connection): Client {
return new DefaultClient(connection);
}
class DefaultClient implements Client {
constructor(readonly connection: Connection) {}
exec(
command: string,
...args: RedisValue[]
): Promise<RedisReply> {
return this.connection.sendCommand(command, args);
}
sendCommand(
command: string,
args?: RedisValue[],
options?: SendCommandOptions,
) {
return this.connection.sendCommand(command, args, options);
}
async subscribe<
TMessage extends PubSubMessageType = DefaultPubSubMessageType,
>(
command: SubscribeCommand,
...channelsOrPatterns: Array<string>
): Promise<RedisSubscription<TMessage>> {
const subscription = new DefaultRedisSubscription<TMessage>(
this.connection,
);
switch (command) {
case "SUBSCRIBE":
await subscription.subscribe(...channelsOrPatterns);
break;
case "PSUBSCRIBE":
await subscription.psubscribe(...channelsOrPatterns);
break;
}
return subscription;
}
close(): void {
this.connection.close();
}
}