mirror of
https://github.com/hexastack/hexabot
synced 2025-05-31 10:57:06 +00:00
fix: refactor broadcastChannel payload types
This commit is contained in:
parent
0a4da02591
commit
24e09be6ed
@ -20,15 +20,11 @@ export enum EBCEvent {
|
||||
LOGOUT = "logout",
|
||||
}
|
||||
|
||||
type BroadcastChannelPayload = {
|
||||
type BroadcastChannelMessage = {
|
||||
event: `${EBCEvent}`;
|
||||
data?: string | number | boolean | Record<string, unknown> | undefined | null;
|
||||
};
|
||||
|
||||
type BroadcastChannelData = {
|
||||
payload: BroadcastChannelPayload;
|
||||
};
|
||||
|
||||
interface IBroadcastChannelProps {
|
||||
channelName: string;
|
||||
children: ReactNode;
|
||||
@ -37,9 +33,9 @@ interface IBroadcastChannelProps {
|
||||
interface IBroadcastChannelContext {
|
||||
subscribe: (
|
||||
event: `${EBCEvent}`,
|
||||
callback: (message: BroadcastChannelData) => void,
|
||||
callback: (message: BroadcastChannelMessage) => void,
|
||||
) => void;
|
||||
postMessage: (payload: BroadcastChannelPayload) => void;
|
||||
postMessage: (message: BroadcastChannelMessage) => void;
|
||||
}
|
||||
|
||||
export const BroadcastChannelContext = createContext<
|
||||
@ -61,12 +57,8 @@ export const BroadcastChannelProvider: FC<IBroadcastChannelProps> = ({
|
||||
>({});
|
||||
|
||||
useEffect(() => {
|
||||
const handleMessage = ({ data }: MessageEvent<BroadcastChannelData>) => {
|
||||
const { payload } = data;
|
||||
|
||||
subscribersRef.current[payload.event].forEach((callback) =>
|
||||
callback(data),
|
||||
);
|
||||
const handleMessage = ({ data }: MessageEvent<BroadcastChannelMessage>) => {
|
||||
subscribersRef.current[data.event].forEach((callback) => callback(data));
|
||||
};
|
||||
|
||||
channelRef.current.addEventListener("message", handleMessage);
|
||||
@ -92,10 +84,8 @@ export const BroadcastChannelProvider: FC<IBroadcastChannelProps> = ({
|
||||
}
|
||||
};
|
||||
};
|
||||
const postMessage: IBroadcastChannelContext["postMessage"] = (payload) => {
|
||||
channelRef.current.postMessage({
|
||||
payload,
|
||||
});
|
||||
const postMessage: IBroadcastChannelContext["postMessage"] = (message) => {
|
||||
channelRef.current.postMessage(message);
|
||||
};
|
||||
|
||||
return (
|
||||
|
@ -19,15 +19,11 @@ export enum EBCEvent {
|
||||
LOGOUT = "logout",
|
||||
}
|
||||
|
||||
type BroadcastChannelPayload = {
|
||||
type BroadcastChannelMessage = {
|
||||
event: `${EBCEvent}`;
|
||||
data?: string | number | boolean | Record<string, unknown> | undefined | null;
|
||||
};
|
||||
|
||||
type BroadcastChannelData = {
|
||||
payload: BroadcastChannelPayload;
|
||||
};
|
||||
|
||||
interface IBroadcastChannelProps {
|
||||
channelName: string;
|
||||
children: ReactNode;
|
||||
@ -36,9 +32,9 @@ interface IBroadcastChannelProps {
|
||||
interface IBroadcastChannelContext {
|
||||
subscribe: (
|
||||
event: `${EBCEvent}`,
|
||||
callback: (message: BroadcastChannelData) => void,
|
||||
callback: (message: BroadcastChannelMessage) => void,
|
||||
) => void;
|
||||
postMessage: (payload: BroadcastChannelPayload) => void;
|
||||
postMessage: (message: BroadcastChannelMessage) => void;
|
||||
}
|
||||
|
||||
export const BroadcastChannelContext = createContext<
|
||||
@ -60,12 +56,8 @@ export const BroadcastChannelProvider: FC<IBroadcastChannelProps> = ({
|
||||
>({});
|
||||
|
||||
useEffect(() => {
|
||||
const handleMessage = ({ data }: MessageEvent<BroadcastChannelData>) => {
|
||||
const { payload } = data;
|
||||
|
||||
subscribersRef.current[payload.event].forEach((callback) =>
|
||||
callback(data),
|
||||
);
|
||||
const handleMessage = ({ data }: MessageEvent<BroadcastChannelMessage>) => {
|
||||
subscribersRef.current[data.event].forEach((callback) => callback(data));
|
||||
};
|
||||
|
||||
channelRef.current.addEventListener("message", handleMessage);
|
||||
@ -92,10 +84,8 @@ export const BroadcastChannelProvider: FC<IBroadcastChannelProps> = ({
|
||||
}
|
||||
};
|
||||
};
|
||||
const postMessage: IBroadcastChannelContext["postMessage"] = (payload) => {
|
||||
channelRef.current.postMessage({
|
||||
payload,
|
||||
});
|
||||
const postMessage: IBroadcastChannelContext["postMessage"] = (message) => {
|
||||
channelRef.current.postMessage(message);
|
||||
};
|
||||
|
||||
return (
|
||||
|
Loading…
Reference in New Issue
Block a user