Skip to content

Commit

Permalink
fix: ストリームの再接続を調整
Browse files Browse the repository at this point in the history
めちゃくちゃ繋ぎなおそうとするのでアカンわ…
  • Loading branch information
rokoucha committed Oct 5, 2021
1 parent 0ad30c7 commit 1b43b32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
23 changes: 15 additions & 8 deletions src/components/futabaComemntProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FutabaClient, Response } from '../lib/futaba'
import { FutabaClient, Response, Threads } from '../lib/futaba'
import { FutabaCommentStreamer } from './futabaCommentStreamer'
import { useReturnAsync } from '../utils'
import React, { useEffect, useRef, useState } from 'react'
Expand Down Expand Up @@ -43,6 +43,10 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
}
}, [settings])

useEffect(() => {
streamsRef.current = []
}, [restart])

useEffect(() => {
if (!futabaRef.current) return

Expand Down Expand Up @@ -75,11 +79,14 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({

;(async () => {
let threads
try {
threads = await futaba.threads()
} catch (e) {
console.error(loggingName, e)
return
while (threads === undefined) {
console.log(loggingName, 'trying to fetch threads...')
try {
threads = await futaba.threads()
break
} catch (e) {
console.error(loggingName, e)
}
}

streams = threads.res
Expand All @@ -98,7 +105,7 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
)
})

console.info(loggingName, streams.length, 'streams')
console.info(loggingName, streams.length, 'streams ready')

streamsRef.current = streams
})()
Expand All @@ -112,7 +119,7 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
key={index}
result={result}
setDplayerComment={setDplayerComment}
setRestart={setRestart}
restart={() => setRestart((restart) => !restart)}
setZenzaComment={setZenzaComment}
stream={stream}
/>
Expand Down
10 changes: 5 additions & 5 deletions src/components/futabaCommentStreamer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ const loggingName = 'Futaba Comment Streamer' as const

type FutabaCommentStreamerProps = {
id: number
restart: () => any
result: () => string | undefined
setDplayerComment: SetterOrUpdater<DPlayerCommentPayload> | null
setRestart: React.Dispatch<React.SetStateAction<boolean>>
setZenzaComment: SetterOrUpdater<ZenzaCommentChat> | null
stream: AsyncGenerator<Response, void, undefined>
}

export const FutabaCommentStreamer: React.VFC<FutabaCommentStreamerProps> = ({
id,
restart,
result,
setDplayerComment,
setRestart,
setZenzaComment,
stream,
}) => {
Expand Down Expand Up @@ -63,15 +63,15 @@ export const FutabaCommentStreamer: React.VFC<FutabaCommentStreamerProps> = ({
} finally {
console.info(loggingName, id, 'closed by remote', result())

setRestart(true)
restart()
}
})()

return () => {
console.info(loggingName, id, 'closed by provider')
console.info(loggingName, id, 'closed by provider', result())
stream.return()

setRestart(true)
restart()
}
})

Expand Down

0 comments on commit 1b43b32

Please sign in to comment.