Skip to content

Commit

Permalink
Remove channels
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Jul 15, 2024
1 parent 9fa0eb1 commit 9bc67d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
10 changes: 1 addition & 9 deletions dist/TSRedis.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import { type RedisOptions } from 'ioredis';
import { z } from 'zod';
declare const channels: z.ZodEnum<["main"]>;
declare const messageSchema: z.ZodUnion<[z.ZodObject<{
type: z.ZodLiteral<"new_patron">;
tier: z.ZodNumber;
channel: z.ZodEnum<["main"]>;
}, "strip", z.ZodTypeAny, {
type: "new_patron";
tier: number;
channel: "main";
}, {
type: "new_patron";
tier: number;
channel: "main";
}>, z.ZodObject<{
type: z.ZodLiteral<"ping">;
channel: z.ZodEnum<["main"]>;
}, "strip", z.ZodTypeAny, {
type: "ping";
channel: "main";
}, {
type: "ping";
channel: "main";
}>]>;
type Message = z.infer<typeof messageSchema>;
type Channel = z.infer<typeof channels>;
export declare class TSRedis {
private redis;
constructor(options?: RedisOptions & {
mocked: boolean;
});
subscribe(channel: Channel, callback: (message: Message) => void): void;
subscribe(callback: (message: Message) => void): void;
publish(message: Message): void;
}
export {};
Expand Down
16 changes: 7 additions & 9 deletions dist/TSRedis.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions src/TSRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@ import Redis, { type RedisOptions } from 'ioredis';
import MockRedis from 'ioredis-mock';
import { z } from 'zod';

const channels = z.enum(['main']);

const newPatronMessageSchema = z.object({
type: z.literal('new_patron'),
tier: z.number().int(),
channel: channels
tier: z.number().int()
});

const pingMessageSchema = z.object({
type: z.literal('ping'),
channel: channels
type: z.literal('ping')
});

const messageSchema = z.union([newPatronMessageSchema, pingMessageSchema]);

type Message = z.infer<typeof messageSchema>;
type Channel = z.infer<typeof channels>;

const CHANNEL_ID = 'main';
export class TSRedis {
private redis: Redis;

constructor(options: RedisOptions & { mocked: boolean } = { mocked: false }) {
this.redis = options.mocked ? new MockRedis(options) : new Redis(options);
}

subscribe(channel: Channel, callback: (message: Message) => void) {
this.redis.subscribe(channel, (err, count) => {
subscribe(callback: (message: Message) => void) {
this.redis.subscribe(CHANNEL_ID, (err, count) => {
if (err) {
console.error('Failed to subscribe: ', err);
} else {
Expand All @@ -37,7 +33,7 @@ export class TSRedis {
});

this.redis.on('message', (receivedChannel, message) => {
if (receivedChannel === channel) {
if (receivedChannel === CHANNEL_ID) {
try {
const parsedMessage = JSON.parse(message);
const validatedMessage = messageSchema.parse(parsedMessage);
Expand All @@ -51,6 +47,6 @@ export class TSRedis {

publish(message: Message) {
const parsedMessage = messageSchema.parse(message);
this.redis.publish(parsedMessage.channel, JSON.stringify(parsedMessage));
this.redis.publish(CHANNEL_ID, JSON.stringify(parsedMessage));
}
}

0 comments on commit 9bc67d4

Please sign in to comment.