Skip to content

Commit 4846d9d

Browse files
committed
Auto switch to the selected database.
1 parent 592018e commit 4846d9d

File tree

9 files changed

+93
-49
lines changed

9 files changed

+93
-49
lines changed

.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
"${workspaceFolder}/dist/**/*.js"
2525
]
2626
},
27+
{
28+
"type": "node",
29+
"request": "launch",
30+
"name": "Commands",
31+
"program": "${workspaceFolder}/sources/samples/commands.ts",
32+
"sourceMaps": true,
33+
"outFiles": [
34+
"${workspaceFolder}/dist/**/*.js"
35+
]
36+
},
2737
{
2838
"type": "node",
2939
"request": "launch",

.vscode/tasks.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "[Redis] Build (Watching)",
8+
"command": "tsc",
9+
"type": "shell",
10+
"args": [
11+
"-w",
12+
"-p",
13+
"."
14+
],
15+
"presentation": {
16+
"echo": true,
17+
"reveal": "never",
18+
"focus": false,
19+
"panel": "shared"
20+
},
21+
"isBackground": true,
22+
"group": {
23+
"kind": "build",
24+
"isDefault": true
25+
},
26+
"problemMatcher": "$tsc-watch"
27+
}
28+
]
29+
}

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes Logs
22

3+
## v0.1.6
4+
5+
- Auto switch to the selected database.
6+
37
## v0.1.5
48

59
- Updated the dependency @litert/core to v0.6.0.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/redis",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "A redis protocol implement for Node.js.",
55
"main": "./dist/index.js",
66
"scripts": {

sources/lib/ProtocolClient.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ implements Abstract.ProtocolClient {
269269

270270
this.emit("reconnected");
271271

272-
this._onReconnected();
272+
this._onReconnected().catch((e) => this.emit("error", e));
273273

274274
}).catch((e) => {
275275

@@ -280,14 +280,11 @@ implements Abstract.ProtocolClient {
280280
});
281281
}
282282

283-
protected _onReconnected(): void {
283+
protected async _onReconnected(): Promise<void> {
284284

285285
// do something after reconnected event.
286286

287-
if (this._buffers.length > 0) {
288-
289-
this._stopPipeline();
290-
}
287+
this._stopPipeline();
291288
}
292289

293290
protected _onError(err: NodeJS.ErrnoException): void {

sources/lib/RedisClient.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ implements Abstract.RedisClient {
2929

3030
private _password!: string;
3131

32+
private _database!: number;
33+
3234
private _subscriber!: SubscriberClient;
3335

3436
private _listeners!: Dict<Array<Abstract.SubscriptionCallback | Readable>>;
@@ -42,6 +44,8 @@ implements Abstract.RedisClient {
4244
) {
4345

4446
super(connection, host, port, createDecoder, createEncoder);
47+
48+
this._database = 0;
4549
}
4650

4751
protected async _initializeSubscriber(): Promise<void> {
@@ -145,32 +149,33 @@ implements Abstract.RedisClient {
145149
return this;
146150
}
147151

148-
protected _onReconnected(): void {
152+
protected async _onReconnected(): Promise<void> {
149153

150154
if (this._password) {
151155

152156
/**
153157
* Cache all imcoming commands before authentication completed.
154158
*/
155-
this._forcePipeline().executeNow(
156-
"AUTH", this._password
157-
).catch((e) => {
158-
159-
delete this._password;
159+
this._forcePipeline();
160160

161-
this.emit("error", e);
161+
await this.executeNow(
162+
"AUTH", this._password
163+
);
164+
}
162165

163-
super._onReconnected();
166+
if (this._database) {
164167

165-
}).then(() => {
168+
/**
169+
* Cache all imcoming commands before authentication completed.
170+
*/
171+
this._forcePipeline();
166172

167-
super._onReconnected();
168-
});
173+
await this.executeNow(
174+
"SELECT", this._database.toString()
175+
);
169176
}
170-
else {
171177

172-
super._onReconnected();
173-
}
178+
return super._onReconnected();
174179
}
175180

176181
public async exists(key: ItemKey): Promise<boolean> {
@@ -555,6 +560,8 @@ implements Abstract.RedisClient {
555560
public async select(index: number): Promise<void> {
556561

557562
await this.execute<Buffer>("SELECT", index.toString());
563+
564+
this._database = index;
558565
}
559566

560567
public async swapDB(db1: number, db2: number): Promise<void> {

sources/lib/SubscriberClient.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,48 +49,43 @@ implements Abstract.ProtocolClient {
4949
this._password = password;
5050
}
5151

52-
protected _onReconnected(): void {
52+
protected async _onReconnected(): Promise<void> {
5353

5454
if (this._password) {
5555

56-
this._forcePipeline().executeNow(
57-
"AUTH", this._password
58-
).then(() => {
56+
this._forcePipeline();
5957

60-
this._resubscribe().catch((e) => {
61-
62-
this.emit("error", new Exception(
63-
Constants.SUBSCRIBE_FAILURE,
64-
"Failed to resubscribe subjects.",
65-
e
66-
));
67-
68-
super._onReconnected();
69-
});
58+
try {
7059

71-
}).catch((e) => {
60+
await this.executeNow(
61+
"AUTH", this._password
62+
);
63+
}
64+
catch (e) {
7265

7366
this.emit("error", new Exception(
7467
Constants.SUBSCRIBE_FAILURE,
7568
"Failed to resubscribe subjects.",
7669
e
7770
));
7871

79-
super._onReconnected();
80-
});
72+
return super._onReconnected();
73+
}
8174
}
82-
else {
8375

84-
this._resubscribe().catch((e) => {
76+
try {
8577

86-
this.emit("error", new Exception(
87-
Constants.SUBSCRIBE_FAILURE,
88-
"Failed to resubscribe subjects.",
89-
e
90-
));
78+
await this._resubscribe();
79+
}
80+
catch (e) {
81+
82+
this.emit("error", new Exception(
83+
Constants.SUBSCRIBE_FAILURE,
84+
"Failed to resubscribe subjects.",
85+
e
86+
));
9187

92-
super._onReconnected();
93-
});
88+
return super._onReconnected();
9489
}
9590
}
9691

sources/samples/commands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function sleep(ms: number): Promise<void> {
3333

3434
let result: any;
3535

36-
await client.auth("redis-password");
36+
await client.auth("redis-passwd");
3737

3838
result = await client.ping();
3939

@@ -43,6 +43,8 @@ async function sleep(ms: number): Promise<void> {
4343

4444
console.log(result.toString());
4545

46+
result = await client.select(1);
47+
4648
await sleep(5000);
4749

4850
while (1) {

0 commit comments

Comments
 (0)