We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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.
The distance between publishing and subscribing/consuming makes it easy to introduce typos and mismatched payloads.
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');
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.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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:
Feature Request
It would be nice if the RedisPubSub constructor accepted a generic containing a record of supported triggers and payloads:
The optimalbits/bull library has a similar problem (message passing between job producers & workers) and also implements a solution like this.
Caveats
The text was updated successfully, but these errors were encountered: