Consolidate control page connection logic

This commit is contained in:
Max Goodhart
2020-07-05 20:32:59 -07:00
parent 067c9c238a
commit 4d415c9197

View File

@@ -58,7 +58,7 @@ function useYDoc(keys) {
return [docValue, doc, setDoc] return [docValue, doc, setDoc]
} }
function App({ wsEndpoint }) { function useStreamwallConnection(wsEndpoint) {
const wsRef = useRef() const wsRef = useRef()
const [isConnected, setIsConnected] = useState(false) const [isConnected, setIsConnected] = useState(false)
const [sharedState, stateDoc, setStateDoc] = useYDoc(['views']) const [sharedState, stateDoc, setStateDoc] = useYDoc(['views'])
@@ -68,8 +68,6 @@ function App({ wsEndpoint }) {
const [stateIdxMap, setStateIdxMap] = useState(new Map()) const [stateIdxMap, setStateIdxMap] = useState(new Map())
const [delayState, setDelayState] = useState() const [delayState, setDelayState] = useState()
const { gridCount } = config
useEffect(() => { useEffect(() => {
const ws = new ReconnectingWebSocket(wsEndpoint, [], { const ws = new ReconnectingWebSocket(wsEndpoint, [], {
maxReconnectionDelay: 5000, maxReconnectionDelay: 5000,
@@ -130,6 +128,10 @@ function App({ wsEndpoint }) {
wsRef.current = ws wsRef.current = ws
}, []) }, [])
const send = useCallback((...args) => {
wsRef.current.send(...args)
}, [])
useEffect(() => { useEffect(() => {
function sendUpdate(update) { function sendUpdate(update) {
wsRef.current.send(update) wsRef.current.send(update)
@@ -148,6 +150,34 @@ function App({ wsEndpoint }) {
} }
}, [stateDoc]) }, [stateDoc])
return {
isConnected,
send,
sharedState,
stateDoc,
config,
streams,
customStreams,
stateIdxMap,
delayState,
}
}
function App({ wsEndpoint }) {
const {
isConnected,
send,
sharedState,
stateDoc,
config,
streams,
customStreams,
stateIdxMap,
delayState,
} = useStreamwallConnection(wsEndpoint)
const { gridCount } = config
const handleSetView = useCallback( const handleSetView = useCallback(
(idx, streamId) => { (idx, streamId) => {
const stream = streams.find((d) => d._id === streamId) const stream = streams.find((d) => d._id === streamId)
@@ -160,7 +190,7 @@ function App({ wsEndpoint }) {
) )
const handleSetListening = useCallback((idx, listening) => { const handleSetListening = useCallback((idx, listening) => {
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'set-listening-view', type: 'set-listening-view',
viewIdx: listening ? idx : null, viewIdx: listening ? idx : null,
@@ -169,7 +199,7 @@ function App({ wsEndpoint }) {
}, []) }, [])
const handleSetBlurred = useCallback((idx, blurred) => { const handleSetBlurred = useCallback((idx, blurred) => {
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'set-view-blurred', type: 'set-view-blurred',
viewIdx: idx, viewIdx: idx,
@@ -179,7 +209,7 @@ function App({ wsEndpoint }) {
}, []) }, [])
const handleReloadView = useCallback((idx) => { const handleReloadView = useCallback((idx) => {
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'reload-view', type: 'reload-view',
viewIdx: idx, viewIdx: idx,
@@ -193,7 +223,7 @@ function App({ wsEndpoint }) {
if (!stream) { if (!stream) {
return return
} }
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'browse', type: 'browse',
url: stream.link, url: stream.link,
@@ -204,7 +234,7 @@ function App({ wsEndpoint }) {
) )
const handleDevTools = useCallback((idx) => { const handleDevTools = useCallback((idx) => {
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'dev-tools', type: 'dev-tools',
viewIdx: idx, viewIdx: idx,
@@ -229,7 +259,7 @@ function App({ wsEndpoint }) {
let newCustomStreams = [...customStreams] let newCustomStreams = [...customStreams]
newCustomStreams[idx] = customStream newCustomStreams[idx] = customStream
newCustomStreams = newCustomStreams.filter((s) => s.label || s.link) newCustomStreams = newCustomStreams.filter((s) => s.label || s.link)
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'set-custom-streams', type: 'set-custom-streams',
streams: newCustomStreams, streams: newCustomStreams,
@@ -238,7 +268,7 @@ function App({ wsEndpoint }) {
}) })
const setStreamCensored = useCallback((isCensored) => { const setStreamCensored = useCallback((isCensored) => {
wsRef.current.send( send(
JSON.stringify({ JSON.stringify({
type: 'set-stream-censored', type: 'set-stream-censored',
isCensored, isCensored,