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
|
@ -67,7 +67,7 @@ filenames = {
|
||||||
"lib/renderer/content-scripts-injector.js",
|
"lib/renderer/content-scripts-injector.js",
|
||||||
"lib/renderer/init.js",
|
"lib/renderer/init.js",
|
||||||
"lib/renderer/inspector.js",
|
"lib/renderer/inspector.js",
|
||||||
"lib/renderer/ipc-renderer-internal-utils.js",
|
"lib/renderer/ipc-renderer-internal-utils.ts",
|
||||||
"lib/renderer/ipc-renderer-internal.js",
|
"lib/renderer/ipc-renderer-internal.js",
|
||||||
"lib/renderer/remote.js",
|
"lib/renderer/remote.js",
|
||||||
"lib/renderer/security-warnings.js",
|
"lib/renderer/security-warnings.js",
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
|
||||||
const errorUtils = require('@electron/internal/common/error-utils')
|
|
||||||
|
|
||||||
let nextId = 0
|
|
||||||
|
|
||||||
exports.invoke = function (command, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const requestId = ++nextId
|
|
||||||
ipcRenderer.once(`${command}_RESPONSE_${requestId}`, (event, error, result) => {
|
|
||||||
if (error) {
|
|
||||||
reject(errorUtils.deserialize(error))
|
|
||||||
} else {
|
|
||||||
resolve(result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
ipcRenderer.send(command, requestId, ...args)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.invokeSync = function (command, ...args) {
|
|
||||||
const [ error, result ] = ipcRenderer.sendSync(command, ...args)
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
throw errorUtils.deserialize(error)
|
|
||||||
} else {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -76,6 +76,9 @@
|
||||||
"aliasify": {
|
"aliasify": {
|
||||||
"replacements": {
|
"replacements": {
|
||||||
"@electron/internal/(.+)": "./lib/$1"
|
"@electron/internal/(.+)": "./lib/$1"
|
||||||
|
},
|
||||||
|
"appliesTo": {
|
||||||
|
"includeExtensions": [".js", ".ts"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
|
|
15
typings/internal-electron.d.ts
vendored
15
typings/internal-electron.d.ts
vendored
|
@ -6,9 +6,24 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare namespace Electron {
|
declare namespace Electron {
|
||||||
|
enum ProcessType {
|
||||||
|
browser = 'browser',
|
||||||
|
renderer = 'renderer',
|
||||||
|
worker = 'worker'
|
||||||
|
}
|
||||||
|
|
||||||
interface App {
|
interface App {
|
||||||
setVersion(version: string): void;
|
setVersion(version: string): void;
|
||||||
setDesktopName(name: string): void;
|
setDesktopName(name: string): void;
|
||||||
setAppPath(path: string | null): void;
|
setAppPath(path: string | null): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SerializedError {
|
||||||
|
message: string;
|
||||||
|
stack?: string,
|
||||||
|
name: string,
|
||||||
|
from: Electron.ProcessType,
|
||||||
|
cause: SerializedError,
|
||||||
|
__ELECTRON_SERIALIZED_ERROR__: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue