electron/lib/common/api/deprecate.js

103 lines
2.8 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
2017-11-20 14:12:34 +00:00
// The method is aliases and the old method is retained for backwards compat
2017-11-14 19:56:16 +00:00
deprecate.alias = function (object, deprecatedName, existingName) {
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
2017-11-14 19:56:16 +00:00
deprecate.warn(deprecatedName, existingName)
2016-01-12 02:40:23 +00:00
}
2017-11-14 19:56:16 +00:00
return this[existingName].apply(this, arguments)
2016-03-25 19:50:33 +00:00
}
2016-01-12 02:40:23 +00:00
if (typeof object === 'function') {
2017-11-14 19:56:16 +00:00
object.prototype[deprecatedName] = newMethod
2016-01-12 02:40:23 +00:00
} else {
2017-11-14 19:56:16 +00:00
object[deprecatedName] = 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
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
2017-11-20 14:12:34 +00:00
// None of the below methods are used, and so will be commented
// out until such time that they are needed to be used and tested.
// // Forward the method to member.
// deprecate.member = (object, method, member) => {
// let warned = false
// object.prototype[method] = function () {
// if (!(warned || process.noDeprecation)) {
// warned = true
// deprecate.warn(method, `${member}.${method}`)
// }
// return this[member][method].apply(this[member], arguments)
// }
// }
//
// // Deprecate a property.
// deprecate.property = (object, property, method) => {
// return Object.defineProperty(object, property, {
// get: function () {
// let warned = false
// if (!(warned || process.noDeprecation)) {
// warned = true
// deprecate.warn(`${property} property`, `${method} method`)
// }
// return this[method]()
// }
// })
// }
//
// // Deprecate an event.
// deprecate.event = (emitter, oldName, newName, fn) => {
// let warned = false
// return emitter.on(newName, function (...args) {
// if (this.listenerCount(oldName) > 0) {
// if (!(warned || process.noDeprecation)) {
// warned = true
// deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
// }
// if (fn != null) {
// fn.apply(this, arguments)
// } else {
// this.emit.apply(this, [oldName].concat(args))
// }
// }
// })
// }
2016-03-25 19:50:33 +00:00
module.exports = deprecate