You've already forked bilibili-subtitle
fix消息通信
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
import useMessaging from "@/messaging/layer2/useMessaging";
|
||||
|
||||
export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages>
|
@@ -1,15 +1,17 @@
|
||||
import { setAuthor, setCtime, setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer'
|
||||
import { useMemo } from 'react'
|
||||
import { useAppDispatch } from './redux'
|
||||
import useMessagingService from '@/messaging/layer2/useMessagingService'
|
||||
import { AllAPPMessages, AllExtensionMessages, AllInjectMessages } from '@/message-typings'
|
||||
import { DEFAULT_USE_PORT } from '@/consts/const'
|
||||
import { useMessaging, useMessagingService } from '@kky002/kky-message'
|
||||
import { useMemoizedFn } from 'ahooks'
|
||||
|
||||
const useMessageService = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
|
||||
//methods
|
||||
const methods: {
|
||||
const methodsFunc: () => {
|
||||
[K in AllAPPMessages['method']]: (params: Extract<AllAPPMessages, { method: K }>['params'], context: MethodContext) => Promise<any>
|
||||
} = useMemo(() => ({
|
||||
} = useMemoizedFn(() => ({
|
||||
SET_INFOS: async (params, context: MethodContext) => {
|
||||
dispatch(setInfos(params.infos))
|
||||
dispatch(setCurInfo(undefined))
|
||||
@@ -24,9 +26,10 @@ const useMessageService = () => {
|
||||
dispatch(setAuthor(params.author))
|
||||
console.debug('video title: ', params.title)
|
||||
},
|
||||
}), [dispatch])
|
||||
}))
|
||||
|
||||
useMessagingService(methods)
|
||||
useMessagingService(DEFAULT_USE_PORT, methodsFunc)
|
||||
}
|
||||
|
||||
export default useMessageService
|
||||
export default useMessageService
|
||||
export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages>
|
@@ -1,16 +1,17 @@
|
||||
import {useAppDispatch, useAppSelector} from './redux'
|
||||
import React, {useCallback} from 'react'
|
||||
import {setNeedScroll, setReviewAction, setTempData} from '../redux/envReducer'
|
||||
import { useMessage } from './message'
|
||||
import { useMessage } from './useMessageService'
|
||||
const useSubtitle = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
const envData = useAppSelector(state => state.env.envData)
|
||||
const reviewed = useAppSelector(state => state.env.tempData.reviewed)
|
||||
const reviewAction = useAppSelector(state => state.env.reviewAction)
|
||||
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
|
||||
const {sendInject} = useMessage()
|
||||
const {sendInject} = useMessage(!!envData.sidePanel)
|
||||
|
||||
const move = useCallback((time: number, togglePause: boolean) => {
|
||||
sendInject('MOVE', {time, togglePause})
|
||||
sendInject(null, 'MOVE', {time, togglePause})
|
||||
|
||||
//review action
|
||||
if (reviewed === undefined && !reviewAction) {
|
||||
|
@@ -19,7 +19,7 @@ import {EventBusContext} from '../Router'
|
||||
import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_MIN, WORDS_RATE} from '../consts/const'
|
||||
import {useAsyncEffect, useInterval} from 'ahooks'
|
||||
import {getModelMaxTokens, getWholeText} from '../utils/bizUtil'
|
||||
import { useMessage } from './message'
|
||||
import { useMessage } from './useMessageService'
|
||||
|
||||
/**
|
||||
* Service是单例,类似后端的服务概念
|
||||
@@ -43,7 +43,7 @@ const useSubtitleService = () => {
|
||||
const autoTranslate = useAppSelector(state => state.env.autoTranslate)
|
||||
const reviewed = useAppSelector(state => state.env.tempData.reviewed)
|
||||
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
|
||||
const {sendInject} = useMessage()
|
||||
const {sendInject} = useMessage(!!envData.sidePanel)
|
||||
|
||||
//如果reviewActions达到15次,则设置reviewed为false
|
||||
useEffect(() => {
|
||||
@@ -78,7 +78,7 @@ const useSubtitleService = () => {
|
||||
// 获取
|
||||
useEffect(() => {
|
||||
if (curInfo && !curFetched) {
|
||||
sendInject('GET_SUBTITLE', {info: curInfo}).then(data => {
|
||||
sendInject(null, 'GET_SUBTITLE', {info: curInfo}).then(data => {
|
||||
data?.body?.forEach((item: TranscriptItem, idx: number) => {
|
||||
item.idx = idx
|
||||
})
|
||||
@@ -93,12 +93,12 @@ const useSubtitleService = () => {
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
// 初始获取列表
|
||||
sendInject('REFRESH_VIDEO_INFO', {force: true})
|
||||
sendInject(null, 'REFRESH_VIDEO_INFO', {force: true})
|
||||
}, [])
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
// 更新设置信息
|
||||
sendInject('GET_VIDEO_ELEMENT_INFO', {}).then(info => {
|
||||
sendInject(null, 'GET_VIDEO_ELEMENT_INFO', {}).then(info => {
|
||||
dispatch(setNoVideo(info.noVideo))
|
||||
if (envData.sidePanel) {
|
||||
// get screen height
|
||||
@@ -194,7 +194,7 @@ const useSubtitleService = () => {
|
||||
|
||||
// 每0.5秒更新当前视频时间
|
||||
useInterval(() => {
|
||||
sendInject('GET_VIDEO_STATUS', {}).then(status => {
|
||||
sendInject(null, 'GET_VIDEO_STATUS', {}).then(status => {
|
||||
dispatch(setCurrentTime(status.currentTime))
|
||||
})
|
||||
}, 500)
|
||||
@@ -202,15 +202,15 @@ const useSubtitleService = () => {
|
||||
// show translated text in the video
|
||||
useEffect(() => {
|
||||
if (hideOnDisableAutoTranslate && !autoTranslate) {
|
||||
sendInject('HIDE_TRANS', {})
|
||||
sendInject(null, 'HIDE_TRANS', {})
|
||||
return
|
||||
}
|
||||
|
||||
const transResult = curIdx?transResults[curIdx]:undefined
|
||||
if (transResult?.code === '200' && transResult.data) {
|
||||
sendInject('UPDATE_TRANS_RESULT', {result: transResult.data})
|
||||
sendInject(null, 'UPDATE_TRANS_RESULT', {result: transResult.data})
|
||||
} else {
|
||||
sendInject('HIDE_TRANS', {})
|
||||
sendInject(null, 'HIDE_TRANS', {})
|
||||
}
|
||||
}, [autoTranslate, curIdx, hideOnDisableAutoTranslate, transResults])
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ import toast from 'react-hot-toast'
|
||||
import {useMemoizedFn} from 'ahooks/es'
|
||||
import {extractJsonArray, extractJsonObject, getModel} from '../utils/bizUtil'
|
||||
import {formatTime} from '../utils/util'
|
||||
import { useMessage } from './message'
|
||||
import { useMessage } from './useMessageService'
|
||||
const useTranslate = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
const data = useAppSelector(state => state.env.data)
|
||||
@@ -43,7 +43,7 @@ const useTranslate = () => {
|
||||
const reviewed = useAppSelector(state => state.env.tempData.reviewed)
|
||||
const reviewAction = useAppSelector(state => state.env.reviewAction)
|
||||
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
|
||||
const {sendExtension} = useMessage()
|
||||
const {sendExtension} = useMessage(!!envData.sidePanel)
|
||||
/**
|
||||
* 获取下一个需要翻译的行
|
||||
* 会检测冷却
|
||||
@@ -135,7 +135,7 @@ const useTranslate = () => {
|
||||
}
|
||||
})
|
||||
dispatch(addTransResults(result))
|
||||
const task = await sendExtension('ADD_TASK', {taskDef})
|
||||
const task = await sendExtension(null, 'ADD_TASK', {taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ const useTranslate = () => {
|
||||
console.debug('addSummarizeTask', taskDef)
|
||||
dispatch(setSummaryStatus({segmentStartIdx: segment.startIdx, type, status: 'pending'}))
|
||||
dispatch(setLastSummarizeTime(Date.now()))
|
||||
const task = await sendExtension('ADD_TASK', {taskDef})
|
||||
const task = await sendExtension(null, 'ADD_TASK', {taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}, [dispatch, envData, summarizeLanguage.name, title])
|
||||
@@ -262,7 +262,7 @@ const useTranslate = () => {
|
||||
id,
|
||||
status: 'pending'
|
||||
}))
|
||||
const task = await sendExtension('ADD_TASK', {taskDef})
|
||||
const task = await sendExtension(null, 'ADD_TASK', {taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}, [dispatch, envData, summarizeLanguage.name, title])
|
||||
@@ -330,7 +330,7 @@ const useTranslate = () => {
|
||||
})
|
||||
|
||||
const getTask = useCallback(async (taskId: string) => {
|
||||
const taskResp = await sendExtension('GET_TASK', {taskId})
|
||||
const taskResp = await sendExtension(null, 'GET_TASK', {taskId})
|
||||
if (taskResp.code === 'ok') {
|
||||
console.debug('getTask', taskResp.task)
|
||||
const task: Task = taskResp.task
|
||||
|
Reference in New Issue
Block a user