Skip to content

Commit

Permalink
Add memoization in db.rooms hooks (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka authored Jan 16, 2025
1 parent ff2fe2f commit a264c5d
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions client/packages/react/src/InstantReactRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,19 @@ export function usePresence<
return unsub;
}, [room.id, opts.user, opts.peers?.join(), opts.keys?.join()]);

return {
...state,
publishPresence: (data) => {
const publishPresence = useCallback(
(data) => {
room._core._reactor.publishPresence(room.type, room.id, data);
},
};
[room.type, room.id],
);
const ret = useMemo(() => {
return {
...state,
publishPresence,
};
}, [state, publishPresence]);
return ret;
}

/**
Expand Down Expand Up @@ -233,7 +240,7 @@ export function useTypingIndicator<
): TypingIndicatorHandle<RoomSchema[RoomType]["presence"]> {
const timeout = useTimeout();

const onservedPresence = rooms.usePresence(room, {
const observedPresence = rooms.usePresence(room, {
keys: [inputName],
});

Expand All @@ -248,40 +255,47 @@ export function useTypingIndicator<
: Object.values(presenceSnapshot?.peers ?? {}).filter(
(p) => p[inputName] === true,
);
}, [opts?.writeOnly, onservedPresence]);

const setActive = (isActive: boolean) => {
room._core._reactor.publishPresence(room.type, room.id, {
[inputName]: isActive,
} as unknown as Partial<RoomSchema[RoomType]>);
}, [opts?.writeOnly, observedPresence]);

if (!isActive) return;
const setActive = useCallback(
(isActive: boolean) => {
room._core._reactor.publishPresence(room.type, room.id, {
[inputName]: isActive,
} as unknown as Partial<RoomSchema[RoomType]>);

if (opts?.timeout === null || opts?.timeout === 0) return;
if (!isActive) return;

timeout.set(opts?.timeout ?? defaultActivityStopTimeout, () => {
room._core._reactor.publishPresence(room.type, room.id, {
[inputName]: null,
} as Partial<RoomSchema[RoomType]>);
});
};
if (opts?.timeout === null || opts?.timeout === 0) return;

return {
active,
setActive: (a: boolean) => {
setActive(a);
timeout.set(opts?.timeout ?? defaultActivityStopTimeout, () => {
room._core._reactor.publishPresence(room.type, room.id, {
[inputName]: null,
} as Partial<RoomSchema[RoomType]>);
});
},
inputProps: {
onKeyDown: (e: KeyboardEvent) => {
const isEnter = opts?.stopOnEnter && e.key === "Enter";
const isActive = !isEnter;
[room.type, room.id, inputName, opts?.timeout, timeout],
);
const onKeyDown = useCallback(
(e: KeyboardEvent) => {
const isEnter = opts?.stopOnEnter && e.key === "Enter";
const isActive = !isEnter;

setActive(isActive);
},
onBlur: () => {
setActive(false);
},
setActive(isActive);
},
[opts.stopOnEnter, setActive],
);
const onBlur = useCallback(() => {
setActive(false);
}, [setActive]);

const inputProps = useMemo(() => {
return { onKeyDown, onBlur };
}, [onKeyDown, onBlur]);

return {
active,
setActive,
inputProps,
};
}

Expand Down

0 comments on commit a264c5d

Please sign in to comment.