Skip to content

Commit

Permalink
优化:更新接口对接类
Browse files Browse the repository at this point in the history
  • Loading branch information
modstart committed Oct 22, 2024
1 parent c628ca4 commit 88cab5a
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 20 deletions.
11 changes: 7 additions & 4 deletions electron/mapi/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,18 @@ const spawnShell = async (command: string | string[], option: {
*/
const availablePort = async (start: number): Promise<number> => {
for (let i = start; i < 65535; i++) {
const available = await isPortAvailable('0.0.0.0', i)
// console.log('isPortAvailable', i, available)
if (available) {
const available = await isPortAvailable(i, '0.0.0.0')
const availableLocal = await isPortAvailable(i, '127.0.0.1')
// console.log('isPortAvailable', i, available, availableLocal)
if (available && availableLocal) {
return i
}
}
throw new Error('no available port')
}

const isPortAvailable = async (host: string, port: number): Promise<boolean> => {

const isPortAvailable = async (port: number, host?: string): Promise<boolean> => {
return new Promise((resolve) => {
const server = net.createServer()
server.listen(port, host)
Expand All @@ -164,6 +165,8 @@ const fixExecutable = async (executable: string) => {
export const Apps = {
shell,
spawnShell,
availablePort,
isPortAvailable,
}

export default {
Expand Down
44 changes: 42 additions & 2 deletions electron/mapi/event/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {AppRuntime} from "../env";
import {ipcMain} from "electron";
import {StrUtil} from "../../lib/util";


const init = () => {

}

type NameType = 'main' | string
type EventType = 'APP_READY' | 'CALL_THIRD_PARTY' | 'CALL_PAGE'
type EventType = 'APP_READY' | 'CALL_THIRD_PARTY' | 'CALL_PAGE' | 'CHANNEL'

const send = (name: NameType, type: EventType, data: any = {}, id?: string): boolean => {
id = id || StrUtil.randomString(32)
Expand All @@ -16,6 +17,7 @@ const send = (name: NameType, type: EventType, data: any = {}, id?: string): boo
if (!AppRuntime.mainWindow) {
return false
}
// console.log('send', payload)
AppRuntime.mainWindow?.webContents.send('MAIN_PROCESS_MESSAGE', payload)
} else {
if (!AppRuntime.windows[name]) {
Expand Down Expand Up @@ -76,11 +78,49 @@ ipcMain.handle('event:callPage', async (_, name: string, type: string, data: any
})
})

const sendChannel = (channel: string, data: any) => {
send('main', 'CHANNEL', {channel, data})
}

let onChannelIsListen = false
let channelOnCallback = {}

const onChannel = (channel: string, callback: (data: any) => void) => {
if (!channelOnCallback[channel]) {
channelOnCallback[channel] = []
}
channelOnCallback[channel].push(callback)
if (!onChannelIsListen) {
onChannelIsListen = true
ipcMain.handle('event:channelSend', (event, channel_, data) => {
if (channelOnCallback[channel_]) {
channelOnCallback[channel_].forEach((callback: (data: any) => void) => {
callback(data)
})
}
})
}
}

const offChannel = (channel: string, callback: (data: any) => void) => {
if (channelOnCallback[channel]) {
channelOnCallback[channel] = channelOnCallback[channel].filter((item: (data: any) => void) => {
return item !== callback
})
}
if (channelOnCallback[channel].length === 0) {
delete channelOnCallback[channel]
}
}

export default {
init,
send
}

export const Events = {
send
send,
sendChannel,
onChannel,
offChannel,
}
23 changes: 21 additions & 2 deletions electron/mapi/event/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const init = () => {
}

const send = (name: string, type: string, data: any = {}) => {
ipcRenderer.invoke('event:send', name, type, data).then()
return ipcRenderer.invoke('event:send', name, type, data).then()
}

const callThirdParty = async (name: string, type: string, data: any, option: any) => {
Expand All @@ -16,9 +16,28 @@ const callPage = async (name: string, type: string, data: any, option: any) => {
return ipcRenderer.invoke('event:callPage', name, type, data, option)
}


const channelCreate = async (callback: (data: any) => void) => {
const channel = Math.random().toString(36).substring(2)
window['__channel'] = window['__channel'] || {}
window['__channel'][channel] = callback
return channel
}

const channelDestroy = async (channel: string) => {
delete window['__channel'][channel]
}

const channelSend = async (channel: string, data: any) => {
return ipcRenderer.invoke('event:channelSend', channel, data)
}

export default {
init,
send,
callThirdParty,
callPage
callPage,
channelCreate,
channelDestroy,
channelSend,
}
5 changes: 4 additions & 1 deletion electron/mapi/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import page from "./page/main";
import user from "./user/main";
import misc from "./misc/main";

import server from "./server/main";

const $mapi = {
app,
log,
Expand All @@ -23,7 +25,8 @@ const $mapi = {
keys,
page,
user,
misc
misc,
server
}

export const MAPI = {
Expand Down
2 changes: 2 additions & 0 deletions electron/mapi/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import misc from "./misc/render";
import adb from "./adb/render";
import scrcpy from "./scrcpy/render";
import ffmpeg from "./ffmpeg/render";
import server from "./server/render";

export const MAPI = {
init(env: typeof AppEnv = null) {
Expand All @@ -43,6 +44,7 @@ export const MAPI = {
adb,
scrcpy,
ffmpeg,
server,
})
db.init()
event.init()
Expand Down
10 changes: 10 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ ipcRenderer.on('MAIN_PROCESS_MESSAGE', (_event: any, payload: any) => {
(resultData: any) => send(0, 'ok', resultData),
(error: string) => send(-1, error)
)
} else if ('CHANNEL' === payload.type) {
// console.log('CHANNEL', payload)
const {channel, data} = payload.data
if (!window['__channel']) {
return
}
if (!window['__channel'][channel]) {
return
}
window['__channel'][channel](data)
} else {
console.warn('Unknown message from main process:', JSON.stringify(payload))
}
Expand Down
10 changes: 9 additions & 1 deletion src/declarations/mapi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ declare interface Window {
send: (name: string, type: string, data: any) => void,
callThirdParty: (name: string, type: string, data: any, option?: any) => Promise<ApiResult<any>>,
callPage: (name: string, type: string, data?: any, option?: any) => Promise<ApiResult<any>>,
// channel main <-> render
channelCreate: (callback: (data: any) => void) => Promise<string>,
channelDestroy: (channel: string) => Promise<void>,
channelSend: (channel: string, data: any) => Promise<void>,
},
page: {
open: (name: string, option?: any) => Promise<void>,
Expand Down Expand Up @@ -180,7 +184,11 @@ declare interface Window {
ffmpeg: {
version: () => Promise<string>,
run: (args: string[]) => Promise<string>,
}
},
server: {
// define any string to any value
[key: string]: Function,
},
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/store/modules/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {mapError} from "../../lib/error";

export type TaskRecordStatus = 'queue' | 'running' | 'querying' | 'success' | 'fail' | 'delete'

export type TaskRecordRunStatus = 'retry' | 'success'
export type TaskRecordRunStatus = 'retry' | 'success' | 'querying'

export type TaskRecordQueryStatus = 'running' | 'success' | 'fail'

Expand Down Expand Up @@ -87,23 +87,27 @@ export const taskStore = defineStore("task", {
record.status = 'running'
record.runStart = Date.now()
record.runCalling = true
this.fireChange(record, 'running')
this.bizMap[record.biz]
.runFunc(record.bizId, record.bizParam)
.then((status: TaskRecordRunStatus) => {
if ('retry' === status) {
record.status = 'queue'
record.runStart = 0
record.runAfter = Date.now() + 1000
} else {
if (!!this.bizMap[record.biz].queryFunc) {
switch (status) {
case 'success':
record.status = 'success'
break
case 'querying':
record.queryAfter = Date.now() + record.queryInterval
record.status = 'querying'
} else {
record.status = 'success'
}
break
case 'retry':
record.status = 'queue'
record.runStart = 0
record.runAfter = Date.now() + 1000
break
}
})
.catch((e) => {
console.error('task.runFunc.error', e)
record.status = 'fail'
record.msg = mapError(e)
})
Expand Down Expand Up @@ -136,6 +140,7 @@ export const taskStore = defineStore("task", {
}
})
.catch((e) => {
console.error('task.queryFunc.error', e)
record.status = 'fail'
record.msg = mapError(e)
changed = true
Expand Down Expand Up @@ -166,6 +171,7 @@ export const taskStore = defineStore("task", {
record.status = 'delete'
})
.catch((e) => {
console.error('task.successFunc.error', e)
record.status = 'fail'
record.msg = mapError(e)
})
Expand All @@ -190,6 +196,8 @@ export const taskStore = defineStore("task", {
.then(() => {
})
.catch((e) => {
console.error('task.failFunc.error', e)
window.$mapi.log.error(`task.failFunc:${e}`)
})
.finally(() => {
this.fireChange(record, 'fail')
Expand Down

0 comments on commit 88cab5a

Please sign in to comment.