Skip to content

Commit

Permalink
fix(react): ensure channel options are passed to channel state listen…
Browse files Browse the repository at this point in the history
…er hook
  • Loading branch information
Owen Pearson authored and Owen Pearson committed Nov 1, 2023
1 parent 5b8fdb3 commit ad264d9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/platform/react-hooks/src/hooks/useChannelStateListener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 +8,24 @@ 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));

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

Expand Down

0 comments on commit ad264d9

Please sign in to comment.