From af1e8a347ea9eb3a0ff4483f08c7a7b248729366 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 14 Nov 2019 05:50:50 +0000 Subject: [PATCH] chore: remove unused promisify code (#21114) --- docs/api/modernization/promisification.md | 13 +--- docs/api/process.md | 6 -- lib/common/api/deprecate.ts | 58 ------------------ spec-main/api-deprecate-spec.ts | 75 ----------------------- typings/internal-electron.d.ts | 5 -- 5 files changed, 1 insertion(+), 156 deletions(-) diff --git a/docs/api/modernization/promisification.md b/docs/api/modernization/promisification.md index 1c95f202ca33..333b978c97c3 100644 --- a/docs/api/modernization/promisification.md +++ b/docs/api/modernization/promisification.md @@ -1,17 +1,6 @@ ## Promisification -The Electron team is currently undergoing an initiative to convert callback-based functions in Electron to return Promises. During this transition period, both the callback and Promise-based versions of these functions will work correctly, and will both be documented. - -To enable deprecation warnings for these updated functions, use the [`process.enablePromiseAPIs` runtime flag](../process.md#processenablepromiseapis). - -When a majority of affected functions are migrated, this flag will be enabled by default and all developers will be able to see these deprecation warnings. At that time, the callback-based versions will also be removed from documentation. This document will be continuously updated as more functions are converted. - -### Candidate Functions - -- [app.importCertificate(options, callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#importCertificate) -- [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print) - -### Converted Functions +The Electron team recently underwent an initiative to convert callback-based APIs to Promise-based ones. See converted functions below: - [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon) - [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage) diff --git a/docs/api/process.md b/docs/api/process.md index 87e34cba02f4..bf5dc57fe792 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -82,12 +82,6 @@ A `Boolean` that controls whether or not deprecation warnings are printed to `st Setting this to `true` will silence deprecation warnings. This property is used instead of the `--no-deprecation` command line flag. -### `process.enablePromiseAPIs` - -A `Boolean` that controls whether or not deprecation warnings are printed to `stderr` when -formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to `true` -will enable deprecation warnings. - ### `process.resourcesPath` _Readonly_ A `String` representing the path to the resources directory. diff --git a/lib/common/api/deprecate.ts b/lib/common/api/deprecate.ts index ceeb54cf08c1..946ff20712d3 100644 --- a/lib/common/api/deprecate.ts +++ b/lib/common/api/deprecate.ts @@ -116,64 +116,6 @@ const deprecate: ElectronInternal.DeprecationUtil = { }) }, - // deprecate a callback-based function in favor of one returning a Promise - promisify: any>(fn: T): T => { - const fnName = fn.name || 'function' - const oldName = `${fnName} with callbacks` - const newName = `${fnName} with Promises` - const warn = warnOnce(oldName, newName) - - return function (this: any, ...params: any[]) { - let cb: Function | undefined - if (params.length > 0 && typeof params[params.length - 1] === 'function') { - cb = params.pop() - } - const promise = fn.apply(this, params) - if (!cb) return promise - if (process.enablePromiseAPIs) warn() - return promise - .then((res: any) => { - process.nextTick(() => { - cb!.length === 2 ? cb!(null, res) : cb!(res) - }) - return res - }, (err: Error) => { - process.nextTick(() => { - cb!.length === 2 ? cb!(err) : cb!() - }) - throw err - }) - } as T - }, - - // convertPromiseValue: Temporarily disabled until it's used - // deprecate a callback-based function in favor of one returning a Promise - promisifyMultiArg: any>(fn: T /* convertPromiseValue: (v: any) => any */): T => { - const fnName = fn.name || 'function' - const oldName = `${fnName} with callbacks` - const newName = `${fnName} with Promises` - const warn = warnOnce(oldName, newName) - - return function (this: any, ...params) { - let cb: Function | undefined - if (params.length > 0 && typeof params[params.length - 1] === 'function') { - cb = params.pop() - } - const promise = fn.apply(this, params) - if (!cb) return promise - if (process.enablePromiseAPIs) warn() - return promise - .then((res: any) => { - process.nextTick(() => { - // eslint-disable-next-line standard/no-callback-literal - cb!.length > 2 ? cb!(null, ...res) : cb!(...res) - }) - }, (err: Error) => { - process.nextTick(() => cb!(err)) - }) - } as T - }, - // change the name of a property renameProperty: (o, oldName, newName) => { const warn = warnOnce(oldName, newName) diff --git a/spec-main/api-deprecate-spec.ts b/spec-main/api-deprecate-spec.ts index c9aa3d7f3879..d65dc08fde7b 100644 --- a/spec-main/api-deprecate-spec.ts +++ b/spec-main/api-deprecate-spec.ts @@ -192,79 +192,4 @@ describe('deprecate', () => { expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.') }) }) - - describe('promisify', () => { - const expected = 'Hello, world!' - let promiseFunc: (param: any) => Promise - let warnings: string[] - - const enableCallbackWarnings = () => { - warnings = [] - deprecate.setHandler(warning => warnings.push(warning)) - process.enablePromiseAPIs = true - } - - beforeEach(() => { - deprecate.setHandler(null) - process.throwDeprecation = true - - promiseFunc = param => new Promise((resolve) => resolve(param)) - }) - - it('acts as a pass-through for promise-based invocations', async () => { - enableCallbackWarnings() - promiseFunc = deprecate.promisify(promiseFunc) - - const actual = await promiseFunc(expected) - expect(actual).to.equal(expected) - expect(warnings).to.have.lengthOf(0) - }) - - it('only calls back an error if the callback is called with (err, data)', async () => { - enableCallbackWarnings() - const erringPromiseFunc = deprecate.promisify( - () => new Promise((resolve, reject) => { - reject(new Error('fail')) - }) - ) - - { - const [err, data] = await new Promise(resolve => { - (erringPromiseFunc as any)((err: Error | undefined, data: any) => { - resolve([err, data]) - }).catch(() => { /* silence deprecation warning */ }) - }) - expect(data).to.be.undefined() - expect(err).to.be.an.instanceOf(Error).with.property('message', 'fail') - } - { - const data = await new Promise(resolve => { - (erringPromiseFunc as any)((data: any) => { resolve(data) }) - .catch(() => { /* silence deprecation warning */ }) - }) - expect(data).to.be.undefined() - } - }) - - it('warns exactly once for callback-based invocations', (done) => { - enableCallbackWarnings() - promiseFunc = deprecate.promisify(promiseFunc) - - let callbackCount = 0 - const invocationCount = 3 - const callback = (actual: number) => { - expect(actual).to.equal(expected) - expect(warnings).to.have.lengthOf(1) - expect(warnings[0]).to.include('promiseFunc') - callbackCount += 1 - if (callbackCount === invocationCount) { - done() - } - } - - for (let i = 0; i < invocationCount; i += 1) { - (promiseFunc as any)(expected, callback) - } - }) - }) }) diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index 0b9265da255e..e8db2167c650 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -90,11 +90,6 @@ declare namespace ElectronInternal { removeProperty(object: T, propertyName: K): T; renameProperty(object: T, oldName: string, newName: K): T; moveAPI(fn: Function, oldUsage: string, newUsage: string): Function; - - promisify any>(fn: T): T; - - // convertPromiseValue: Temporarily disabled until it's used - promisifyMultiArg any>(fn: T, /*convertPromiseValue: (v: any) => any*/): T; } interface DesktopCapturer {