Skip to content

Add .ready promise to Subscriber #1372

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/commands/psubscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Pattern Subscriber", () => {
receivedMessages.push(message);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

const testMessage: TestMessage = {
user: "testUser",
Expand Down Expand Up @@ -52,7 +52,7 @@ describe("Pattern Subscriber", () => {
messages.push(data);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

await redis.publish("chat:room1:messages", { msg: "Hello Room 1" });
await redis.publish("chat:room2:messages", { msg: "Hello Room 2" });
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("Pattern Subscriber", () => {
messages[pattern].push({ channel, message });
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

await redis.publish("user:123", { type: "user" });
await redis.publish("chat:room1", { type: "chat" });
Expand All @@ -108,7 +108,7 @@ describe("Pattern Subscriber", () => {
messages[pattern].push({ channel, message });
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

// Initial messages
await redis.publish("user:123", { msg: "user1" });
Expand Down Expand Up @@ -153,7 +153,8 @@ describe("Pattern Subscriber", () => {
channelMessages.push(message);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await patternSubscriber.ready;
await channelSubscriber.ready;

const testMessage: TestMessage = { msg: "Hello" };
await redis.publish("user:123", testMessage);
Expand Down
11 changes: 6 additions & 5 deletions pkg/commands/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Subscriber", () => {
});

// Wait for subscription to establish
await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

const testMessage = {
user: "testUser",
Expand All @@ -42,7 +42,7 @@ describe("Subscriber", () => {
receivedMessages.push(data.message);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

const messages = [
{ user: "user1", message: "First", timestamp: Date.now() },
Expand Down Expand Up @@ -72,7 +72,7 @@ describe("Subscriber", () => {
channelMessages.push(data.message);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

const testMessage = {
user: "testUser",
Expand Down Expand Up @@ -100,7 +100,8 @@ describe("Subscriber", () => {
subscriber1.on("message", (data) => messages1.push(data.message));
subscriber2.on("message", (data) => messages2.push(data.message));

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber1.ready;
await subscriber2.ready;

const testMessage = {
user: "testUser",
Expand Down Expand Up @@ -131,7 +132,7 @@ describe("Subscriber", () => {
messages[data.channel].push(data.message);
});

await new Promise((resolve) => setTimeout(resolve, 500));
await subscriber.ready;

// Send initial messages to both channels
await redis.publish("channel1", { test: "before1" });
Expand Down
19 changes: 10 additions & 9 deletions pkg/commands/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,17 @@ export class Subscriber<TMessage = any> extends EventTarget {
private subscriptions: Map<string, SubscriptionInfo>;
private client: Requester;
private listeners: Map<string, Set<Listener<TMessage, any>>>;
public ready: Promise<void>;

constructor(client: Requester, channels: string[], isPattern: boolean = false) {
super();
this.client = client;
this.subscriptions = new Map();
this.listeners = new Map();

for (const channel of channels) {
if (isPattern) {
this.subscribeToPattern(channel);
} else {
this.subscribeToChannel(channel);
}
}
this.ready = isPattern
? Promise.all(channels.map((c) => this.subscribeToPattern(c))).then(() => {})
: Promise.all(channels.map((c) => this.subscribeToChannel(c))).then(() => {});
}

private subscribeToChannel(channel: string) {
Expand All @@ -66,7 +63,7 @@ export class Subscriber<TMessage = any> extends EventTarget {
},
});

command.exec(this.client).catch((error) => {
const commandPromise = command.exec(this.client).catch((error) => {
if (error.name !== "AbortError") {
this.dispatchToListeners("error", error);
}
Expand All @@ -77,6 +74,8 @@ export class Subscriber<TMessage = any> extends EventTarget {
controller,
isPattern: false,
});

return commandPromise;
}

private subscribeToPattern(pattern: string) {
Expand All @@ -89,7 +88,7 @@ export class Subscriber<TMessage = any> extends EventTarget {
},
});

command.exec(this.client).catch((error) => {
const commandPromise = command.exec(this.client).catch((error) => {
if (error.name !== "AbortError") {
this.dispatchToListeners("error", error);
}
Expand All @@ -100,6 +99,8 @@ export class Subscriber<TMessage = any> extends EventTarget {
controller,
isPattern: true,
});

return commandPromise;
}

private handleMessage(data: string, isPattern: boolean) {
Expand Down