Created
January 19, 2023 17:39
-
-
Save h-jennings/1a9e2d1d366da2f85e833ebe8f3411f2 to your computer and use it in GitHub Desktop.
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
const AudioPrimitiveContext = React.createContext<InterpreterFrom< | |
typeof audioMachine | |
> | null>(null); | |
export const AudioPrimitiveProvider = ({ | |
children, | |
}: { | |
children?: React.ReactNode; | |
}) => { | |
const [machine] = React.useState(() => { | |
return audioMachine; | |
}); | |
const audioService = useInterpret(machine); | |
return ( | |
<AudioPrimitiveContext.Provider value={audioService}> | |
{children} | |
</AudioPrimitiveContext.Provider> | |
); | |
}; | |
export const useAudioService = () => { | |
const context = React.useContext(AudioPrimitiveContext); | |
if (!context) { | |
throw new Error('useAudioState must be used within a AudioProvider'); | |
} | |
return context; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment