chore: fix promisify helper (#16544)
* chore: fix promise deprecation helper * fix deprecations * update deprecation tests
This commit is contained in:
parent
63bf370cc0
commit
5a35c3a279
12 changed files with 65 additions and 48 deletions
|
@ -36,7 +36,7 @@ Object.assign(app, {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.getFileIcon = deprecate.promisify(app.getFileIcon, 3)
|
app.getFileIcon = deprecate.promisify(app.getFileIcon)
|
||||||
|
|
||||||
const nativeAppMetrics = app.getAppMetrics
|
const nativeAppMetrics = app.getAppMetrics
|
||||||
app.getAppMetrics = () => {
|
app.getAppMetrics = () => {
|
||||||
|
|
|
@ -11,10 +11,10 @@ const fromPartition = (partition) => {
|
||||||
if (!session[wrappedSymbol]) {
|
if (!session[wrappedSymbol]) {
|
||||||
session[wrappedSymbol] = true
|
session[wrappedSymbol] = true
|
||||||
const { cookies } = session
|
const { cookies } = session
|
||||||
cookies.flushStore = deprecate.promisify(cookies.flushStore, 0)
|
cookies.flushStore = deprecate.promisify(cookies.flushStore)
|
||||||
cookies.get = deprecate.promisify(cookies.get, 1)
|
cookies.get = deprecate.promisify(cookies.get)
|
||||||
cookies.remove = deprecate.promisify(cookies.remove, 2)
|
cookies.remove = deprecate.promisify(cookies.remove)
|
||||||
cookies.set = deprecate.promisify(cookies.set, 1)
|
cookies.set = deprecate.promisify(cookies.set)
|
||||||
}
|
}
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,6 @@ Object.setPrototypeOf(Session.prototype, EventEmitter.prototype)
|
||||||
Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype)
|
Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype)
|
||||||
|
|
||||||
Session.prototype._init = function () {
|
Session.prototype._init = function () {
|
||||||
this.protocol.isProtocolHandled = deprecate.promisify(this.protocol.isProtocolHandled, 2)
|
this.protocol.isProtocolHandled = deprecate.promisify(this.protocol.isProtocolHandled)
|
||||||
app.emit('session-created', this)
|
app.emit('session-created', this)
|
||||||
}
|
}
|
||||||
|
|
|
@ -374,7 +374,7 @@ WebContents.prototype._init = function () {
|
||||||
// render-view-deleted event, so ignore the listeners warning.
|
// render-view-deleted event, so ignore the listeners warning.
|
||||||
this.setMaxListeners(0)
|
this.setMaxListeners(0)
|
||||||
|
|
||||||
this.capturePage = deprecate.promisify(this.capturePage, 2)
|
this.capturePage = deprecate.promisify(this.capturePage)
|
||||||
|
|
||||||
// Dispatch IPC messages to the ipc module.
|
// Dispatch IPC messages to the ipc module.
|
||||||
this.on('-ipc-message', function (event, internal, channel, args) {
|
this.on('-ipc-message', function (event, internal, channel, args) {
|
||||||
|
|
|
@ -69,27 +69,27 @@ const deprecate = {
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
promisify: (fn, cbParamIndex) => {
|
promisify: (fn) => {
|
||||||
const fnName = fn.name || 'function'
|
const fnName = fn.name || 'function'
|
||||||
const oldName = `${fnName} with callbacks`
|
const oldName = `${fnName} with callbacks`
|
||||||
const newName = `${fnName} with Promises`
|
const newName = `${fnName} with Promises`
|
||||||
const warn = warnOnce(oldName, newName)
|
const warn = warnOnce(oldName, newName)
|
||||||
|
|
||||||
return function (...params) {
|
return function (...params) {
|
||||||
const cb = params.splice(cbParamIndex, 1)[0]
|
let cb
|
||||||
|
if (params.length > 0 && typeof params[params.length - 1] === 'function') {
|
||||||
|
cb = params.pop()
|
||||||
|
}
|
||||||
const promise = fn.apply(this, params)
|
const promise = fn.apply(this, params)
|
||||||
|
if (!cb) return promise
|
||||||
if (typeof cb !== 'function') return promise
|
|
||||||
if (process.enablePromiseAPIs) warn()
|
if (process.enablePromiseAPIs) warn()
|
||||||
return promise
|
return promise
|
||||||
.then(res => {
|
.then(res => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
cb(null, res)
|
cb.length === 2 ? cb(null, res) : cb(res)
|
||||||
})
|
})
|
||||||
}, err => {
|
}, err => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => cb(err))
|
||||||
cb(err)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -56,4 +56,4 @@ const getSources = (options) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getSources = deprecate.promisify(getSources, 1)
|
exports.getSources = deprecate.promisify(getSources)
|
||||||
|
|
|
@ -859,7 +859,7 @@ describe('app module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('fetches a non-empty icon (callback)', () => {
|
it('fetches a non-empty icon (callback)', (done) => {
|
||||||
app.getFileIcon(iconPath, (icon) => {
|
app.getFileIcon(iconPath, (icon) => {
|
||||||
expect(icon.isEmpty()).to.be.false()
|
expect(icon.isEmpty()).to.be.false()
|
||||||
done()
|
done()
|
||||||
|
@ -875,7 +875,7 @@ describe('app module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('fetches normal icon size by default (callback)', () => {
|
it('fetches normal icon size by default (callback)', (done) => {
|
||||||
app.getFileIcon(iconPath, (icon) => {
|
app.getFileIcon(iconPath, (icon) => {
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
|
|
||||||
|
@ -903,7 +903,7 @@ describe('app module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('fetches a normal icon (callback)', () => {
|
it('fetches a normal icon (callback)', (done) => {
|
||||||
app.getFileIcon(iconPath, { size: 'normal' }, (icon) => {
|
app.getFileIcon(iconPath, { size: 'normal' }, (icon) => {
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
|
|
||||||
|
|
|
@ -507,7 +507,7 @@ describe('BrowserWindow module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('returns a Promise with a Buffer (callback)', () => {
|
it('returns a Promise with a Buffer (callback)', (done) => {
|
||||||
w.capturePage({
|
w.capturePage({
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
|
@ -540,24 +540,26 @@ describe('BrowserWindow module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('preserves transparency (callback)', async () => {
|
it('preserves transparency (callback)', (done) => {
|
||||||
const w = await openTheWindow({
|
openTheWindow({
|
||||||
show: false,
|
show: false,
|
||||||
width: 400,
|
width: 400,
|
||||||
height: 400,
|
height: 400,
|
||||||
transparent: true
|
transparent: true
|
||||||
})
|
}).then(w => {
|
||||||
const p = emittedOnce(w, 'ready-to-show')
|
const p = emittedOnce(w, 'ready-to-show')
|
||||||
w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>')
|
w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>')
|
||||||
await p
|
p.then(() => {
|
||||||
w.show()
|
w.show()
|
||||||
|
|
||||||
w.capturePage((image) => {
|
w.capturePage((image) => {
|
||||||
const imgBuffer = image.toPNG()
|
const imgBuffer = image.toPNG()
|
||||||
// Check the 25th byte in the PNG.
|
// Check the 25th byte in the PNG.
|
||||||
// Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha
|
// Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha
|
||||||
expect(imgBuffer[25]).to.equal(6)
|
expect(imgBuffer[25]).to.equal(6)
|
||||||
done()
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -138,7 +138,7 @@ describe('deprecations', () => {
|
||||||
|
|
||||||
it('acts as a pass-through for promise-based invocations', async () => {
|
it('acts as a pass-through for promise-based invocations', async () => {
|
||||||
enableCallbackWarnings()
|
enableCallbackWarnings()
|
||||||
promiseFunc = deprecate.promisify(promiseFunc, 1)
|
promiseFunc = deprecate.promisify(promiseFunc)
|
||||||
|
|
||||||
const actual = await promiseFunc(expected)
|
const actual = await promiseFunc(expected)
|
||||||
expect(actual).to.equal(expected)
|
expect(actual).to.equal(expected)
|
||||||
|
@ -147,12 +147,11 @@ describe('deprecations', () => {
|
||||||
|
|
||||||
it('warns exactly once for callback-based invocations', (done) => {
|
it('warns exactly once for callback-based invocations', (done) => {
|
||||||
enableCallbackWarnings()
|
enableCallbackWarnings()
|
||||||
promiseFunc = deprecate.promisify(promiseFunc, 1)
|
promiseFunc = deprecate.promisify(promiseFunc)
|
||||||
|
|
||||||
let callbackCount = 0
|
let callbackCount = 0
|
||||||
const invocationCount = 3
|
const invocationCount = 3
|
||||||
const callback = (error, actual) => {
|
const callback = (actual) => {
|
||||||
expect(error).to.be.null()
|
|
||||||
expect(actual).to.equal(expected)
|
expect(actual).to.equal(expected)
|
||||||
expect(warnings).to.have.lengthOf(1)
|
expect(warnings).to.have.lengthOf(1)
|
||||||
expect(warnings[0]).to.include('promiseFunc')
|
expect(warnings[0]).to.include('promiseFunc')
|
||||||
|
|
|
@ -62,9 +62,10 @@ describe('desktopCapturer', () => {
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('responds to subsequent calls of different options (callback)', (done) => {
|
it('responds to subsequent calls of different options (callback)', (done) => {
|
||||||
let callCount = 0
|
let callCount = 0
|
||||||
const callback = (error, sources) => {
|
const callback = (err, sources) => {
|
||||||
callCount++
|
callCount++
|
||||||
expect(error).to.be.null()
|
expect(err).to.be.null()
|
||||||
|
expect(sources).to.not.be.null()
|
||||||
if (callCount === 2) done()
|
if (callCount === 2) done()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -680,14 +680,14 @@ describe('protocol module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('protocol.isProtocolHandled', (done) => {
|
describe('protocol.isProtocolHandled', () => {
|
||||||
it('returns true for about:', async () => {
|
it('returns true for about:', async () => {
|
||||||
const result = await protocol.isProtocolHandled('about')
|
const result = await protocol.isProtocolHandled('about')
|
||||||
assert.strictEqual(result, true)
|
assert.strictEqual(result, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('returns true for about: (callback)', () => {
|
it('returns true for about: (callback)', (done) => {
|
||||||
protocol.isProtocolHandled('about', (result) => {
|
protocol.isProtocolHandled('about', (result) => {
|
||||||
assert.strictEqual(result, true)
|
assert.strictEqual(result, true)
|
||||||
done()
|
done()
|
||||||
|
@ -700,7 +700,7 @@ describe('protocol module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('returns true for file: (callback)', () => {
|
it('returns true for file: (callback)', (done) => {
|
||||||
protocol.isProtocolHandled('file', (result) => {
|
protocol.isProtocolHandled('file', (result) => {
|
||||||
assert.strictEqual(result, true)
|
assert.strictEqual(result, true)
|
||||||
done()
|
done()
|
||||||
|
@ -722,7 +722,7 @@ describe('protocol module', () => {
|
||||||
assert.strictEqual(result, false)
|
assert.strictEqual(result, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns true for custom protocol', () => {
|
it('returns true for custom protocol', (done) => {
|
||||||
const emptyHandler = (request, callback) => callback()
|
const emptyHandler = (request, callback) => callback()
|
||||||
protocol.registerStringProtocol(protocolName, emptyHandler, async (error) => {
|
protocol.registerStringProtocol(protocolName, emptyHandler, async (error) => {
|
||||||
assert.strictEqual(error, null)
|
assert.strictEqual(error, null)
|
||||||
|
@ -733,7 +733,7 @@ describe('protocol module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('returns true for custom protocol (callback)', () => {
|
it('returns true for custom protocol (callback)', (done) => {
|
||||||
const emptyHandler = (request, callback) => callback()
|
const emptyHandler = (request, callback) => callback()
|
||||||
protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
|
protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
|
||||||
assert.strictEqual(error, null)
|
assert.strictEqual(error, null)
|
||||||
|
@ -744,7 +744,7 @@ describe('protocol module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns true for intercepted protocol', () => {
|
it('returns true for intercepted protocol', (done) => {
|
||||||
const emptyHandler = (request, callback) => callback()
|
const emptyHandler = (request, callback) => callback()
|
||||||
protocol.interceptStringProtocol('http', emptyHandler, async (error) => {
|
protocol.interceptStringProtocol('http', emptyHandler, async (error) => {
|
||||||
assert.strictEqual(error, null)
|
assert.strictEqual(error, null)
|
||||||
|
@ -755,7 +755,7 @@ describe('protocol module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO(codebytere): remove when promisification is complete
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('returns true for intercepted protocol (callback)', () => {
|
it('returns true for intercepted protocol (callback)', (done) => {
|
||||||
const emptyHandler = (request, callback) => callback()
|
const emptyHandler = (request, callback) => callback()
|
||||||
protocol.interceptStringProtocol('http', emptyHandler, (error) => {
|
protocol.interceptStringProtocol('http', emptyHandler, (error) => {
|
||||||
assert.strictEqual(error, null)
|
assert.strictEqual(error, null)
|
||||||
|
|
|
@ -266,12 +266,13 @@ describe('session module', () => {
|
||||||
}
|
}
|
||||||
expect(error).to.be.undefined(error)
|
expect(error).to.be.undefined(error)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('with callbacks', (done) => {
|
it('with callbacks', (done) => {
|
||||||
const name = 'foo'
|
const name = 'foo'
|
||||||
const value = 'bar'
|
const value = 'bar'
|
||||||
const { cookies } = session.defaultSession
|
const { cookies } = session.defaultSession
|
||||||
|
|
||||||
cookies.set({ url, name, value }, error => {
|
cookies.set({ url, name, value }, (error) => {
|
||||||
if (error) return done(error)
|
if (error) return done(error)
|
||||||
cookies.flushStore(error => done(error))
|
cookies.flushStore(error => done(error))
|
||||||
})
|
})
|
||||||
|
@ -279,6 +280,19 @@ describe('session module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('ses.cookies.flushStore(callback)', () => {
|
||||||
|
it('flushes the cookies to disk and invokes the callback when done', (done) => {
|
||||||
|
session.defaultSession.cookies.set({
|
||||||
|
url: url,
|
||||||
|
name: 'foo',
|
||||||
|
value: 'bar'
|
||||||
|
}, (error) => {
|
||||||
|
if (error) return done(error)
|
||||||
|
session.defaultSession.cookies.flushStore(() => done())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should survive an app restart for persistent partition', async () => {
|
it('should survive an app restart for persistent partition', async () => {
|
||||||
const appPath = path.join(__dirname, 'fixtures', 'api', 'cookie-app')
|
const appPath = path.join(__dirname, 'fixtures', 'api', 'cookie-app')
|
||||||
const electronPath = remote.getGlobal('process').execPath
|
const electronPath = remote.getGlobal('process').execPath
|
||||||
|
@ -735,7 +749,7 @@ describe('session module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('ses.setCertificateVerifyProc(callback)', () => {
|
describe('ses.setCertificateVerifyProc(callback)', (done) => {
|
||||||
let server = null
|
let server = null
|
||||||
|
|
||||||
beforeEach((done) => {
|
beforeEach((done) => {
|
||||||
|
|
|
@ -597,6 +597,7 @@ describe('webContents module', () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO(codebytere): remove when promisification is complete
|
||||||
it('can set the correct zoom level (callback)', async () => {
|
it('can set the correct zoom level (callback)', async () => {
|
||||||
try {
|
try {
|
||||||
await w.loadURL('about:blank')
|
await w.loadURL('about:blank')
|
||||||
|
|
Loading…
Add table
Reference in a new issue