This repository was archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSockerProvider.tsx
96 lines (86 loc) · 2.64 KB
/
SockerProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as React from "react";
import usePartySocket from "partysocket/react";
import { ReactNode, useCallback, useEffect, useState } from "react";
type SocketContextType<T> = {
/**
* Ideally use the useSocketMessage hook instead to narrow down the data you are looking for
*/
messages: Record<keyof T, any>;
setMessages: React.Dispatch<React.SetStateAction<Record<string, string>>>;
sendJson: (obj: Partial<{ [key in keyof T]: T[key] }>) => void;
};
const SocketContext = React.createContext<
SocketContextType<unknown> | undefined
>(undefined);
/**
* The provider that should wrap everything PartyKit
* @param children All Child providers need to be under this Provider
* @param room The PartyKit room
* @constructor
*/
export function SocketProvider({
children,
room,
}: {
children: ReactNode;
room: string;
}) {
const [messages, setMessages] = useState<Record<string, string>>({});
const socket = usePartySocket({
host: "localhost:1999",
room,
onClose() {},
// onOpen(event) {
// },
onMessage(event) {
setMessages(JSON.parse(event.data));
},
});
const sendJson = useCallback(
(msg: Object) => {
socket.send(JSON.stringify(msg));
},
[socket],
);
const value = {
messages,
setMessages,
sendJson,
};
return (
<SocketContext.Provider value={value}>{children}</SocketContext.Provider>
);
}
/**
* Gives the ability to access the SocketProvider return values like `sendJson` and `messages`
*
* This should only be used by child providers and shouldn't be used in components
*/
export function useSocket<T>() {
const context = React.useContext(SocketContext);
if (context === undefined) {
throw new Error("useSocket must be used within a SocketProvider");
}
return context as SocketContextType<T>;
}
/**
* Use this in your React components as a sort of subscriber to messages
* @param callbackFn The callbackFn that is called when the matching message is found
* @param message When a message with this key is found, the callbackFn is called
*/
export function useSocketMessage<T>(
callbackFn: (args: T) => void,
message: string,
) {
const { messages, setMessages } = useSocket<T>();
// @ts-ignore
const possibleMessage = messages[message];
useEffect(() => {
if (possibleMessage) {
// clear the message so providers don't have to handle things, they will only fire once
// this is done before the `callbackFn`. I feel if it fails it may not continue therefore if it fails, that message is discarded
setMessages({});
callbackFn(possibleMessage);
}
}, [callbackFn, possibleMessage, setMessages]);
}