electron/lib/common/api/deprecate.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-01-14 18:35:29 +00:00
// Deprecate a method.
2016-03-25 19:50:33 +00:00
const deprecate = function (oldName, newName, fn) {
2017-11-14 01:21:57 +00:00
let warned = false
2016-03-25 19:50:33 +00:00
return function () {
2016-01-12 02:40:23 +00:00
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
deprecate.warn(oldName, newName)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
return fn.apply(this, arguments)
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// The method is renamed.
2017-11-14 18:33:38 +00:00
deprecate.rename = (object, oldName, newName) => {
console.log('we are here')
2017-11-14 01:21:57 +00:00
let warned = false
const newMethod = function () {
2016-01-12 02:40:23 +00:00
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
deprecate.warn(oldName, newName)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
return this[newName].apply(this, arguments)
}
2016-01-12 02:40:23 +00:00
if (typeof object === 'function') {
2016-03-29 00:35:49 +00:00
object.prototype[oldName] = newMethod
2016-01-12 02:40:23 +00:00
} else {
2016-03-29 00:35:49 +00:00
object[oldName] = newMethod
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Forward the method to member.
2017-11-14 01:21:57 +00:00
deprecate.member = (object, method, member) => {
let warned = false
2016-03-29 00:35:49 +00:00
object.prototype[method] = function () {
2016-01-12 02:40:23 +00:00
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
2017-11-14 01:21:57 +00:00
deprecate.warn(method, `${member}.${method}`)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
return this[member][method].apply(this[member], arguments)
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Deprecate a property.
2017-11-14 01:21:57 +00:00
deprecate.property = (object, property, method) => {
2016-01-12 02:40:23 +00:00
return Object.defineProperty(object, property, {
2016-03-25 19:50:33 +00:00
get: function () {
2017-11-14 01:21:57 +00:00
let warned = false
2016-01-12 02:40:23 +00:00
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
2017-11-14 01:21:57 +00:00
deprecate.warn(`${property} property`, `${method} method`)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
return this[method]()
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
})
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Deprecate an event.
2017-11-14 01:21:57 +00:00
deprecate.event = (emitter, oldName, newName, fn) => {
let warned = false
2016-03-25 19:50:33 +00:00
return emitter.on(newName, function (...args) {
2016-01-12 02:40:23 +00:00
if (this.listenerCount(oldName) > 0) {
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
2017-11-14 01:21:57 +00:00
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
2016-01-12 02:40:23 +00:00
}
if (fn != null) {
2016-05-19 22:28:08 +00:00
fn.apply(this, arguments)
2016-01-12 02:40:23 +00:00
} else {
2016-05-19 22:28:08 +00:00
this.emit.apply(this, [oldName].concat(args))
2016-01-12 02:40:23 +00:00
}
}
2016-03-25 19:50:33 +00:00
})
}
2016-01-12 02:40:23 +00:00
2017-11-14 01:21:57 +00:00
deprecate.warn = (oldName, newName) => {
return deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
2016-03-25 19:50:33 +00:00
}
2016-01-12 02:40:23 +00:00
2017-11-14 01:21:57 +00:00
let deprecationHandler = null
2016-01-14 18:35:29 +00:00
// Print deprecation message.
2017-11-14 01:21:57 +00:00
deprecate.log = (message) => {
if (typeof deprecationHandler === 'function') {
2016-03-25 19:50:33 +00:00
deprecationHandler(message)
} else if (process.throwDeprecation) {
2016-03-25 19:50:33 +00:00
throw new Error(message)
2016-01-12 02:40:23 +00:00
} else if (process.traceDeprecation) {
2016-03-25 19:50:33 +00:00
return console.trace(message)
2016-01-12 02:40:23 +00:00
} else {
2017-11-14 01:21:57 +00:00
return console.warn(`(electron) ${message}`)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:50:33 +00:00
}
2016-01-12 02:40:23 +00:00
2017-11-14 01:21:57 +00:00
deprecate.setHandler = (handler) => {
2016-03-25 19:50:33 +00:00
deprecationHandler = handler
}
2017-11-14 01:21:57 +00:00
deprecate.getHandler = () => deprecationHandler
2016-03-25 19:50:33 +00:00
module.exports = deprecate