From f38c5a7718593d46931c6b0bd0584bcfbfb9aa9d Mon Sep 17 00:00:00 2001 From: Owen Pearson Date: Thu, 5 Oct 2023 12:33:38 +0100 Subject: [PATCH] fix: improve channel options reattachment logic 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). --- src/common/lib/client/realtimechannel.ts | 15 ++++++++++++++- test/realtime/channel.test.js | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/common/lib/client/realtimechannel.ts b/src/common/lib/client/realtimechannel.ts index ce6f50d687..615ab6a686 100644 --- a/src/common/lib/client/realtimechannel.ts +++ b/src/common/lib/client/realtimechannel.ts @@ -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 { diff --git a/test/realtime/channel.test.js b/test/realtime/channel.test.js index 252a9b1ef6..a079b64062 100644 --- a/test/realtime/channel.test.js +++ b/test/realtime/channel.test.js @@ -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 { @@ -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 @@ -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 () {