import { ClientIdResolver } from './client-id.js'; import { Logger } from './logger.js'; import { Room } from './room.js'; import { RoomOptions } from './room-options.js'; import * as Ably from '@ably/pubsub-core'; /** * Manages the lifecycle of chat rooms. */ export interface Rooms { /** * Gets a room reference by its unique identifier. * * Creates a new room instance or returns an existing one. The Rooms class ensures * only one instance exists per room name. Always call `release()` when the room * is no longer needed to free resources. * * **Note**: * - If options differ from an existing room, an error is thrown. * - If `get` is called during a release, it waits for release to complete. * - If `release` is called before `get` resolves, the promise rejects. * @param name - The unique identifier of the room * @param options - Optional configuration for the room features * @returns Promise resolving to the Room instance, or rejecting with: * - {@link ErrorCode.RoomExistsWithDifferentOptions} if room exists with different options * - {@link ErrorCode.ResourceDisposed} if the rooms instance has been disposed * - {@link ErrorCode.RoomReleasedBeforeOperationCompleted} if room is released before get completes * @example * ``` * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, Room } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Get a room with default options * const room = await chatClient.rooms.get('general-chat'); * * // Always release when done * await chatClient.rooms.release('general-chat'); * * // Handle errors when options conflict * try { * // This will throw if 'game-room' already exists with different options * const room1 = await chatClient.rooms.get('game-room', { * typing: { heartbeatThrottleMs: 1000 } * }); * * const room2 = await chatClient.rooms.get('game-room', { * typing: { heartbeatThrottleMs: 2000 } // Different options! * }); * } catch (error) { * if (error.code === 40000) { * console.error('Room already exists with different options'); * } * } * ``` */ get(name: string, options?: RoomOptions): Promise; /** * Releases a room, freeing its resources and detaching it from Ably. * * After release, the room object is no longer usable. To use the room again, * call `get()` to create a new instance. This method only releases the reference * and detaches from Ably; it doesn't unsubscribe existing event listeners. * * **Note**: * - Calling release aborts any in-progress `get` calls for the same room. * - The room object becomes unusable after release. * @param name - The unique identifier of the room to release * @returns Promise that resolves when the room is fully released * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * // Get a room with default options and attach to it * const room = await chatClient.rooms.get('temporary-chat'); * await room.attach(); * * // Do chat operations... * * // When done, release the room * await chatClient.rooms.release('temporary-chat'); * * // The room object is now unusable * try { * await room.messages.send({ text: 'This will fail' }); * } catch (error) { * console.error('Room has been released'); * } * * // To use the room again, get a new instance * const newRoom = await chatClient.rooms.get('temporary-chat'); * * // Handle release of non-existent rooms (no-op) * await chatClient.rooms.release('non-existent-room'); // Safe, does nothing * ``` */ release(name: string): Promise; /** * Disposes all rooms that are currently in the rooms map. * This method releases all rooms concurrently and clears the rooms map. * @returns A promise that resolves when all rooms have been released. */ dispose(): Promise; } /** * Manages the chat rooms. */ export declare class DefaultRooms implements InternalRooms { private readonly _realtime; private readonly _chatApi; private readonly _rooms; private readonly _releasing; private readonly _clientIdResolver; private readonly _logger; private _isReact; private _disposed; /** * Constructs a new Rooms instance. * @param realtime An instance of the Ably Realtime client. * @param clientIdResolver A resolver for the clientId. * @param logger An instance of the Logger. * @param idempotentRestPublishing Whether the ChatApi should attach an idempotency key to outgoing requests. */ constructor(realtime: Ably.PubSubRealtimeClient, clientIdResolver: ClientIdResolver, logger: Logger, idempotentRestPublishing: boolean); /** * @inheritDoc */ get(name: string, options?: RoomOptions): Promise; /** * @inheritDoc */ release(name: string): Promise; /** * @inheritDoc */ get count(): number; /** * Ensures the rooms instance has not been disposed. */ private _ensureNotDisposed; /** * Handles the case where a room already exists. * @param existingRoom The existing room entry in the map. * @param name The unique identifier of the room. * @param options The options for the room. * @returns A promise that resolves to the existing room. */ private _handleExistingRoom; /** * Creates a new room when no existing room or ongoing release exists. * @param name The unique identifier of the room. * @param nonce A random, internal identifier useful for debugging and logging. * @param options The options for the room. * @returns A new room object. */ private _createNewRoom; /** * Waits for an ongoing release to complete, then creates a new room. * @param name The unique identifier of the room. * @param nonce A random, internal identifier useful for debugging and logging. * @param options The options for the room. * @param ongoingRelease The promise of an ongoing release operation. * @returns A promise that resolves to a room. */ private _waitForReleaseAndCreateRoom; /** * Creates a promise that can be aborted if the room is released before completion. * @param name The unique identifier of the room. * @param nonce A random, internal identifier useful for debugging and logging. * @param options The options for the room. * @param ongoingRelease A promise that resolves when the previous release operation is complete. * @param abortController An AbortController to manage the abort signal. * @returns A promise that resolves to a new room or rejects if the operation is aborted. */ private _createAbortableRoomPromise; /** * Handles release when no room exists. * @param name The unique identifier of the room. * @param ongoingRelease An ongoing release promise, if any. * @returns A promise that resolves when the release operation is complete. */ private _handleNonExistentRoomRelease; /** * Handles release when there's already a release in progress. * @param name The unique identifier of the room. * @param existingRoom The existing room entry in the map. * @param ongoingRelease The promise of an ongoing release operation. */ private _handleConcurrentRelease; /** * Performs the actual room release operation. * @param name The unique identifier of the room. * @param existingRoom The existing room entry in the map. */ private _performRoomRelease; /** * Executes the room release and cleanup. * @param name The unique identifier of the room. * @param existingRoom The existing room entry in the map. */ private _executeRoomRelease; /** * makes a new room object * @param name The unique identifier of the room. * @param nonce A random, internal identifier useful for debugging and logging. * @param options The options for the room. * @returns DefaultRoom A new room object. */ private _makeRoom; /** * Sets react JS mode. */ useReact(): void; }