Skip to content

Commit af1e8a3

Browse files
codebyterezcbenz
authored andcommitted
chore: remove unused promisify code (electron#21114)
1 parent 457b7bf commit af1e8a3

File tree

5 files changed

+1
-156
lines changed

5 files changed

+1
-156
lines changed

docs/api/modernization/promisification.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
## Promisification
22

3-
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.
4-
5-
To enable deprecation warnings for these updated functions, use the [`process.enablePromiseAPIs` runtime flag](../process.md#processenablepromiseapis).
6-
7-
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.
8-
9-
### Candidate Functions
10-
11-
- [app.importCertificate(options, callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#importCertificate)
12-
- [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print)
13-
14-
### Converted Functions
3+
The Electron team recently underwent an initiative to convert callback-based APIs to Promise-based ones. See converted functions below:
154

165
- [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon)
176
- [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage)

docs/api/process.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ A `Boolean` that controls whether or not deprecation warnings are printed to `st
8282
Setting this to `true` will silence deprecation warnings. This property is used
8383
instead of the `--no-deprecation` command line flag.
8484

85-
### `process.enablePromiseAPIs`
86-
87-
A `Boolean` that controls whether or not deprecation warnings are printed to `stderr` when
88-
formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to `true`
89-
will enable deprecation warnings.
90-
9185
### `process.resourcesPath` _Readonly_
9286

9387
A `String` representing the path to the resources directory.

lib/common/api/deprecate.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -116,64 +116,6 @@ const deprecate: ElectronInternal.DeprecationUtil = {
116116
})
117117
},
118118

119-
// deprecate a callback-based function in favor of one returning a Promise
120-
promisify: <T extends (...args: any[]) => any>(fn: T): T => {
121-
const fnName = fn.name || 'function'
122-
const oldName = `${fnName} with callbacks`
123-
const newName = `${fnName} with Promises`
124-
const warn = warnOnce(oldName, newName)
125-
126-
return function (this: any, ...params: any[]) {
127-
let cb: Function | undefined
128-
if (params.length > 0 && typeof params[params.length - 1] === 'function') {
129-
cb = params.pop()
130-
}
131-
const promise = fn.apply(this, params)
132-
if (!cb) return promise
133-
if (process.enablePromiseAPIs) warn()
134-
return promise
135-
.then((res: any) => {
136-
process.nextTick(() => {
137-
cb!.length === 2 ? cb!(null, res) : cb!(res)
138-
})
139-
return res
140-
}, (err: Error) => {
141-
process.nextTick(() => {
142-
cb!.length === 2 ? cb!(err) : cb!()
143-
})
144-
throw err
145-
})
146-
} as T
147-
},
148-
149-
// convertPromiseValue: Temporarily disabled until it's used
150-
// deprecate a callback-based function in favor of one returning a Promise
151-
promisifyMultiArg: <T extends (...args: any[]) => any>(fn: T /* convertPromiseValue: (v: any) => any */): T => {
152-
const fnName = fn.name || 'function'
153-
const oldName = `${fnName} with callbacks`
154-
const newName = `${fnName} with Promises`
155-
const warn = warnOnce(oldName, newName)
156-
157-
return function (this: any, ...params) {
158-
let cb: Function | undefined
159-
if (params.length > 0 && typeof params[params.length - 1] === 'function') {
160-
cb = params.pop()
161-
}
162-
const promise = fn.apply(this, params)
163-
if (!cb) return promise
164-
if (process.enablePromiseAPIs) warn()
165-
return promise
166-
.then((res: any) => {
167-
process.nextTick(() => {
168-
// eslint-disable-next-line standard/no-callback-literal
169-
cb!.length > 2 ? cb!(null, ...res) : cb!(...res)
170-
})
171-
}, (err: Error) => {
172-
process.nextTick(() => cb!(err))
173-
})
174-
} as T
175-
},
176-
177119
// change the name of a property
178120
renameProperty: (o, oldName, newName) => {
179121
const warn = warnOnce(oldName, newName)

spec-main/api-deprecate-spec.ts

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -192,79 +192,4 @@ describe('deprecate', () => {
192192
expect(warnings[0]).to.equal('\'old\' is deprecated and will be removed. Please use \'new\' instead.')
193193
})
194194
})
195-
196-
describe('promisify', () => {
197-
const expected = 'Hello, world!'
198-
let promiseFunc: (param: any) => Promise<any>
199-
let warnings: string[]
200-
201-
const enableCallbackWarnings = () => {
202-
warnings = []
203-
deprecate.setHandler(warning => warnings.push(warning))
204-
process.enablePromiseAPIs = true
205-
}
206-
207-
beforeEach(() => {
208-
deprecate.setHandler(null)
209-
process.throwDeprecation = true
210-
211-
promiseFunc = param => new Promise((resolve) => resolve(param))
212-
})
213-
214-
it('acts as a pass-through for promise-based invocations', async () => {
215-
enableCallbackWarnings()
216-
promiseFunc = deprecate.promisify(promiseFunc)
217-
218-
const actual = await promiseFunc(expected)
219-
expect(actual).to.equal(expected)
220-
expect(warnings).to.have.lengthOf(0)
221-
})
222-
223-
it('only calls back an error if the callback is called with (err, data)', async () => {
224-
enableCallbackWarnings()
225-
const erringPromiseFunc = deprecate.promisify(
226-
() => new Promise((resolve, reject) => {
227-
reject(new Error('fail'))
228-
})
229-
)
230-
231-
{
232-
const [err, data] = await new Promise(resolve => {
233-
(erringPromiseFunc as any)((err: Error | undefined, data: any) => {
234-
resolve([err, data])
235-
}).catch(() => { /* silence deprecation warning */ })
236-
})
237-
expect(data).to.be.undefined()
238-
expect(err).to.be.an.instanceOf(Error).with.property('message', 'fail')
239-
}
240-
{
241-
const data = await new Promise(resolve => {
242-
(erringPromiseFunc as any)((data: any) => { resolve(data) })
243-
.catch(() => { /* silence deprecation warning */ })
244-
})
245-
expect(data).to.be.undefined()
246-
}
247-
})
248-
249-
it('warns exactly once for callback-based invocations', (done) => {
250-
enableCallbackWarnings()
251-
promiseFunc = deprecate.promisify(promiseFunc)
252-
253-
let callbackCount = 0
254-
const invocationCount = 3
255-
const callback = (actual: number) => {
256-
expect(actual).to.equal(expected)
257-
expect(warnings).to.have.lengthOf(1)
258-
expect(warnings[0]).to.include('promiseFunc')
259-
callbackCount += 1
260-
if (callbackCount === invocationCount) {
261-
done()
262-
}
263-
}
264-
265-
for (let i = 0; i < invocationCount; i += 1) {
266-
(promiseFunc as any)(expected, callback)
267-
}
268-
})
269-
})
270195
})

typings/internal-electron.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ declare namespace ElectronInternal {
9090
removeProperty<T, K extends (keyof T & string)>(object: T, propertyName: K): T;
9191
renameProperty<T, K extends (keyof T & string)>(object: T, oldName: string, newName: K): T;
9292
moveAPI(fn: Function, oldUsage: string, newUsage: string): Function;
93-
94-
promisify<T extends (...args: any[]) => any>(fn: T): T;
95-
96-
// convertPromiseValue: Temporarily disabled until it's used
97-
promisifyMultiArg<T extends (...args: any[]) => any>(fn: T, /*convertPromiseValue: (v: any) => any*/): T;
9893
}
9994

10095
interface DesktopCapturer {

0 commit comments

Comments
 (0)