import { Messages, Occupancy, Presence, Room, RoomOptions, RoomReactions, RoomStatus, Typing } from '@ably/chat'; import { jsonRpc, jsonRpcBlocking } from './jsonrpc'; import { RpcMessages } from './RpcMessages'; import { RpcOccupancy } from './RpcOccupancy'; import { RpcPresence } from './RpcPresence'; import { RpcRoomReactions } from './RpcRoomReactions'; import { RpcRoomStatus } from './RpcRoomStatus'; import { RpcTyping } from './RpcTyping'; export class RpcRoom implements Room { private readonly _instanceId: string; private _messages: RpcMessages | undefined; private _presence: RpcPresence | undefined; private _reactions: RpcRoomReactions | undefined; private _typing: RpcTyping | undefined; private _occupancy: RpcOccupancy | undefined; private _status: RpcRoomStatus | undefined; constructor(instanceId: string) { this._instanceId = instanceId; } get roomId(): string { const { response } = jsonRpcBlocking.request('Room#roomId', { refId: this._instanceId }); return response; } get messages(): Messages { if (!this._messages) { const { refId } = jsonRpcBlocking.request('Room#messages', { refId: this._instanceId }); this._messages = new RpcMessages(refId); } return this._messages; } get presence(): Presence { if (!this._presence) { const { refId } = jsonRpcBlocking.request('Room#presence', { refId: this._instanceId }); this._presence = new RpcPresence(refId); } return this._presence; } get reactions(): RoomReactions { if (!this._reactions) { const { refId } = jsonRpcBlocking.request('Room#reactions', { refId: this._instanceId }); this._reactions = new RpcRoomReactions(refId); } return this._reactions; } get typing(): Typing { if (!this._typing) { const { refId } = jsonRpcBlocking.request('Room#typing', { refId: this._instanceId }); this._typing = new RpcTyping(refId); } return this._typing; } get occupancy(): Occupancy { if (!this._occupancy) { const { refId } = jsonRpcBlocking.request('Room#occupancy', { refId: this._instanceId }); this._occupancy = new RpcOccupancy(refId); } return this._occupancy; } get status(): RoomStatus { if (!this._status) { const { refId } = jsonRpcBlocking.request('Room#status', { refId: this._instanceId, }); this._status = new RpcRoomStatus(refId); } return this._status; } async attach(): Promise { await jsonRpc.request('Room.attach', { refId: this._instanceId, }); } async detach(): Promise { await jsonRpc.request('Room.detach', { refId: this._instanceId, }); } options(): RoomOptions { const { response } = jsonRpcBlocking.request('Room.options', { refId: this._instanceId, }); return response; } }