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

pub/sub type safety improvements #510

Open
cainlevy opened this issue Apr 13, 2022 · 0 comments
Open

pub/sub type safety improvements #510

cainlevy opened this issue Apr 13, 2022 · 0 comments

Comments

@cainlevy
Copy link

I'm working to improve the type safety of our RedisPubSub integration and have found a simple pattern that I believe may be useful to other people.

Problem

The distance between publishing and subscribing/consuming makes it easy to introduce typos and mismatched payloads.

Workaround

Define a list of trigger names with associated payload types:

type TriggeredPayloads = {
  helloWorld: string;
};

type TypedRedisPubSub = RedisPubSub & {
  publish: <T extends keyof TriggeredPayloads>(
    topic: T,
    payload: TriggeredPayloads[T],
  ) => Promise<void>;
  asyncIterator: <T extends keyof TriggeredPayloads>(topic: T) => AsyncIterator<T>;
};

const myPubSub = new RedisPubSub() as TypedRedisPubSub;

// ok
myPubSub.publish('helloWorld', 'Bonjour');
// not ok
myPubSub.publish('helloworld', 'Bonjour');
myPubSub.publish('helloWorld', true);

// ok
myPubSub.asyncIterator('helloWorld');
// not ok
myPubSub.asyncIterator('helloworld');

Feature Request

It would be nice if the RedisPubSub constructor accepted a generic containing a record of supported triggers and payloads:

type TriggeredPayloads = {
  helloWorld: string;
};
const myPubSub = new RedisPubSub<TriggeredPayloads>();

The optimalbits/bull library has a similar problem (message passing between job producers & workers) and also implements a solution like this.

Caveats

  • This only helps codebases that contain both the publisher and the subscriber
  • This does not solve race conditions during a deploy where the new version includes payload changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant