2018-09-12 22:13:22 +00:00
|
|
|
'use strict'
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-14 01:21:57 +00:00
|
|
|
let deprecationHandler = null
|
2016-02-16 23:09:35 +00:00
|
|
|
|
2018-09-12 22:13:22 +00:00
|
|
|
function warnOnce (oldName, newName) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 18:16:49 +00:00
|
|
|
const deprecate = {
|
|
|
|
setHandler: (handler) => { deprecationHandler = handler },
|
|
|
|
getHandler: () => deprecationHandler,
|
|
|
|
warn: (oldName, newName) => {
|
|
|
|
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
|
|
|
|
},
|
|
|
|
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
|
|
|
|
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`)
|
2018-09-11 18:16:49 +00:00
|
|
|
return emitter.on(newName, function (...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
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2018-11-19 17:56:26 +00:00
|
|
|
promisify: (promise, cb) => {
|
2018-11-28 04:50:53 +00:00
|
|
|
const oldName = `function with callbacks`
|
|
|
|
const newName = `function with Promises`
|
2018-11-19 17:56:26 +00:00
|
|
|
const warn = warnOnce(oldName, newName)
|
|
|
|
|
|
|
|
if (typeof cb !== 'function') return promise
|
2018-11-26 21:00:46 +00:00
|
|
|
if (process.enablePromiseAPIs) warn()
|
2018-11-19 17:56:26 +00:00
|
|
|
return promise
|
|
|
|
.then(res => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
cb(null, res)
|
|
|
|
})
|
|
|
|
}, err => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
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()
|
2018-09-12 22:13:22 +00:00
|
|
|
o[newName] = o[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
|
|
|
|
2016-03-25 19:50:33 +00:00
|
|
|
module.exports = deprecate
|