2019-02-14 22:29:20 +00:00
|
|
|
let deprecationHandler: ElectronInternal.DeprecationHandler | null = null
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
function warnOnce (oldName: string, newName?: string) {
|
2018-09-12 22:13:22 +00:00
|
|
|
let warned = false
|
|
|
|
const msg = newName
|
|
|
|
? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
|
|
|
|
: `'${oldName}' is deprecated and will be removed.`
|
|
|
|
return () => {
|
|
|
|
if (!warned && !process.noDeprecation) {
|
|
|
|
warned = true
|
|
|
|
deprecate.log(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
const deprecate: ElectronInternal.DeprecationUtil = {
|
2018-09-11 18:16:49 +00:00
|
|
|
setHandler: (handler) => { deprecationHandler = handler },
|
|
|
|
getHandler: () => deprecationHandler,
|
|
|
|
warn: (oldName, newName) => {
|
2018-12-05 09:34:09 +00:00
|
|
|
if (!process.noDeprecation) {
|
|
|
|
deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
|
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
|
|
|
log: (message) => {
|
|
|
|
if (typeof deprecationHandler === 'function') {
|
|
|
|
deprecationHandler(message)
|
|
|
|
} else if (process.throwDeprecation) {
|
|
|
|
throw new Error(message)
|
|
|
|
} else if (process.traceDeprecation) {
|
|
|
|
return console.trace(message)
|
2018-04-23 19:46:12 +00:00
|
|
|
} else {
|
2018-09-11 18:16:49 +00:00
|
|
|
return console.warn(`(electron) ${message}`)
|
2018-04-23 19:46:12 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
|
2019-02-08 17:50:11 +00:00
|
|
|
function: (fn, newName) => {
|
|
|
|
const warn = warnOnce(fn.name, newName)
|
2019-02-14 22:29:20 +00:00
|
|
|
return function (this: any) {
|
2019-02-08 17:50:11 +00:00
|
|
|
warn()
|
|
|
|
fn.apply(this, arguments)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
event: (emitter, oldName, newName) => {
|
2018-09-12 22:13:22 +00:00
|
|
|
const warn = newName.startsWith('-') /* internal event */
|
|
|
|
? warnOnce(`${oldName} event`)
|
|
|
|
: warnOnce(`${oldName} event`, `${newName} event`)
|
2019-02-14 22:29:20 +00:00
|
|
|
return emitter.on(newName, function (this: NodeJS.EventEmitter, ...args) {
|
2018-09-12 22:13:22 +00:00
|
|
|
if (this.listenerCount(oldName) !== 0) {
|
|
|
|
warn()
|
|
|
|
this.emit(oldName, ...args)
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
removeProperty: (o, removedName) => {
|
|
|
|
// if the property's already been removed, warn about it
|
|
|
|
if (!(removedName in o)) {
|
|
|
|
deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`)
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
2018-05-29 13:58:02 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
// wrap the deprecated property in an accessor to warn
|
|
|
|
const warn = warnOnce(removedName)
|
|
|
|
let val = o[removedName]
|
|
|
|
return Object.defineProperty(o, removedName, {
|
2018-09-11 18:16:49 +00:00
|
|
|
configurable: true,
|
|
|
|
get: () => {
|
|
|
|
warn()
|
2018-09-12 22:13:22 +00:00
|
|
|
return val
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
set: newVal => {
|
2018-09-11 18:16:49 +00:00
|
|
|
warn()
|
2018-09-12 22:13:22 +00:00
|
|
|
val = newVal
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
promisify: <T extends (...args: any[]) => any>(fn: T): T => {
|
2018-12-10 16:13:09 +00:00
|
|
|
const fnName = fn.name || 'function'
|
|
|
|
const oldName = `${fnName} with callbacks`
|
|
|
|
const newName = `${fnName} with Promises`
|
2018-11-19 17:56:26 +00:00
|
|
|
const warn = warnOnce(oldName, newName)
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
return function (this: any, ...params: any[]) {
|
|
|
|
let cb: Function | undefined
|
2019-01-25 22:23:24 +00:00
|
|
|
if (params.length > 0 && typeof params[params.length - 1] === 'function') {
|
|
|
|
cb = params.pop()
|
|
|
|
}
|
2018-12-10 16:13:09 +00:00
|
|
|
const promise = fn.apply(this, params)
|
2019-01-25 22:23:24 +00:00
|
|
|
if (!cb) return promise
|
2018-12-10 16:13:09 +00:00
|
|
|
if (process.enablePromiseAPIs) warn()
|
|
|
|
return promise
|
2019-02-14 22:29:20 +00:00
|
|
|
.then((res: any) => {
|
2018-12-10 16:13:09 +00:00
|
|
|
process.nextTick(() => {
|
2019-02-14 22:29:20 +00:00
|
|
|
cb!.length === 2 ? cb!(null, res) : cb!(res)
|
2018-12-10 16:13:09 +00:00
|
|
|
})
|
2019-02-14 22:29:20 +00:00
|
|
|
}, (err: Error) => {
|
2019-02-07 18:25:20 +00:00
|
|
|
process.nextTick(() => {
|
2019-02-14 22:29:20 +00:00
|
|
|
cb!.length === 2 ? cb!(err) : cb!()
|
2019-02-07 18:25:20 +00:00
|
|
|
})
|
2018-11-19 17:56:26 +00:00
|
|
|
})
|
2019-02-14 22:29:20 +00:00
|
|
|
} as T
|
2018-11-19 17:56:26 +00:00
|
|
|
},
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
promisifyMultiArg: <T extends (...args: any[]) => any>(fn: T): T => {
|
2019-02-13 21:24:57 +00:00
|
|
|
const fnName = fn.name || 'function'
|
|
|
|
const oldName = `${fnName} with callbacks`
|
|
|
|
const newName = `${fnName} with Promises`
|
|
|
|
const warn = warnOnce(oldName, newName)
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
return function (this: any, ...params) {
|
|
|
|
let cb: Function | undefined
|
2019-02-13 21:24:57 +00:00
|
|
|
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
|
2019-02-14 22:29:20 +00:00
|
|
|
.then((res: any) => {
|
2019-02-13 21:24:57 +00:00
|
|
|
process.nextTick(() => {
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2019-02-14 22:29:20 +00:00
|
|
|
cb!.length > 2 ? cb!(null, ...res) : cb!(...res)
|
2019-02-13 21:24:57 +00:00
|
|
|
})
|
2019-02-14 22:29:20 +00:00
|
|
|
}, (err: Error) => {
|
|
|
|
process.nextTick(() => cb!(err))
|
2019-02-13 21:24:57 +00:00
|
|
|
})
|
2019-02-14 22:29:20 +00:00
|
|
|
} as T
|
2019-02-13 21:24:57 +00:00
|
|
|
},
|
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
renameProperty: (o, oldName, newName) => {
|
|
|
|
const warn = warnOnce(oldName, newName)
|
|
|
|
|
|
|
|
// if the new property isn't there yet,
|
|
|
|
// inject it and warn about it
|
|
|
|
if ((oldName in o) && !(newName in o)) {
|
2018-05-29 13:58:02 +00:00
|
|
|
warn()
|
2019-02-14 22:29:20 +00:00
|
|
|
o[newName] = (o as any)[oldName]
|
2018-05-29 13:40:19 +00:00
|
|
|
}
|
2018-09-11 18:16:49 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
// wrap the deprecated property in an accessor to warn
|
|
|
|
// and redirect to the new property
|
|
|
|
return Object.defineProperty(o, oldName, {
|
|
|
|
get: () => {
|
2018-09-11 18:16:49 +00:00
|
|
|
warn()
|
2018-09-12 22:13:22 +00:00
|
|
|
return o[newName]
|
2018-09-11 18:16:49 +00:00
|
|
|
},
|
2018-09-12 22:13:22 +00:00
|
|
|
set: value => {
|
2018-09-11 18:16:49 +00:00
|
|
|
warn()
|
2018-09-12 22:13:22 +00:00
|
|
|
o[newName] = value
|
2018-09-11 18:16:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-05-29 13:40:19 +00:00
|
|
|
}
|
2017-11-20 14:12:34 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
export default deprecate
|