Skip to content

Commit

Permalink
fix rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Mar 6, 2024
1 parent 0831ebe commit 9554ab3
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions app/src/ts/renderer/ipc-back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ function createChannelName (): string {
}

let backWindowPort: MessagePort
const backWindowPortEvent = new window.node.events.EventEmitter()

interface Deferred {
resolve: (value: any) => void
reject: (reason: any) => void
}

const promiseMap = new Map<string, Deferred>()

ipcRenderer.on('port', e => {
backWindowPort = e.ports[0]
Expand All @@ -21,7 +27,18 @@ ipcRenderer.on('port', e => {
if (ev.data.type === 'setBatchStatus') {
store.commit(Action.SET_BATCH_STATUS, ev.data.payload[0])
}
backWindowPortEvent.emit('message', ev)

const channel = ev.data.id
if (typeof channel === 'string') {
if (promiseMap.has(channel)) {
const deferred = promiseMap.get(channel)!
if (ev.data.err) {
deferred.reject(new Error(ev.data.err))
} else {
deferred.resolve(ev.data.data)
}
}
}
}
})

Expand All @@ -32,13 +49,14 @@ function invokeBackWindow<T> (name: string, args: any[] = []): Promise<T> {
return
}
const callbackChannel = createChannelName()
backWindowPortEvent.once('message', (ev) => {
if (ev.data.id === callbackChannel) {
if (ev.data.err) {
reject(new Error(ev.data.err))
} else {
resolve(ev.data.data)
}
promiseMap.set(callbackChannel, {
resolve: (value) => {
promiseMap.delete(callbackChannel)
resolve(value)
},
reject: (reason) => {
promiseMap.delete(callbackChannel)
reject(reason)
}
})
backWindowPort.postMessage({
Expand Down

0 comments on commit 9554ab3

Please sign in to comment.