-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useScreenCapture): update the hook's states
- Loading branch information
Showing
3 changed files
with
32 additions
and
34 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
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
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 |
---|---|---|
@@ -1,39 +1,34 @@ | ||
import { useCallback, useRef, useState } from 'react'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
/** | ||
* @see {@link https://malkiii.github.io/react-pre-hooks/docs/hooks/useScreenCapture | useScreenCapture} hook. | ||
*/ | ||
export const useScreenCapture = (options: DisplayMediaStreamOptions = {}) => { | ||
const streamRef = useRef<MediaStream>(); | ||
const [isEnabled, setIsEnabled] = useState<boolean>(false); | ||
const [stream, setStream] = useState<MediaStream | null>(null); | ||
const [isPending, setIsPending] = useState(false); | ||
|
||
const startStreaming = useCallback(async () => { | ||
streamRef.current?.getTracks().forEach(t => t.stop()); | ||
setIsPending(true); | ||
stream?.getTracks().forEach(t => t.stop()); | ||
|
||
try { | ||
streamRef.current = await navigator.mediaDevices.getDisplayMedia(options); | ||
const videoTrack = streamRef.current.getVideoTracks().at(0); | ||
if (videoTrack) videoTrack.onended = () => setIsEnabled(false); | ||
const newStream = await navigator.mediaDevices.getDisplayMedia(options); | ||
const videoTrack = newStream.getVideoTracks()[0]; | ||
if (videoTrack) videoTrack.onended = () => setStream(null); | ||
|
||
setIsEnabled(true); | ||
} catch (error) { | ||
setIsEnabled(false); | ||
throw error; | ||
setStream(newStream); | ||
setIsPending(false); | ||
|
||
return newStream; | ||
} finally { | ||
setIsPending(false); | ||
} | ||
}, []); | ||
}, [stream]); | ||
|
||
const stopStreaming = useCallback(() => { | ||
streamRef.current?.getTracks().forEach(t => t.stop()); | ||
setIsEnabled(false); | ||
}, []); | ||
|
||
const toggle = useCallback( | ||
async (force?: boolean) => { | ||
const shouldStart = force ?? !isEnabled; | ||
shouldStart ? await startStreaming() : stopStreaming(); | ||
}, | ||
[isEnabled] | ||
); | ||
stream?.getTracks().forEach(t => t.stop()); | ||
setStream(null); | ||
}, [stream]); | ||
|
||
return { streamRef, start: startStreaming, stop: stopStreaming, toggle, isEnabled }; | ||
return { stream, start: startStreaming, stop: stopStreaming, isPending }; | ||
}; |