-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: CallContext 일부 상태값 제거 및 AudioContext 생성 #50
- Loading branch information
1 parent
331e2ac
commit 2554b10
Showing
2 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters