chore: simplify promisify helper (#15952)
chore: simplify promisify helper
This commit is contained in:
parent
db2fda1b6f
commit
4b18a38e9f
4 changed files with 73 additions and 23 deletions
|
@ -40,10 +40,7 @@ Object.assign(app, {
|
|||
}
|
||||
})
|
||||
|
||||
const nativeGetFileIcon = app.getFileIcon
|
||||
app.getFileIcon = (path, options = {}, cb) => {
|
||||
return deprecate.promisify(nativeGetFileIcon.call(app, path, options), cb)
|
||||
}
|
||||
app.getFileIcon = deprecate.promisify(app.getFileIcon, 2)
|
||||
|
||||
const nativeAppMetrics = app.getAppMetrics
|
||||
app.getAppMetrics = () => {
|
||||
|
|
|
@ -330,14 +330,11 @@ WebContents.prototype._init = function () {
|
|||
// The navigation controller.
|
||||
NavigationController.call(this, this)
|
||||
|
||||
// Every remote callback from renderer process would add a listenter to the
|
||||
// Every remote callback from renderer process would add a listener to the
|
||||
// render-view-deleted event, so ignore the listeners warning.
|
||||
this.setMaxListeners(0)
|
||||
|
||||
const nativeCapturePage = this.capturePage
|
||||
this.capturePage = function (rect, cb) {
|
||||
return deprecate.promisify(nativeCapturePage.call(this, rect), cb)
|
||||
}
|
||||
this.capturePage = deprecate.promisify(this.capturePage, 1)
|
||||
|
||||
// Dispatch IPC messages to the ipc module.
|
||||
this.on('ipc-message', function (event, [channel, ...args]) {
|
||||
|
|
|
@ -69,23 +69,29 @@ const deprecate = {
|
|||
})
|
||||
},
|
||||
|
||||
promisify: (promise, cb) => {
|
||||
const oldName = `function with callbacks`
|
||||
const newName = `function with Promises`
|
||||
promisify: (fn, cbParamIndex) => {
|
||||
const fnName = fn.name || 'function'
|
||||
const oldName = `${fnName} with callbacks`
|
||||
const newName = `${fnName} with Promises`
|
||||
const warn = warnOnce(oldName, newName)
|
||||
|
||||
if (typeof cb !== 'function') return promise
|
||||
if (process.enablePromiseAPIs) warn()
|
||||
return promise
|
||||
.then(res => {
|
||||
process.nextTick(() => {
|
||||
cb(null, res)
|
||||
return function (...params) {
|
||||
const cb = params.splice(cbParamIndex, 1)[0]
|
||||
const promise = fn.apply(this, params)
|
||||
|
||||
if (typeof cb !== 'function') return promise
|
||||
if (process.enablePromiseAPIs) warn()
|
||||
return promise
|
||||
.then(res => {
|
||||
process.nextTick(() => {
|
||||
cb(null, res)
|
||||
})
|
||||
}, err => {
|
||||
process.nextTick(() => {
|
||||
cb(err)
|
||||
})
|
||||
})
|
||||
}, err => {
|
||||
process.nextTick(() => {
|
||||
cb(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
renameProperty: (o, oldName, newName) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue