Skip to content

Commit

Permalink
fix: improve channel options reattachment logic
Browse files Browse the repository at this point in the history
This internal change makes the client more selective about which options
provided to `channels.get` will be interpreted as requiring
reattachment. More precisely, the channel params and modes will now be
shallow compared to existing params and modes, and only if they differ
will the call the `chnanels.get` throw an error (, provided of course
that the channel is already attached or attaching).
  • Loading branch information
owenpearson committed Oct 19, 2023
1 parent 65ff5d7 commit f38c5a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,20 @@ class RealtimeChannel extends Channel {
}

_shouldReattachToSetOptions(options?: API.Types.ChannelOptions) {
return (this.state === 'attached' || this.state === 'attaching') && (options?.params || options?.modes);
if (!(this.state === 'attached' || this.state === 'attaching')) {
return false;
}
if (options?.params) {
if (!this.params || !Utils.shallowEquals(this.params, options.params)) {
return true;
}
}
if (options?.modes) {
if (!this.modes || !Utils.arrEquals(options.modes, this.modes)) {
return true;
}
}
return false;
}

publish(...args: any[]): void | Promise<void> {
Expand Down
10 changes: 7 additions & 3 deletions test/realtime/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async

try {
realtime.channels.get(testName, {
params: params,
params: {
modes: 'subscribe',
},
});
} catch (err) {
try {
Expand Down Expand Up @@ -714,7 +716,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var setOptionsReturned = false;
channel.setOptions(
{
params: params,
params: {
modes: 'publish',
},
},
function () {
/* Wait a tick so we don' depend on whether the update event runs the
Expand Down Expand Up @@ -743,7 +747,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var setOptionsReturned = false;
channel.setOptions(
{
modes: modes,
modes: ['subscribe'],
},
function () {
Ably.Realtime.Platform.Config.nextTick(function () {
Expand Down

0 comments on commit f38c5a7

Please sign in to comment.