This commit is contained in:
IndieKKY
2024-10-06 13:50:30 +08:00
parent 7df231443e
commit 86746dc2a3
3 changed files with 14 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ type PortContext<L2ReqMsg, L2ResMsg> = {
id: string
name: string //暂时没什么用
port: chrome.runtime.Port
portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg>
l1protocol: Layer1Protocol<L2ReqMsg, L2ResMsg>
ready: boolean
tabId?: number // 所属tab
@@ -57,7 +57,7 @@ class ExtensionMessaging {
const id = crypto.randomUUID()
const name = port.name
// 创建消息处理器
const portMessageHandler = new Layer1Protocol<L2ReqMsg, L2ResMsg>(async (req: L2ReqMsg) => {
const l1protocol = new Layer1Protocol<L2ReqMsg, L2ResMsg>(async (req: L2ReqMsg) => {
const { tabId } = portContext
const method = this.methods?.[req.method]
if (method != null) {
@@ -84,7 +84,7 @@ class ExtensionMessaging {
}
}, port)
// 创建portContext
const portContext: PortContext<L2ReqMsg, L2ResMsg> = { id, name, port, portMessageHandler, ready: false }
const portContext: PortContext<L2ReqMsg, L2ResMsg> = { id, name, port, l1protocol, ready: false }
this.portIdToPort.set(id, portContext)
// 监听断开连接
@@ -110,7 +110,7 @@ class ExtensionMessaging {
//check tags
if (tags == null || tags.some(tag => portContext.tags?.includes(tag))) {
try {
res = await portContext.portMessageHandler.sendMessage({ method, params, from: 'extension' })
res = await portContext.l1protocol.sendMessage({ method, params, from: 'extension' })
} catch (e) {
console.error('send message to port error', portContext.id, e)
}

View File

@@ -3,7 +3,7 @@ import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSIO
class InjectMessaging {
port?: chrome.runtime.Port
portMessageHandler?: Layer1Protocol<L2ReqMsg, L2ResMsg>
l1protocol?: Layer1Protocol<L2ReqMsg, L2ResMsg>
//类实例
methods?: {
[key: string]: (params: any, context: MethodContext) => Promise<L2ResMsg>
@@ -65,9 +65,9 @@ class InjectMessaging {
this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, {
name: 'bilibili-inject',
})
this.portMessageHandler = new Layer1Protocol<L2ReqMsg, L2ResMsg>(this.messageHandler, this.port)
this.l1protocol = new Layer1Protocol<L2ReqMsg, L2ResMsg>(this.messageHandler, this.port)
//握手
this.portMessageHandler.sendMessage({
this.l1protocol.sendMessage({
from: 'inject',
method: MESSAGE_TO_EXTENSION_HANDSHAKE,
params: {
@@ -78,7 +78,7 @@ class InjectMessaging {
}
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
return await this.portMessageHandler!.sendMessage({
return await this.l1protocol!.sendMessage({
from: 'inject',
method,
params: params ?? {},

View File

@@ -7,13 +7,13 @@ const debug = (...args: any[]) => {
console.debug('[App Messaging]', ...args)
}
let portMessageHandlerInit: boolean = false
let portMessageHandler: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
let l1protocolInit: boolean = false
let l1protocol: Layer1Protocol<L2ReqMsg, L2ResMsg> | undefined
// let postInjectMessage: (method: string, params: PostMessagePayload) => Promise<PostMessageResponse> | undefined
export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
finished: portMessageHandlerInit,
data: portMessageHandler!,
finished: l1protocolInit,
data: l1protocol!,
}), 100, 15000)
const useMessagingService = (methods?: {
@@ -67,7 +67,7 @@ const useMessagingService = (methods?: {
name: 'bilibili-app',
})
}, [])
portMessageHandler = useMemo(() => {
l1protocol = useMemo(() => {
if (messageHandler && port) {
const pmh = new Layer1Protocol<L2ReqMsg, L2ResMsg>(messageHandler, port)
@@ -83,7 +83,7 @@ const useMessagingService = (methods?: {
tags: [TAG_TARGET_APP],
},
})
portMessageHandlerInit = true
l1protocolInit = true
return pmh
}