refactor: Port renderer-internal-utils to TypeScript (#16942)
* chore: make aliasify work on .ts files as well * refactor: Port renderer-internal-utils to TypeScript * refactor: Implement feedback <3
This commit is contained in:
parent
46a24c82ff
commit
9112ad01be
5 changed files with 49 additions and 31 deletions
30
lib/renderer/ipc-renderer-internal-utils.ts
Normal file
30
lib/renderer/ipc-renderer-internal-utils.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import * as ipcRenderer from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
import * as errorUtils from '@electron/internal/common/error-utils'
|
||||
|
||||
let nextId = 0
|
||||
|
||||
export function invoke<T> (command: string, ...args: any[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const requestId = ++nextId
|
||||
ipcRenderer.once(`${command}_RESPONSE_${requestId}`, (
|
||||
_event: Electron.Event, error: Electron.SerializedError, result: any
|
||||
) => {
|
||||
if (error) {
|
||||
reject(errorUtils.deserialize(error))
|
||||
} else {
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
ipcRenderer.send(command, requestId, ...args)
|
||||
})
|
||||
}
|
||||
|
||||
export function invokeSync<T> (command: string, ...args: any[]): T {
|
||||
const [ error, result ] = ipcRenderer.sendSync(command, ...args)
|
||||
|
||||
if (error) {
|
||||
throw errorUtils.deserialize(error)
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue