-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInternalCtx.js
73 lines (73 loc) · 2.56 KB
/
InternalCtx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { useEffect, useReducer, useRef, useState } from 'react';
import { Platform } from 'react-native';
import { useVideoCtx } from './ScreenContainer';
const InternalCtx = React.createContext(undefined);
export const useInternalCtx = () => {
const ctx = React.useContext(InternalCtx);
if (!ctx) {
throw new Error('Internal Context not found!');
}
return ctx;
};
export const InternalProvider = ({ initialPaused = false, initialMuted = false, children, }) => {
let videoInstance = useRef();
const { setSeeking } = useVideoCtx();
const [paused, setPaused] = useState(Platform.OS === "ios" ? true : initialPaused);
const [muted, setMuted] = useState(initialMuted);
/* useEffect(() => {
if (Platform.OS === "ios") {
setTimeout(() => {
setPaused(false);
}, 100)
}
},[]) */
const seekerRef = useRef({}).current;
const [{ duration, bufferTime, ended }, setState] = useReducer((s, a) => ({ ...s, ...a }), {
duration: 0,
bufferTime: 0,
ended: false,
});
const forceUpdate = useState({})[1];
const mutableState = useRef({
prevPaused: initialPaused,
currentTime: 0,
}).current;
const value = {
paused,
setPaused,
muted,
setMuted,
videoInstance,
seekerRef,
duration,
bufferTime,
ended,
setState,
forceUpdate,
mutableState,
onSeek: (data) => {
if (data.eventName === 'GRANT') {
mutableState.prevPaused = paused;
setPaused(true);
setSeeking(true);
seekerRef.progressStopped = true;
}
if (data.eventName === 'MOVE') {
mutableState.currentTime = duration * data.ratio;
forceUpdate({}); // to trigger timer update when dragging thumb
}
if (data.eventName === 'RELEASE') {
videoInstance.current?.seek(duration * data.ratio);
setPaused(mutableState.prevPaused);
setSeeking(false);
setTimeout(() => {
// use setTimeout to prevent seeker thumb jumping in case that
// RELEASE is called instantly after GRANT
// need to use mutable state to check, look at <Video onProgress />
seekerRef.progressStopped = false;
}, 50);
}
},
};
return <InternalCtx.Provider value={value}>{children}</InternalCtx.Provider>;
};