electron/lib/common/api/deprecate.js

132 lines
3.7 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
// Deprecate an event.
deprecate.event = (emitter, oldName, newName) => {
let warned = false
return emitter.on(newName, function (...args) {
// There are no listeners for this event
if (this.listenerCount(oldName) === 0) { return }
// noDeprecation set or if user has already been warned
if (warned || process.noDeprecation) { return }
warned = true
const isInternalEvent = newName.startsWith('-')
if (isInternalEvent) {
// The event cannot be use anymore. Log that.
deprecate.log(`'${oldName}' event has been deprecated.`)
} else {
// The event has a new name now. Warn with that.
deprecate.warn(`'${oldName}' event`, `'${newName}' event`)
}
this.emit(oldName, ...args)
})
}
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
// Commented out until such time as it is needed
2017-11-20 14:12:34 +00:00
// // 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)
// }
// }
2018-05-29 13:40:19 +00:00
// Remove a property with no replacement
deprecate.removeProperty = (object, deprecatedName) => {
if (!process.noDeprecation) {
deprecate.log(`The '${deprecatedName}' property of ${object} has been deprecated and marked for removal.`)
}
}
// Remove a function with no replacement
deprecate.removeFunction = (deprecatedName) => {
if (!process.noDeprecation) {
deprecate.log(`The '${deprecatedName}' function has been deprecated and marked for removal.`)
}
}
// Replace the old name of a property
deprecate.renameProperty = (object, deprecatedName, newName) => {
let warned = false
let warn = () => {
if (!(warned || process.noDeprecation)) {
warned = true
deprecate.warn(deprecatedName, newName)
}
}
if ((typeof object[newName] === 'undefined') &&
(typeof object[deprecatedName] !== 'undefined')) {
warn()
object[newName] = object[deprecatedName]
}
2018-05-29 13:40:19 +00:00
return Object.defineProperty(object, deprecatedName, {
get: function () {
warn()
2018-05-29 13:40:19 +00:00
return this[newName]
},
set: function (value) {
warn()
2018-05-29 13:40:19 +00:00
this[newName] = value
}
})
}
2017-11-20 14:12:34 +00:00
2016-03-25 19:50:33 +00:00
module.exports = deprecate