Skip to content

Commit

Permalink
优化:跨页面调用方式优化
Browse files Browse the repository at this point in the history
  • Loading branch information
modstart committed Dec 14, 2024
1 parent c442ca0 commit ca69c69
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v0.3.0

- 新增:调试窗口统一管理,方便查看调试信息
- 优化:跨页面调用方式优化

## v0.2.0

Expand Down
17 changes: 14 additions & 3 deletions electron/mapi/event/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,34 @@ ipcMain.handle('event:send', async (_, name: NameType, type: EventType, data: an
})

const callPage = async (name: string, type: string, data: any, option?: {
waitReadyTimeout?: number,
timeout?: number
}) => {
option = Object.assign({timeout: 10}, option)
option = Object.assign({
waitReadyTimeout: 10 * 1000,
timeout: 10 * 1000
}, option)
return new Promise((resolve, reject) => {
const id = StrUtil.randomString(32)
const timer = setTimeout(() => {
ipcMain.removeListener(listenerKey, listener)
resolve({code: -1, msg: 'timeout'})
}, option.timeout * 1000)
}, option.timeout)
const listener = (_, result) => {
clearTimeout(timer)
resolve(result)
return true
}
const listenerKey = 'event:callPage:' + id
ipcMain.once(listenerKey, listener)
if (!send(name, 'CALL_PAGE', {type, data}, id)) {
const payload = {
type,
data,
option: {
waitReadyTimeout: option.waitReadyTimeout
}
}
if (!send(name, 'CALL_PAGE', payload, id)) {
clearTimeout(timer)
ipcMain.removeListener(listenerKey, listener)
resolve({code: -1, msg: 'send failed'})
Expand Down
40 changes: 33 additions & 7 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ ipcRenderer.on('MAIN_PROCESS_MESSAGE', (_event: any, payload: any) => {
if ('APP_READY' === payload.type) {
MAPI.init(payload.data.AppEnv)
} else if ('CALL_PAGE' === payload.type) {
const {type, data} = payload.data
let {type, data, option} = payload.data
option = Object.assign({
waitReadyTimeout: 10 * 1000,
}, option)
// console.log('CALL_PAGE', type, {type, data, option})
const resultEventName = `event:callPage:${payload.id}`
const send = (code: number, msg: string, data?: any) => {
ipcRenderer.send(resultEventName, {code, msg, data})
Expand All @@ -74,16 +78,38 @@ ipcRenderer.on('MAIN_PROCESS_MESSAGE', (_event: any, payload: any) => {
send(-1, 'error')
return
}
console.log('CALL_PAGE', type, JSON.stringify(window['__page'].callPage))
const callPageExecute = () => {
window['__page'].callPage[type](
(resultData: any) => send(0, 'ok', resultData),
(error: string) => send(-1, error),
data
)
}
if (!window['__page'].callPage[type]) {
if (option.waitReadyTimeout > 0) {
const start = Date.now()
const monitor = () => {
setTimeout(() => {
if (!window['__page'].callPage[type]) {
if (Date.now() - start > option.waitReadyTimeout) {
send(-1, 'timeout')
return
} else {
monitor()
return
}
} else {
callPageExecute()
}
}, 10)
}
monitor()
return
}
send(-1, 'event not found')
return
}
window['__page'].callPage[type](
(resultData: any) => send(0, 'ok', resultData),
(error: string) => send(-1, error),
data
)
callPageExecute()
} else if ('CHANNEL' === payload.type) {
const {channel, data} = payload.data
if (!window['__page'].channel || !window['__page'].channel[channel]) {
Expand Down
1 change: 0 additions & 1 deletion src/components/Device/DeviceActionScreenshot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const doScreenshot = async () => {
const image = await window.$mapi.adb.screencap(props.device.id)
const base64 = 'data:image/png;base64,' + image
await window.$mapi.app.windowOpen('thirdPartyImageBeautifier')
await window.$mapi.app.windowReady('thirdPartyImageBeautifier')
const res = await window.$mapi.event.callPage('thirdPartyImageBeautifier', 'doSetImage', base64)
console.log('res', res)
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion src/declarations/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ declare interface Window {
},
event: {
send: (name: string, type: string, data: any) => void,
callPage: (name: string, type: string, data?: any, option?: any) => Promise<ApiResult<any>>,
callPage: (name: string, type: string, data?: any, option?: {
waitReadyTimeout?: number,
timeout?: number
}) => Promise<ApiResult<any>>,
channelSend: (channel: string, data: any) => Promise<void>,
},
user: {
Expand Down

0 comments on commit ca69c69

Please sign in to comment.