Skip to content

Commit 99d268c

Browse files
committed
refactor: Rename useSetter into useMethod
1 parent 2e72d51 commit 99d268c

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

lib/Session.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Talk from "talkjs";
33
import { SessionContext } from "./SessionContext";
44
import { SessionEvents } from "./types";
55
import { EventListeners } from "./EventListeners";
6-
import { useSetter } from "./hooks";
6+
import { useMethod } from "./hooks";
77

88
type UserProps =
99
| { userId: string; syncUser?: undefined }
@@ -22,8 +22,14 @@ export function Session(props: SessionProps) {
2222
const [ready, markReady] = useState(false);
2323
const [session, setSession] = useState<Talk.Session>();
2424

25-
const { userId, syncUser, appId, signature, sessionRef, desktopNotificationEnabled } =
26-
props;
25+
const {
26+
userId,
27+
syncUser,
28+
appId,
29+
signature,
30+
sessionRef,
31+
desktopNotificationEnabled,
32+
} = props;
2733

2834
useEffect(() => {
2935
Talk.ready.then(() => markReady(true));
@@ -57,7 +63,7 @@ export function Session(props: SessionProps) {
5763
}
5864
}, [ready, appId, userId, syncUser, sessionRef]);
5965

60-
useSetter(
66+
useMethod(
6167
session,
6268
desktopNotificationEnabled,
6369
"setDesktopNotificationEnabled",

lib/hooks.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ export function usePreviousIfDeeplyEqual<T>(val: T) {
2424
}
2525

2626
/**
27-
* Calls method `setter` on `box` with `value`, iff `value` is set & deeply
27+
* Calls method `method` on `box` with `value`, iff `value` is set & deeply
2828
* different from before (and the box is alive)
2929
*/
30-
export function useSetter<
30+
export function useMethod<
3131
V,
3232
S extends string,
3333
T extends TalkObject & Record<S, (val: V) => any>,
34-
>(box: T | undefined, value: V | undefined, setter: S) {
34+
>(box: T | undefined, value: V | undefined, method: S) {
3535
value = usePreviousIfDeeplyEqual(value);
3636
useEffect(() => {
3737
if (value !== undefined && box?.isAlive) {
38-
box[setter](value);
38+
box[method](value);
3939
}
40-
}, [setter, box, value]);
40+
}, [method, box, value]);
4141
}
4242

4343
/**
44-
* Calls method `setter` on `box` with the arguments `args`, iff `args` is set &
45-
* deeply different from before (and the box is alive)
44+
* Like {@link useMethod}, except `args` is an array that gets spread into
45+
* the method call
4646
*/
47-
export function useSpreadSetter<
47+
export function useSpreadMethod<
4848
V extends any[],
4949
S extends string,
5050
T extends TalkObject & Record<S, (...args: V) => any>,
@@ -77,10 +77,10 @@ export function useConversation<T extends Talk.UIBox>(
7777
}, [session, syncConversation, conversationId]);
7878

7979
const args = (
80-
conversation !== undefined ? [conversation, { asGuest }] : []
80+
conversation !== undefined ? [conversation, { asGuest }] : undefined
8181
) as any;
8282

83-
useSpreadSetter(box, args, "select");
83+
useSpreadMethod(box, args, "select");
8484
}
8585

8686
// subset of Session to help TypeScript pick the right overloads

lib/ui/Chatbox.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CSSProperties, ReactNode } from "react";
22
import type Talk from "talkjs";
33
import { useSession } from "../SessionContext";
44
import { getKeyForObject, splitObjectByPrefix } from "../util";
5-
import { useSetter, useConversation, useUIBox } from "../hooks";
5+
import { useMethod, useConversation, useUIBox } from "../hooks";
66
import { FirstParameter, UIBoxProps } from "../types";
77
import { MountedBox } from "../MountedBox";
88

@@ -48,9 +48,9 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
4848
options;
4949

5050
const box = useUIBox(session, "createChatbox", simpleOptions, chatboxRef);
51-
useSetter(box, messageFilter, "setMessageFilter");
52-
useSetter(box, presence, "setPresence");
53-
useSetter(box, highlightedWords, "setHighlightedWords");
51+
useMethod(box, messageFilter, "setMessageFilter");
52+
useMethod(box, presence, "setPresence");
53+
useMethod(box, highlightedWords, "setHighlightedWords");
5454
useConversation(session, box, syncConversation, conversationId, asGuest);
5555

5656
return (

lib/ui/Inbox.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CSSProperties, ReactNode } from "react";
22
import type Talk from "talkjs";
33
import { useSession } from "../SessionContext";
44
import { getKeyForObject, splitObjectByPrefix } from "../util";
5-
import { useSetter, useConversation, useUIBox } from "../hooks";
5+
import { useMethod, useConversation, useUIBox } from "../hooks";
66
import { FirstParameter, UIBoxProps } from "../types";
77
import { MountedBox } from "../MountedBox";
88

@@ -53,10 +53,10 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
5353
} = options;
5454

5555
const box = useUIBox(session, "createInbox", simpleOptions, inboxRef);
56-
useSetter(box, messageFilter, "setMessageFilter");
57-
useSetter(box, feedFilter, "setFeedFilter");
58-
useSetter(box, presence, "setPresence");
59-
useSetter(box, highlightedWords, "setHighlightedWords");
56+
useMethod(box, messageFilter, "setMessageFilter");
57+
useMethod(box, feedFilter, "setFeedFilter");
58+
useMethod(box, presence, "setPresence");
59+
useMethod(box, highlightedWords, "setHighlightedWords");
6060
useConversation(session, box, syncConversation, conversationId, asGuest);
6161

6262
return (

lib/ui/Popup.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Talk from "talkjs";
22
import { useSession } from "../SessionContext";
33
import { getKeyForObject, splitObjectByPrefix } from "../util";
4-
import { useSetter, useConversation, useUIBox, useMountBox } from "../hooks";
4+
import { useMethod, useConversation, useUIBox, useMountBox } from "../hooks";
55
import { EventListeners } from "../EventListeners";
66
import { UIBoxProps } from "../types";
77

@@ -36,9 +36,9 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
3636
options;
3737

3838
const box = useUIBox(session, "createPopup", simpleOptions, popupRef);
39-
useSetter(box, messageFilter, "setMessageFilter");
40-
useSetter(box, presence, "setPresence");
41-
useSetter(box, highlightedWords, "setHighlightedWords");
39+
useMethod(box, messageFilter, "setMessageFilter");
40+
useMethod(box, presence, "setPresence");
41+
useMethod(box, highlightedWords, "setHighlightedWords");
4242
useConversation(session, box, syncConversation, conversationId, asGuest);
4343
useMountBox(box, undefined);
4444

0 commit comments

Comments
 (0)