Skip to content

Commit

Permalink
chore: ロギングを整備し終端理由を表示
Browse files Browse the repository at this point in the history
  • Loading branch information
rokoucha committed Oct 4, 2021
1 parent 1389a6f commit 680c6d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
21 changes: 12 additions & 9 deletions src/components/futabaComemntProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { SetterOrUpdater } from 'recoil'
import type { Settings } from '../types/atom'
import type { ZenzaCommentChat } from '../types/miraktest-zenza'

const loggingName = 'Futaba Comment Provider' as const

export type FutabaCommentProviderProps = {
program: Program | null
service: Service | null
Expand Down Expand Up @@ -37,30 +39,30 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
try {
futabaRef.current = new FutabaClient({ baseUrl: settings.baseUrl })
} catch (error) {
console.error('Futaba Comment Provider', error)
console.error(loggingName, error)
}
}, [settings])

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

if (settings.baseUrl === '') {
console.warn('板が設定されていません')
console.warn(loggingName, '板が設定されていません')
return
}

if (settings.keyword === '') {
console.warn('検索条件が設定されていません')
console.warn(loggingName, '検索条件が設定されていません')
return
}

if (!setZenzaComment && !setDplayerComment) {
console.warn('コメントレンダラーがありません')
console.warn(loggingName, 'コメントレンダラーがありません')
return
}

if (!service) {
console.warn('サービスが取得できていません')
console.warn(loggingName, 'サービスが取得できていません')
return
}

Expand All @@ -76,7 +78,7 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
try {
threads = await futaba.threads()
} catch (e) {
console.error('Futaba Comment Provider', e)
console.error(loggingName, e)
return
}

Expand All @@ -87,7 +89,7 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
.sort((a, b) => b.res.length - a.res.length)
.slice(0, settings.maxStreams)
.map((t, i) => {
console.info('Futaba Comment Provider', 'stream', i, t)
console.info(loggingName, 'stream', i, t)
return useReturnAsync(
futaba.stream({
interval: settings.interval * 1000,
Expand All @@ -96,18 +98,19 @@ export const FutabaCommentProvider: React.VFC<FutabaCommentProviderProps> = ({
)
})

console.info('Futaba Comment Provider', streams.length, 'streams')
console.info(loggingName, streams.length, 'streams')

streamsRef.current = streams
})()
}, [program, service, settings, restart])

return (
<>
{streamsRef.current.map(([stream], index) => (
{streamsRef.current.map(([stream, result], index) => (
<FutabaCommentStreamer
id={index}
key={index}
result={result}
setDplayerComment={setDplayerComment}
setRestart={setRestart}
setZenzaComment={setZenzaComment}
Expand Down
17 changes: 11 additions & 6 deletions src/components/futabaCommentStreamer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import type { Response } from '../lib/futaba/schema'
import type { DPlayerCommentPayload } from '../types/miraktest-dplayer'
import type { ZenzaCommentChat } from '../types/miraktest-zenza'

const loggingName = 'Futaba Comment Streamer' as const

type FutabaCommentStreamerProps = {
id: number
result: () => string | undefined
setDplayerComment: SetterOrUpdater<DPlayerCommentPayload> | null
setRestart: React.Dispatch<React.SetStateAction<boolean>>
setZenzaComment: SetterOrUpdater<ZenzaCommentChat> | null
Expand All @@ -14,17 +17,18 @@ type FutabaCommentStreamerProps = {

export const FutabaCommentStreamer: React.VFC<FutabaCommentStreamerProps> = ({
id,
result,
setDplayerComment,
setRestart,
setZenzaComment,
stream,
}) => {
useEffect(() => {
console.info('Futaba Comment Streamer', id, 'open')
console.info(loggingName, id, 'open')
;(async () => {
try {
for await (const response of stream) {
console.info('Futaba Comment Streamer', id, response.comment)
console.info(loggingName, id, response.comment)

if (setZenzaComment) {
setZenzaComment({
Expand Down Expand Up @@ -53,17 +57,18 @@ export const FutabaCommentStreamer: React.VFC<FutabaCommentStreamerProps> = ({
}
}
} catch (e) {
console.error('Futaba Comment Streamer', id, e)
} finally {
console.info('Futaba Comment Streamer', id, 'closed by remote')
console.error(loggingName, id, e)

stream.return()
} finally {
console.info(loggingName, id, 'closed by remote', result())

setRestart(true)
}
})()

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

setRestart(true)
Expand Down

0 comments on commit 680c6d4

Please sign in to comment.