Skip to content
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
7 changes: 1 addition & 6 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ function App() {
setConvId(convIds[nextConv]);
}, [convId]);

const createUser = useCallback(() => {
console.log("createUser");
return new Talk.User(me);
}, [me]);

const createConv = useCallback(
(session: Talk.Session) => {
console.log("createConv");
Expand Down Expand Up @@ -141,7 +136,7 @@ function App() {
<>
<Session
appId={import.meta.env.VITE_APP_ID}
syncUser={createUser}
syncUser={() => new Talk.User(me)}
onBrowserPermissionNeeded={onPerm}
onUnreadsChange={onUnreads}
sessionRef={sessionRef}
Expand Down
37 changes: 29 additions & 8 deletions lib/Session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ export function Session(props: SessionProps) {
Talk.ready.then(() => markReady(true));
}, []);

useEffect(() => {
if (ready) {
const me =
typeof syncUser === "function"
? syncUser()
: syncUser ?? new Talk.User(userId);
const me = ready
? typeof syncUser === "function"
? syncUser()
: syncUser ?? new Talk.User(userId)
: null;

const session = new Talk.Session({ appId, me, token, tokenFetcher, signature });
useEffect(() => {
if (me) {
const session = new Talk.Session({
appId,
me,
token,
tokenFetcher,
signature,
});
setSession(session);
if (sessionRef) {
sessionRef.current = session;
Expand All @@ -65,7 +72,21 @@ export function Session(props: SessionProps) {
}
};
}
}, [ready, signature, appId, userId, syncUser, sessionRef]);
// We intentionally add `me?.id` to the dependency array here instead of
// just `me`, because `me` is an object so a shallow comparison will always
// return `false`.
//
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
ready,
signature,
appId,
userId,
me?.id,
token,
tokenFetcher,
sessionRef,
]);

useMethod(
session,
Expand Down