Skip to content

Commit

Permalink
Feat: CallContext 일부 상태값 제거 및 AudioContext 생성 #50
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Sep 13, 2024
1 parent 331e2ac commit 2554b10
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
67 changes: 67 additions & 0 deletions src/contexts/AudioProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<Props> = ({ children }) => {
const [audio, dispatch] = useReducer(callReducer, initialAudioState);
const value = useMemo(() => ({ audio, dispatch }), [audio]);

return (
<AudioContext.Provider value={value}>{children}</AudioContext.Provider>
);
};

export default AudioProvider;
11 changes: 0 additions & 11 deletions src/contexts/CallProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ interface Props {
}

export const CallActionType = {
SET_STREAM: "SET_STREAM",
SET_MATCHING: "SET_MATCHING",
SET_ROOMTYPE: "SET_ROOMTYPE",
SET_CURRNUM: "SET_CURRNUM",
DEL_ALL: "DEL_ALL",
};

const initialCallState: CallInfo = {
stream: null,
deviceId: null,
roomName: null,
roomType: null,
opponent: null,
Expand All @@ -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,
Expand All @@ -52,8 +43,6 @@ const callReducer = (
};
case CallActionType.DEL_ALL:
return {
stream: null,
deviceId: null,
roomName: null,
roomType: null,
opponent: null,
Expand Down

0 comments on commit 2554b10

Please sign in to comment.