Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't compare agent channel param when determining whether channel options require reattach #1478

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ class RealtimeChannel extends Channel {
return false;
}
if (options?.params) {
if (!this.params || !Utils.shallowEquals(this.params, options.params)) {
// Don't check against the `agent` param - it isn't returned in the ATTACHED message
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
const requestedParams = Object.assign({}, options.params);
delete requestedParams.agent;
if (Object.keys(requestedParams).length !== 0 && !this.params) {
return true;
}

if (this.params && !Utils.shallowEquals(this.params, requestedParams)) {
return true;
}
}
Expand Down
31 changes: 25 additions & 6 deletions src/platform/react-hooks/src/hooks/useChannelStateListener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef } from 'react';
import { Types } from '../../../../../ably.js';
import { ChannelNameAndId } from '../AblyReactHooks.js';
import { ChannelNameAndId, ChannelNameAndOptions, channelOptionsWithAgent } from '../AblyReactHooks.js';
import { useAbly } from './useAbly.js';
import { useEventListener } from './useEventListener.js';

Expand All @@ -8,22 +9,40 @@ type ChannelStateListener = (stateChange: Types.ChannelStateChange) => any;
export function useChannelStateListener(channelName: string, listener?: ChannelStateListener);

export function useChannelStateListener(
options: ChannelNameAndId | string,
options: ChannelNameAndOptions | string,
state?: Types.ChannelState | Types.ChannelState[],
listener?: ChannelStateListener
);

export function useChannelStateListener(
channelNameOrNameAndId: ChannelNameAndId | string,
channelNameOrNameAndId: ChannelNameAndOptions | string,
stateOrListener?: Types.ChannelState | Types.ChannelState[] | ChannelStateListener,
listener?: (stateChange: Types.ChannelStateChange) => any
) {
const channelName =
typeof channelNameOrNameAndId === 'string' ? channelNameOrNameAndId : channelNameOrNameAndId.channelName;
const channelHookOptions =
typeof channelNameOrNameAndId === 'object' ? channelNameOrNameAndId : { channelName: channelNameOrNameAndId };
const id = (channelNameOrNameAndId as ChannelNameAndId)?.id;

const { channelName, options: channelOptions } = channelHookOptions;

const ably = useAbly(id);
const channel = ably.channels.get(channelName);
const channel = ably.channels.get(channelName, channelOptionsWithAgent(channelOptions));
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved

const channelOptionsRef = useRef(channelOptions);

useEffect(() => {
if (channelOptionsRef.current !== channelOptions && channelOptions) {
channel.setOptions(channelOptionsWithAgent(channelOptions));
}
channelOptionsRef.current = channelOptions;
}, [channel, channelOptions]);

useEffect(() => {
if (channelOptionsRef.current !== channelOptions && channelOptions) {
channel.setOptions(channelOptionsWithAgent(channelOptions));
}
channelOptionsRef.current = channelOptions;
}, [channel, channelOptions]);

const _listener = typeof listener === 'function' ? listener : (stateOrListener as ChannelStateListener);

Expand Down
Loading