electron/lib/common/api/deprecate.js

109 lines
2.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) {
var warned
warned = false
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.
2016-03-25 19:50:33 +00:00
deprecate.rename = function (object, oldName, newName) {
var newMethod, warned
warned = false
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.
2016-03-25 19:50:33 +00:00
deprecate.member = function (object, method, member) {
var warned
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
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.
2016-03-25 19:50:33 +00:00
deprecate.property = function (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 () {
var warned
warned = false
2016-01-12 02:40:23 +00:00
if (!(warned || process.noDeprecation)) {
2016-03-25 19:50:33 +00:00
warned = true
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.
2016-03-25 19:50:33 +00:00
deprecate.event = function (emitter, oldName, newName, fn) {
var warned
warned = false
return emitter.on(newName, function (...args) {
2016-01-14 18:35:29 +00:00
// there is listeners for old API.
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
deprecate.warn("'" + oldName + "' event", "'" + newName + "' event")
2016-01-12 02:40:23 +00:00
}
if (fn != null) {
2016-03-25 19:50:33 +00:00
return fn.apply(this, arguments)
2016-01-12 02:40:23 +00:00
} else {
2016-03-25 19:50:33 +00:00
return 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
2016-01-14 18:35:29 +00:00
// Print deprecation warning.
2016-03-25 19:50:33 +00:00
deprecate.warn = function (oldName, newName) {
return deprecate.log(oldName + ' is deprecated. Use ' + newName + ' instead.')
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:50:33 +00:00
var deprecationHandler = null
2016-01-14 18:35:29 +00:00
// Print deprecation message.
2016-03-25 19:50:33 +00:00
deprecate.log = function (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 {
2016-03-25 19:50:33 +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
2016-03-25 19:50:33 +00:00
deprecate.setHandler = function (handler) {
deprecationHandler = handler
}
2016-03-25 19:50:33 +00:00
deprecate.getHandler = function () {
return deprecationHandler
}
2016-03-25 19:50:33 +00:00
module.exports = deprecate