Skip to content

Commit 31891bf

Browse files
umair-ablyclaude
andcommitted
DX-1211: rewrite RealtimePresence interface docstrings (prerequisites, side-effects, failure modes)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 1d45ea3 commit 31891bf

1 file changed

Lines changed: 83 additions & 19 deletions

File tree

ably.d.ts

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,51 +2155,69 @@ export declare interface Presence {
21552155
*/
21562156
export declare interface RealtimePresence {
21572157
/**
2158-
* Indicates whether the presence set synchronization between Ably and the clients on the channel has been completed. Set to `true` when the sync is complete.
2158+
* Indicates whether the presence set synchronization between Ably and the clients on the channel has been completed. Set to `true` when the sync is complete, and back to `false` whenever a new sync starts (typically after a (re)attach). The value is also `true` after the local presence set is cleared (when the channel becomes detached or failed, or attaches without the server reporting any presence members), so it indicates only that no sync is in progress (though it is initially `false`, before any sync has started), not that the channel is attached or that a sync ever ran. To wait for an in-progress sync, call {@link RealtimePresence.get | `get()`}, which by default resolves only once the sync completes.
2159+
*
2160+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#sync-complete
21592161
*/
21602162
syncComplete: boolean;
21612163
/**
2162-
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel for a given {@link PresenceAction}.
2164+
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel for a given {@link PresenceAction}. This only removes the local listener; it does not detach the channel or remove this client from the presence set (use {@link RealtimePresence.leave | `leave()`} for that), and presence events may continue to arrive for any other registered listeners.
21632165
*
21642166
* @param presence - A specific {@link PresenceAction} to deregister the listener for.
21652167
* @param listener - An event listener function.
2168+
* @example
2169+
* ```ts
2170+
* channel.presence.unsubscribe('enter', listener);
2171+
* ```
2172+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21662173
*/
21672174
unsubscribe(presence: PresenceAction, listener: messageCallback<PresenceMessage>): void;
21682175
/**
2169-
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel for a given array of {@link PresenceAction} objects.
2176+
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel for a given array of {@link PresenceAction} objects. This only removes the local listener and does not detach the channel.
21702177
*
21712178
* @param presence - An array of {@link PresenceAction} objects to deregister the listener for.
21722179
* @param listener - An event listener function.
2180+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21732181
*/
21742182
unsubscribe(presence: Array<PresenceAction>, listener: messageCallback<PresenceMessage>): void;
21752183
/**
2176-
* Deregisters any listener that is registered to receive {@link PresenceMessage} on the channel for a specific {@link PresenceAction}
2184+
* Deregisters any listener that is registered to receive {@link PresenceMessage} on the channel for a specific {@link PresenceAction}. This only removes the local listeners and does not detach the channel.
21772185
*
21782186
* @param presence - A specific {@link PresenceAction} to deregister the listeners for.
2187+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21792188
*/
21802189
unsubscribe(presence: PresenceAction): void;
21812190
/**
2182-
* Deregisters any listener that is registered to receive {@link PresenceMessage} on the channel for an array of {@link PresenceAction} objects
2191+
* Deregisters any listener that is registered to receive {@link PresenceMessage} on the channel for an array of {@link PresenceAction} objects. This only removes the local listeners and does not detach the channel.
21832192
*
21842193
* @param presence - An array of {@link PresenceAction} objects to deregister the listeners for.
2194+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21852195
*/
21862196
unsubscribe(presence: Array<PresenceAction>): void;
21872197
/**
2188-
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel.
2198+
* Deregisters a specific listener that is registered to receive {@link PresenceMessage} on the channel. This only removes the local listener and does not detach the channel.
21892199
*
21902200
* @param listener - An event listener function.
2201+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21912202
*/
21922203
unsubscribe(listener: messageCallback<PresenceMessage>): void;
21932204
/**
2194-
* Deregisters all listeners currently receiving {@link PresenceMessage} for the channel.
2205+
* Deregisters all listeners currently receiving {@link PresenceMessage} for the channel. This only removes the local listeners and does not detach the channel or remove this client from the presence set, so the channel stays attached and an entered client remains present.
2206+
*
2207+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#unsubscribe
21952208
*/
21962209
unsubscribe(): void;
21972210

21982211
/**
2199-
* Retrieves the current members present on the channel and the metadata for each member, such as their {@link PresenceAction} and ID. Returns an array of {@link PresenceMessage} objects.
2212+
* Retrieves the current members present on the channel and the metadata for each member, such as their {@link PresenceAction} and ID. Implicitly attaches the channel if it is not already attached. Requires the `presence_subscribe` mode (granted by default unless {@link ChannelOptions.modes} excludes it); without it the call resolves with an empty array rather than rejecting (or it rejects with a hinted {@link ErrorInfo} when {@link ClientOptions.strictMode} is enabled).
22002213
*
22012214
* @param params - A set of parameters which are used to specify which presence members should be retrieved.
22022215
* @returns A promise which, upon success, will be fulfilled with an array of {@link PresenceMessage} objects. Upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
2216+
* @example
2217+
* ```ts
2218+
* const members = await channel.presence.get();
2219+
* ```
2220+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#get
22032221
*/
22042222
get(params?: RealtimePresenceParams): Promise<PresenceMessage[]>;
22052223
/**
@@ -2217,10 +2235,16 @@ export declare interface RealtimePresence {
22172235
*/
22182236
get(params: RealtimePresenceParams | null, callback: StandardCallback<PresenceMessage[]>): void;
22192237
/**
2220-
* Retrieves a {@link PaginatedResult} object, containing an array of historical {@link PresenceMessage} objects for the channel. If the channel is configured to persist messages, then presence messages can be retrieved from history for up to 72 hours in the past. If not, presence messages can only be retrieved from history for up to two minutes in the past.
2238+
* Retrieves a {@link PaginatedResult} object, containing an array of historical {@link PresenceMessage} objects for the channel. Presence messages are retrievable for up to 72 hours in the past when message persistence is enabled for the channel by a channel rule; without it, only presence messages from the last two minutes, the service's default retention, are returned.
22212239
*
22222240
* @param params - A set of parameters which are used to specify which presence messages should be retrieved.
22232241
* @returns A promise which, upon success, will be fulfilled with a {@link PaginatedResult} object containing an array of {@link PresenceMessage} objects. Upon failure, the promise will be rejected with an {@link ErrorInfo} object which explains the error.
2242+
* @example
2243+
* ```ts
2244+
* const result = await channel.presence.history();
2245+
* ```
2246+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#history
2247+
* @see https://ably.com/docs/storage-history/storage
22242248
*/
22252249
history(params?: RealtimeHistoryParams): Promise<PaginatedResult<PresenceMessage>>;
22262250
/**
@@ -2238,18 +2262,28 @@ export declare interface RealtimePresence {
22382262
*/
22392263
history(params: RealtimeHistoryParams | null, callback: StandardCallback<PaginatedResult<PresenceMessage>>): void;
22402264
/**
2241-
* Registers a listener that is called each time a {@link PresenceMessage} matching a given {@link PresenceAction}, or an action within an array of {@link PresenceAction | `PresenceAction`s}, is received on the channel, such as a new member entering the presence set.
2265+
* Registers a listener that is called each time a {@link PresenceMessage} matching a given {@link PresenceAction}, or an action within an array of {@link PresenceAction | `PresenceAction`s}, is received on the channel, such as a new member entering the presence set. Implicitly attaches the channel unless {@link ChannelOptions.attachOnSubscribe} is `false`. Requires the `presence_subscribe` mode (granted by default unless {@link ChannelOptions.modes} excludes it); if the channel attaches without it the server never delivers presence events, so the listener silently never fires: the call still resolves and nothing is logged.
22422266
*
22432267
* @param action - A {@link PresenceAction} or an array of {@link PresenceAction | `PresenceAction`s} to register the listener for.
22442268
* @param listener - An event listener function.
2245-
* @returns A promise which resolves upon success of the channel {@link RealtimeChannel.attach | `attach()`} operation and rejects with an {@link ErrorInfo} object upon its failure.
2269+
* @returns A promise which resolves upon success of the channel {@link RealtimeChannel.attach | `attach()`} operation and rejects with an {@link ErrorInfo} object upon its failure. When {@link ChannelOptions.attachOnSubscribe} is `false`, no attach is performed.
2270+
* @example
2271+
* ```ts
2272+
* await channel.presence.subscribe('enter', (member) => console.log(member.clientId));
2273+
* ```
2274+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#subscribe
22462275
*/
22472276
subscribe(action: PresenceAction | Array<PresenceAction>, listener?: messageCallback<PresenceMessage>): Promise<void>;
22482277
/**
2249-
* Registers a listener that is called each time a {@link PresenceMessage} is received on the channel, such as a new member entering the presence set.
2278+
* Registers a listener that is called each time a {@link PresenceMessage} is received on the channel, such as a new member entering the presence set. Implicitly attaches the channel unless {@link ChannelOptions.attachOnSubscribe} is `false`. Requires the `presence_subscribe` mode (granted by default unless {@link ChannelOptions.modes} excludes it); if the channel attaches without it the server never delivers presence events, so the listener silently never fires: the call still resolves and nothing is logged.
22502279
*
22512280
* @param listener - An event listener function.
2252-
* @returns A promise which resolves upon success of the channel {@link RealtimeChannel.attach | `attach()`} operation and rejects with an {@link ErrorInfo} object upon its failure.
2281+
* @returns A promise which resolves upon success of the channel {@link RealtimeChannel.attach | `attach()`} operation and rejects with an {@link ErrorInfo} object upon its failure. When {@link ChannelOptions.attachOnSubscribe} is `false`, no attach is performed.
2282+
* @example
2283+
* ```ts
2284+
* await channel.presence.subscribe((member) => console.log(member.clientId));
2285+
* ```
2286+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#subscribe
22532287
*/
22542288
subscribe(listener?: messageCallback<PresenceMessage>): Promise<void>;
22552289
/**
@@ -2272,10 +2306,15 @@ export declare interface RealtimePresence {
22722306
callback: ErrorCallback,
22732307
): void;
22742308
/**
2275-
* Enters the presence set for the channel, optionally passing a `data` payload. A `clientId` is required to be present on a channel.
2309+
* Enters the presence set for the channel, optionally passing a `data` payload. A `clientId` is required: if the client has no `clientId` (or the wildcard `*`), the call rejects with an {@link ErrorInfo}; set a `clientId` in {@link ClientOptions} or in the token, or use {@link RealtimePresence.enterClient | `enterClient()`} to enter on behalf of another identity. Implicitly attaches the channel if it is not already attached. Once entered, the member is automatically re-entered whenever the channel re-attaches after a disconnection; if that re-enter fails, the failure surfaces as a channel `update` event carrying the {@link ErrorInfo}, not as a rejection.
22762310
*
22772311
* @param data - The payload associated with the presence member.
22782312
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2313+
* @example
2314+
* ```ts
2315+
* await channel.presence.enter({ status: 'online' });
2316+
* ```
2317+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#enter
22792318
*/
22802319
enter(data?: any): Promise<void>;
22812320
/**
@@ -2293,10 +2332,15 @@ export declare interface RealtimePresence {
22932332
*/
22942333
enter(data: any, callback: ErrorCallback): void;
22952334
/**
2296-
* Updates the `data` payload for a presence member. If called before entering the presence set, this is treated as an {@link PresenceActions.ENTER} event.
2335+
* Updates the `data` payload for a presence member. If called before entering the presence set, this is treated as an {@link PresenceActions.ENTER} event. Requires an identified client: if the client has no `clientId` (or the wildcard `*`) set in the {@link ClientOptions} or the token, the call rejects with an {@link ErrorInfo} (use {@link RealtimePresence.updateClient | `updateClient()`} to update on behalf of another identity). Implicitly attaches the channel if it is not already attached.
22972336
*
22982337
* @param data - The payload to update for the presence member.
22992338
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2339+
* @example
2340+
* ```ts
2341+
* await channel.presence.update({ status: 'busy' });
2342+
* ```
2343+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#update
23002344
*/
23012345
update(data?: any): Promise<void>;
23022346
/**
@@ -2314,10 +2358,15 @@ export declare interface RealtimePresence {
23142358
*/
23152359
update(data: any, callback: ErrorCallback): void;
23162360
/**
2317-
* Leaves the presence set for the channel. A client must have previously entered the presence set before they can leave it.
2361+
* Leaves the presence set for the channel. A client must have previously entered the presence set before they can leave it. Requires the client to be identified: when the `clientId` is unset or a wildcard, the call rejects with an {@link ErrorInfo}; to leave on behalf of another identity use {@link RealtimePresence.leaveClient | `leaveClient()`}, which requires a wildcard `clientId` on the API key or token. Unlike {@link RealtimePresence.enter | `enter()`}, leaving does not implicitly attach the channel: the call proceeds only while the channel is `attached` or `attaching`, and rejects with an {@link ErrorInfo} in any other channel state or when the connection is unusable.
23182362
*
23192363
* @param data - The payload associated with the presence member.
23202364
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2365+
* @example
2366+
* ```ts
2367+
* await channel.presence.leave();
2368+
* ```
2369+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#leave
23212370
*/
23222371
leave(data?: any): Promise<void>;
23232372
/**
@@ -2335,27 +2384,42 @@ export declare interface RealtimePresence {
23352384
*/
23362385
leave(data: any, callback: ErrorCallback): void;
23372386
/**
2338-
* Enters the presence set of the channel for a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`.
2387+
* Enters the presence set of the channel for a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`; this is enforced by the server rather than the client, so without it the call rejects with an {@link ErrorInfo} returned by the server. Implicitly attaches the channel if it is not already attached. After a transient disconnection the library automatically re-enters the member on re-attach; if that re-enter fails, the failure surfaces as a channel `update` event carrying the {@link ErrorInfo}, not as a rejection.
23392388
*
23402389
* @param clientId - The ID of the client to enter into the presence set.
23412390
* @param data - The payload associated with the presence member.
23422391
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2392+
* @example
2393+
* ```ts
2394+
* await channel.presence.enterClient('bob', { status: 'online' });
2395+
* ```
2396+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#enter-client
23432397
*/
23442398
enterClient(clientId: string, data?: any): Promise<void>;
23452399
/**
2346-
* Updates the `data` payload for a presence member using a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`.
2400+
* Updates the `data` payload for a presence member using a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`; this is enforced by the server rather than the client, so without it the call rejects with an {@link ErrorInfo} returned by the server. Implicitly attaches the channel if it is not already attached.
23472401
*
23482402
* @param clientId - The ID of the client to update in the presence set.
23492403
* @param data - The payload to update for the presence member.
23502404
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2405+
* @example
2406+
* ```ts
2407+
* await channel.presence.updateClient('bob', { status: 'busy' });
2408+
* ```
2409+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#update-client
23512410
*/
23522411
updateClient(clientId: string, data?: any): Promise<void>;
23532412
/**
2354-
* Leaves the presence set of the channel for a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`.
2413+
* Leaves the presence set of the channel for a given `clientId`. Enables a single client to update presence on behalf of any number of clients using a single connection. The library must have been instantiated with an API key or a token bound to a wildcard `clientId`; this is enforced by the server rather than the client, so without it the call rejects with an {@link ErrorInfo} returned by the server. Unlike {@link RealtimePresence.enterClient | `enterClient()`}, this call does not implicitly attach the channel; when the channel is neither attached nor attaching it rejects with an {@link ErrorInfo} rather than attaching just to leave.
23552414
*
23562415
* @param clientId - The ID of the client to leave the presence set for.
23572416
* @param data - The payload associated with the presence member.
23582417
* @returns A promise which resolves upon success of the operation and rejects with an {@link ErrorInfo} object upon its failure.
2418+
* @example
2419+
* ```ts
2420+
* await channel.presence.leaveClient('bob');
2421+
* ```
2422+
* @see https://ably.com/docs/pub-sub/api/javascript/realtime/realtime-presence#leave-client
23592423
*/
23602424
leaveClient(clientId: string, data?: any): Promise<void>;
23612425
}

0 commit comments

Comments
 (0)