Skip to content

Commit

Permalink
Add RealtimeChannel.unsubscribeAll and `RealtimePresence.unsubscrib…
Browse files Browse the repository at this point in the history
…eAll`

Also deprecates the ability to call `.unsubscribe()` methods without
any valid parameters.
  • Loading branch information
VeskeR committed Jun 13, 2024
1 parent ebaea38 commit 1d3f09d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
14 changes: 12 additions & 2 deletions ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1847,9 +1847,14 @@ export declare interface RealtimePresence {
unsubscribe(listener: messageCallback<PresenceMessage>): void;
/**
* Deregisters all listeners currently receiving {@link PresenceMessage} for the channel.
*
* @deprecated This method overload is deprecated and will be removed in a future version. Use the {@link RealtimePresence.unsubscribeAll | `unsubscribeAll()`} method instead.
*/
unsubscribe(): void;

/**
* Deregisters all listeners currently receiving {@link PresenceMessage} for the channel.
*/
unsubscribeAll(): void;
/**
* 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.
*
Expand Down Expand Up @@ -2044,9 +2049,14 @@ export declare interface RealtimeChannel extends EventEmitter<channelEventCallba
unsubscribe(listener: messageCallback<InboundMessage>): void;
/**
* Deregisters all listeners to messages on this channel. This removes all earlier subscriptions.
*
* @deprecated This method overload is deprecated and will be removed in a future version. Use the {@link RealtimeChannel.unsubscribeAll | `unsubscribeAll()`} method instead.
*/
unsubscribe(): void;

/**
* Deregisters all listeners to messages on this channel. This removes all earlier subscriptions.
*/
unsubscribeAll(): void;
/**
* A {@link RealtimePresence} object.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,20 @@ class RealtimeChannel extends EventEmitter {
return;
}

if (event === undefined && listener === undefined) {
this.logger.deprecated(
'The ability to call `RealtimeChannel.unsubscribe()` without any valid parameters',
'Please use `RealtimeChannel.unsubscribeAll()` method if you want to deregister all listeners to messages on the channel.',
);
}

this.subscriptions.off(event, listener);
}

unsubscribeAll(): void {
this.subscriptions.off();
}

sync(): void {
/* check preconditions */
switch (this.state) {
Expand Down
12 changes: 12 additions & 0 deletions src/common/lib/client/realtimepresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,20 @@ class RealtimePresence extends EventEmitter {
const args = RealtimeChannel.processListenerArgs(_args);
const event = args[0];
const listener = args[1];

if (event === undefined && listener === undefined) {
this.logger.deprecated(
'The ability to call `RealtimePresence.unsubscribe()` without any valid parameters',
'Please use `RealtimePresence.unsubscribeAll()` method if you want to deregister all listeners currently receiving presence messages for the channel.',
);
}

this.subscriptions.off(event, listener);
}

unsubscribeAll(): void {
this.subscriptions.off();
}
}

class PresenceMap extends EventEmitter {
Expand Down
2 changes: 1 addition & 1 deletion test/package/browser/template/src/index-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ globalThis.testAblyPackage = async function () {
// Check that id and timestamp of a message received from Ably can be assigned to non-optional types
const { id: string, timestamp: number } = receivedMessage;

channel.unsubscribe();
channel.unsubscribeAll();

// Check that we can use the TypeScript overload that accepts a Message object
await channel.publish({ name: 'message', data: { foo: 'bar' } });
Expand Down
2 changes: 1 addition & 1 deletion test/package/browser/template/src/index-modular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ globalThis.testAblyPackage = async function () {

await checkStandaloneFunction();

channel.unsubscribe();
channel.unsubscribeAll();

// Check that we can use the TypeScript overload that accepts a Message object
await channel.publish({ name: 'message', data: { foo: 'bar' } });
Expand Down
2 changes: 1 addition & 1 deletion test/realtime/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var unsubscribeTest = function () {
channelByEvent.unsubscribe('event', listenerByEvent);
channelByListener.unsubscribe(listenerNoEvent);
channelAll.unsubscribe();
channelAll.unsubscribeAll();
whenPromiseSettles(channelByEvent.publish('event', 'data'), function (err) {
try {
expect(!err, 'Error publishing single event: ' + err).to.be.ok;
Expand Down
2 changes: 1 addition & 1 deletion test/realtime/presence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
},
function (cb) {
channel.presence.subscribe(function (presmsg) {
channel.presence.unsubscribe();
channel.presence.unsubscribeAll();
try {
expect(presmsg.action).to.equal('leave', 'Check action was leave');
expect(presmsg.clientId).to.equal(goneClientId, 'Check goneClient has left');
Expand Down

0 comments on commit 1d3f09d

Please sign in to comment.