@graphql-hive/pubsub
Version:
48 lines (45 loc) • 2.2 kB
TypeScript
import { DisposableSymbols } from '@whatwg-node/disposablestack';
import { MaybePromise } from '@whatwg-node/promise-helpers';
import { Redis } from 'ioredis';
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
/**
* When a Redis connection enters "subscriber mode" (after calling SUBSCRIBE), it can only execute
* subscriber commands (SUBSCRIBE, UNSUBSCRIBE, etc.). Meaning, it cannot execute other commands like PUBLISH.
* To avoid this, we use two separate Redis clients: one for publishing and one for subscribing.
*/
interface RedisPubSubConnections {
/** The redis instance that publishes events/topics. */
pub: Redis;
/** The redis instance that listens and subscribes to events/topics. */
sub: Redis;
}
interface RedisPubSubOptions {
/**
* Prefix for Redis channels to avoid conflicts.
* Intentionally no default because we don't want to accidentally share channels between different services.
*/
channelPrefix: string;
/**
* By default, when the pub/sub instance is disposed, it will call
* `quit` on both Redis clients. Set this to `true` if you
* want to keep the clients alive after disposal.
*
* This might be useful if you want to manage the Redis clients' lifecycle
* outside of the pub/sub instance.
*
* @default false
*/
noQuitOnDispose?: boolean;
}
/** {@link PubSub Hive PubSub} implementation of [Redis Pub/Sub](https://redis.io/docs/latest/develop/pubsub/). */
declare class RedisPubSub<M extends TopicDataMap = TopicDataMap> implements PubSub<M> {
#private;
constructor(redis: RedisPubSubConnections, options: RedisPubSubOptions);
subscribedTopics(): Promise<(keyof M)[]>;
publish<Topic extends keyof M>(topic: Topic, data: M[Topic]): Promise<void>;
subscribe<Topic extends keyof M>(topic: Topic): AsyncIterable<M[Topic]>;
subscribe<Topic extends keyof M>(topic: Topic, listener: PubSubListener<M, Topic>): MaybePromise<() => MaybePromise<void>>;
dispose(): Promise<void>;
[DisposableSymbols.asyncDispose](): Promise<void>;
}
export { RedisPubSub, type RedisPubSubConnections, type RedisPubSubOptions };