Skip to content

Commit

Permalink
fix: wrap usePresence channel options in ref
Browse files Browse the repository at this point in the history
  • Loading branch information
owenpearson committed Oct 4, 2023
1 parent 89589f1 commit 4879d50
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/platform/react-hooks/src/hooks/usePresence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Types } from '../../../../../ably.js';
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { ChannelParameters } from '../AblyReactHooks.js';
import { useAbly } from './useAbly.js';
import { useStateErrors } from './useStateErrors.js';
Expand Down Expand Up @@ -28,11 +28,21 @@ export function usePresence<T = any>(

const subscribeOnly = typeof channelNameOrNameAndOptions === 'string' ? false : params.subscribeOnly;

const channel = ably.channels.get(params.channelName, params.options);
const channelOptions = params.options;
const channelOptionsRef = useRef(channelOptions);

const channel = useMemo(() => ably.channels.get(params.channelName, channelOptionsRef.current), [ably, params.channelName]);
const skip = params.skip;

const { connectionError, channelError } = useStateErrors(params);

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

const [presenceData, updatePresenceData] = useState<Array<PresenceMessage<T>>>([]);

const updatePresence = async (message?: Types.PresenceMessage) => {
Expand Down

0 comments on commit 4879d50

Please sign in to comment.