Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mode property to client #1618

Merged
merged 1 commit into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Redis.ts
Expand Up @@ -338,6 +338,20 @@ class Redis extends Commander {
return new Redis({ ...this.options, ...override });
}

/**
* Mode of the connection.
*
* One of `"normal"`, `"subscriber"`, or `"monitor"`. When the connection is
* not in `"normal"` mode, certain commands are not allowed.
*/
get mode(): "normal" | "subscriber" | "monitor" {
return this.options.monitor
? "monitor"
: this.condition.subscriber
? "subscriber"
: "normal";
}

/**
* Listen for all requests received by the server in real time.
*
Expand Down
12 changes: 12 additions & 0 deletions test/functional/monitor.ts
Expand Up @@ -41,6 +41,18 @@ describe("monitor", () => {
});
});

it("should report being in 'monitor' mode", (done) => {
const redis = new Redis();
redis.monitor(async (err, monitor) => {
await waitForMonitorReady(monitor);
expect(redis.mode).to.equal("normal");
expect(monitor.mode).to.equal("monitor");
redis.disconnect();
monitor.disconnect();
done();
});
});

it("should continue monitoring after reconnection", (done) => {
const redis = new Redis();
redis.monitor((err, monitor) => {
Expand Down
20 changes: 20 additions & 0 deletions test/functional/pub_sub.ts
Expand Up @@ -29,6 +29,15 @@ describe("pub/sub", function () {
});
});

it("should report being in 'subscriber' mode when subscribed", (done) => {
const redis = new Redis();
redis.subscribe("foo", function () {
expect(redis.mode).to.equal("subscriber");
redis.disconnect();
done();
});
});

it("should exit subscriber mode using unsubscribe", (done) => {
const redis = new Redis();
redis.subscribe("foo", "bar", function () {
Expand All @@ -52,6 +61,17 @@ describe("pub/sub", function () {
});
});

it("should report being in 'normal' mode after unsubscribing", (done) => {
const redis = new Redis();
redis.subscribe("foo", "bar", function () {
redis.unsubscribe("foo", "bar", function (err, count) {
expect(redis.mode).to.equal("normal");
redis.disconnect();
done();
});
});
});

it("should receive messages when subscribe a channel", (done) => {
const redis = new Redis();
const pub = new Redis();
Expand Down