Skip to content

Commit 9e6db7d

Browse files
authoredJul 11, 2022
feat: add mode property to client (#1618)
Adds a .mode getter to the client that reports whether the client is in 'normal', 'subscriber', or 'monitor' mode.
1 parent 5c989e5 commit 9e6db7d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
 

‎lib/Redis.ts

+14
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,20 @@ class Redis extends Commander {
338338
return new Redis({ ...this.options, ...override });
339339
}
340340

341+
/**
342+
* Mode of the connection.
343+
*
344+
* One of `"normal"`, `"subscriber"`, or `"monitor"`. When the connection is
345+
* not in `"normal"` mode, certain commands are not allowed.
346+
*/
347+
get mode(): "normal" | "subscriber" | "monitor" {
348+
return this.options.monitor
349+
? "monitor"
350+
: this.condition.subscriber
351+
? "subscriber"
352+
: "normal";
353+
}
354+
341355
/**
342356
* Listen for all requests received by the server in real time.
343357
*

‎test/functional/monitor.ts

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ describe("monitor", () => {
4141
});
4242
});
4343

44+
it("should report being in 'monitor' mode", (done) => {
45+
const redis = new Redis();
46+
redis.monitor(async (err, monitor) => {
47+
await waitForMonitorReady(monitor);
48+
expect(redis.mode).to.equal("normal");
49+
expect(monitor.mode).to.equal("monitor");
50+
redis.disconnect();
51+
monitor.disconnect();
52+
done();
53+
});
54+
});
55+
4456
it("should continue monitoring after reconnection", (done) => {
4557
const redis = new Redis();
4658
redis.monitor((err, monitor) => {

‎test/functional/pub_sub.ts

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe("pub/sub", function () {
2929
});
3030
});
3131

32+
it("should report being in 'subscriber' mode when subscribed", (done) => {
33+
const redis = new Redis();
34+
redis.subscribe("foo", function () {
35+
expect(redis.mode).to.equal("subscriber");
36+
redis.disconnect();
37+
done();
38+
});
39+
});
40+
3241
it("should exit subscriber mode using unsubscribe", (done) => {
3342
const redis = new Redis();
3443
redis.subscribe("foo", "bar", function () {
@@ -52,6 +61,17 @@ describe("pub/sub", function () {
5261
});
5362
});
5463

64+
it("should report being in 'normal' mode after unsubscribing", (done) => {
65+
const redis = new Redis();
66+
redis.subscribe("foo", "bar", function () {
67+
redis.unsubscribe("foo", "bar", function (err, count) {
68+
expect(redis.mode).to.equal("normal");
69+
redis.disconnect();
70+
done();
71+
});
72+
});
73+
});
74+
5575
it("should receive messages when subscribe a channel", (done) => {
5676
const redis = new Redis();
5777
const pub = new Redis();

0 commit comments

Comments
 (0)
Please sign in to comment.