import Emittery from 'emittery'; import type { Redis, Cluster } from 'ioredis'; import type { PubSubOptions, ConnectionEvents, PubSubChannelHandler, PubSubPatternHandler } from '../types.ts'; /** * Abstract factory implements the shared functionality required by Redis cluster * and the normal Redis connections. * * @example * ```ts * class MyConnection extends AbstractConnection { * protected makeSubscriberConnection() { * // Implementation specific to your connection type * } * } * ``` */ export declare abstract class AbstractConnection> extends Emittery { connectionName: string; /** * Reference to the main ioRedis connection */ ioConnection: T; /** * Reference to the main ioRedis subscriber connection */ ioSubscriberConnection?: T; /** * A list of active subscriptions */ protected subscriptions: Map>; /** * A list of active pattern subscriptions */ protected psubscriptions: Map>; /** * The last error emitted by the `error` event. We set it to `null` after * the `ready` event */ lastError?: any; /** * Returns status of the main connection */ get status(): "connect" | "wait" | "end" | "close" | "connecting" | "ready" | "reconnecting" | "disconnecting"; /** * Returns status of the subscriber connection or * undefined when there is no subscriber * connection */ get subscriberStatus(): "connect" | "wait" | "end" | "close" | "connecting" | "ready" | "reconnecting" | "disconnecting" | undefined; /** * Get the number of commands queued in automatic pipelines. * This is not available (and returns 0) until the cluster is connected and slots information have been received. */ get autoPipelineQueueSize(): number; /** * Returns a boolean notifying if the connection is * in connecting state * * @example * ```ts * if (connection.isConnecting()) { * console.log('Connection is establishing...') * } * ``` */ isConnecting(): boolean; /** * Returns a boolean notifying if the connection is in * ready state * * @example * ```ts * if (connection.isReady()) { * await connection.ioConnection.set('key', 'value') * } * ``` */ isReady(): boolean; /** * Returns a boolean notifying if the connection has been closed * * @example * ```ts * if (connection.isClosed()) { * console.log('Connection is closed') * } * ``` */ isClosed(): boolean; /** * Parent class must implement makeSubscriberConnection */ protected abstract makeSubscriberConnection(): void; /** * Create a new AbstractConnection instance * * @param connectionName - Name identifier for this connection * * @example * ```ts * class MyConnection extends AbstractConnection { * constructor() { * super('main') * } * } * ``` */ constructor(connectionName: string); /** * Monitoring the redis connection via event emitter to cleanup * things properly and also notify subscribers of this class */ protected monitorConnection(): void; /** * Monitoring the subscriber connection via event emitter to * cleanup things properly and also notify subscribers of * this class. */ protected monitorSubscriberConnection(): void; /** * Setting up the subscriber connection. The method results * in a noop when a connection already exists. */ protected setupSubscriberConnection(): void; /** * Gracefully end the redis connection * * @example * ```ts * await connection.quit() * ``` */ quit(): Promise; /** * Forcefully end the redis connection * * @example * ```ts * await connection.disconnect() * ``` */ disconnect(): Promise; /** * Subscribe to a given channel to receive Redis pub/sub events. A * new subscriber connection will be created/managed automatically. * * @param channel - The channel name to subscribe to * @param handler - Function to handle received messages * @param options - Optional subscription configuration * * @example * ```ts * connection.subscribe('notifications', (message) => { * console.log('Received:', message) * }) * ``` */ subscribe(channel: string, handler: PubSubChannelHandler, options?: PubSubOptions): void; /** * Unsubscribe from a channel * * @param channel - The channel name to unsubscribe from * @param handler - Optional specific handler to remove * * @example * ```ts * await connection.unsubscribe('notifications') * ``` */ unsubscribe(channel: string, handler?: PubSubChannelHandler): Promise; /** * Make redis subscription for a pattern * * @param pattern - The pattern to subscribe to * @param handler - Function to handle received pattern messages * @param options - Optional subscription configuration * * @example * ```ts * connection.psubscribe('news.*', (channel, message) => { * console.log(`Channel ${channel}:`, message) * }) * ``` */ psubscribe(pattern: string, handler: PubSubPatternHandler, options?: PubSubOptions): void; /** * Unsubscribe from a given pattern * * @param pattern - The pattern to unsubscribe from * @param handler - Optional specific handler to remove * * @example * ```ts * await connection.punsubscribe('news.*') * ``` */ punsubscribe(pattern: string, handler?: PubSubPatternHandler): Promise; /** * Publish the pub/sub message * * @param channel - The channel to publish to * @param message - The message to publish * @param callback - Optional callback for completion * * @example * ```ts * // Promise-based * const count = await connection.publish('notifications', 'Hello World') * * // Callback-based * connection.publish('notifications', 'Hello World', (err, count) => { * console.log('Published to', count, 'subscribers') * }) * ``` */ publish(channel: string, message: string, callback: (error: Error | null | undefined, count: number | undefined) => void): void; publish(channel: string, message: string): Promise; /** * Define a custom command using LUA script. You can run the * registered command using the "runCommand" method. * * @param args - Arguments for defining the command * * @example * ```ts * connection.defineCommand('myCommand', { * numberOfKeys: 1, * lua: 'return redis.call("get", KEYS[1])' * }) * ``` */ defineCommand(...args: Parameters): this; /** * Run a pre registered command * * @param command - The name of the registered command * @param args - Arguments to pass to the command * * @example * ```ts * const result = await connection.runCommand('myCommand', 'key1') * ``` */ runCommand(command: string, ...args: any[]): any; }