Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions pkg/commands/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ describe("Subscriber", () => {
});

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

const testMessage = {
user: "testUser",
message: "Hello, World!",
timestamp: Date.now(),
};
const testMessage = "really?";

await redis.publish(channel, testMessage);
await new Promise((resolve) => setTimeout(resolve, 500));
await new Promise((resolve) => setTimeout(resolve, 1000));

expect(receivedMessages).toHaveLength(1);
expect(receivedMessages[0]).toEqual(testMessage);
Expand Down
12 changes: 11 additions & 1 deletion pkg/commands/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ export class Subscriber<TMessage = any> extends EventTarget {
this.dispatchToListeners(type, count);
} else {
const message =
this.opts?.automaticDeserialization === false ? messageStr : JSON.parse(messageStr);
this.opts?.automaticDeserialization === false
? messageStr
: parseWithTryCatch(messageStr);

this.dispatchToListeners(type, { channel, message });
this.dispatchToListeners(`${type}:${channel}`, { channel, message });
Expand Down Expand Up @@ -241,3 +243,11 @@ export class SubscribeCommand extends Command<number, number> {
});
}
}

const parseWithTryCatch = (str: string) => {
try {
return JSON.parse(str);
} catch {
return str;
}
};
Loading