Skip to content

Commit 36b39c1

Browse files
committed
优化:事件调用方法优化
1 parent 4f38085 commit 36b39c1

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

electron/mapi/event/main.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ ipcMain.handle('event:send', async (_, name: NameType, type: EventType, data: an
4040
send(name, type, data)
4141
})
4242

43-
ipcMain.handle('event:callThirdParty', async (_, name: string, type: string, data: any, option: any) => {
43+
const callThirdParty = async (name: string, type: string, data: any, option?: {
44+
timeout?: number
45+
}) => {
4446
option = Object.assign({timeout: 10}, option)
4547
return new Promise((resolve, reject) => {
4648
const id = StrUtil.randomString(32)
@@ -61,9 +63,16 @@ ipcMain.handle('event:callThirdParty', async (_, name: string, type: string, dat
6163
resolve({code: -1, msg: 'send failed'})
6264
}
6365
})
66+
}
67+
ipcMain.handle('event:callThirdParty', async (_, name: string, type: string, data: any, option?: {
68+
timeout?: number
69+
}) => {
70+
return callThirdParty(name, type, data, option)
6471
})
6572

66-
ipcMain.handle('event:callPage', async (_, name: string, type: string, data: any, option: any) => {
73+
const callPage = async (name: string, type: string, data: any, option?: {
74+
timeout?: number
75+
}) => {
6776
option = Object.assign({timeout: 10}, option)
6877
return new Promise((resolve, reject) => {
6978
const id = StrUtil.randomString(32)
@@ -84,6 +93,12 @@ ipcMain.handle('event:callPage', async (_, name: string, type: string, data: any
8493
resolve({code: -1, msg: 'send failed'})
8594
}
8695
})
96+
}
97+
98+
ipcMain.handle('event:callPage', async (_, name: string, type: string, data: any, option?: {
99+
timeout?: number
100+
}) => {
101+
return callPage(name, type, data, option)
87102
})
88103

89104
const sendChannel = (channel: string, data: any) => {
@@ -130,6 +145,8 @@ export const Events = {
130145
broadcast,
131146
send,
132147
sendChannel,
148+
callThirdParty,
149+
callPage,
133150
onChannel,
134151
offChannel,
135152
}

electron/preload/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ window['__callPage'] = {}
3333
ipcRenderer.on('MAIN_PROCESS_MESSAGE', (_event: any, payload: any) => {
3434
if ('APP_READY' === payload.type) {
3535
MAPI.init(payload.data.AppEnv)
36-
// @ts-ignore
3736
window['__thirdParty'] = window['__thirdParty'] || {}
3837
window['__thirdParty'].name = payload.data.name
3938
} else if ('CALL_THIRD_PARTY' === payload.type) {

src/declarations/type.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
declare interface Window {
2+
__callPage: {
3+
[key: string]: (data: any) => Promise<any>
4+
},
5+
__thirdParty: {
6+
events: {
7+
[key: string]: (success: (data: any) => void, error: (msg: string) => void, data: any) => void
8+
}
9+
},
210
__page: {
311
onBroadcast: (type: string, cb: Function) => void,
412
offBroadcast: (type: string, cb: Function) => void

0 commit comments

Comments
 (0)