From 2554b1019c86fc2174806d75e23a0bd3ebd8478d Mon Sep 17 00:00:00 2001 From: luckylooky2 Date: Sat, 14 Sep 2024 08:46:55 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20CallContext=20=EC=9D=BC=EB=B6=80=20?= =?UTF-8?q?=EC=83=81=ED=83=9C=EA=B0=92=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20?= =?UTF-8?q?AudioContext=20=EC=83=9D=EC=84=B1=20#50?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contexts/AudioProvider.tsx | 67 ++++++++++++++++++++++++++++++++++ src/contexts/CallProvider.tsx | 11 ------ 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/contexts/AudioProvider.tsx diff --git a/src/contexts/AudioProvider.tsx b/src/contexts/AudioProvider.tsx new file mode 100644 index 0000000..109e80c --- /dev/null +++ b/src/contexts/AudioProvider.tsx @@ -0,0 +1,67 @@ +import { AudioInfo } from "@typings/front"; +import React, { createContext, FC, useMemo, useReducer } from "react"; + +interface Props { + children: React.ReactNode; +} + +export const AudioActionType = { + SET_STREAM: "SET_STREAM", + SET_AUDIOLIST: "SET_AUDIOLIST", + DEL_ALL: "DEL_ALL", +}; + +const initialAudioState: AudioInfo = { + stream: null, + deviceId: null, + audioList: [], +}; + +export const AudioContext = createContext<{ + audio: AudioInfo; + dispatch: React.Dispatch<{ + type: string; + payload?: any; + }>; +}>({ + audio: initialAudioState, + dispatch: () => {}, +}); + +const callReducer = ( + state: AudioInfo, + action: { type: string; payload?: any } +): AudioInfo => { + switch (action.type) { + case AudioActionType.SET_STREAM: + return { + ...state, + stream: action.payload.stream, + deviceId: action.payload.deviceId, + }; + case AudioActionType.SET_AUDIOLIST: + return { + ...state, + audioList: action.payload, + }; + case AudioActionType.DEL_ALL: + return { + stream: null, + deviceId: null, + audioList: [], + }; + default: + return state; + } +}; + +const AudioProvider: FC = ({ children }) => { + const [audio, dispatch] = useReducer(callReducer, initialAudioState); + const value = useMemo(() => ({ audio, dispatch }), [audio]); + + return ( + {children} + ); +}; + +export default AudioProvider; diff --git a/src/contexts/CallProvider.tsx b/src/contexts/CallProvider.tsx index 4917631..5e33354 100644 --- a/src/contexts/CallProvider.tsx +++ b/src/contexts/CallProvider.tsx @@ -6,7 +6,6 @@ interface Props { } export const CallActionType = { - SET_STREAM: "SET_STREAM", SET_MATCHING: "SET_MATCHING", SET_ROOMTYPE: "SET_ROOMTYPE", SET_CURRNUM: "SET_CURRNUM", @@ -14,8 +13,6 @@ export const CallActionType = { }; const initialCallState: CallInfo = { - stream: null, - deviceId: null, roomName: null, roomType: null, opponent: null, @@ -38,12 +35,6 @@ const callReducer = ( action: { type: string; payload?: any } ): CallInfo => { switch (action.type) { - case CallActionType.SET_STREAM: - return { - ...state, - stream: action.payload.stream, - deviceId: action.payload.deviceId, - }; case CallActionType.SET_MATCHING: return { ...state, @@ -52,8 +43,6 @@ const callReducer = ( }; case CallActionType.DEL_ALL: return { - stream: null, - deviceId: null, roomName: null, roomType: null, opponent: null,