electron/atom/common/api/lib/deprecate.coffee

70 lines
2 KiB
CoffeeScript
Raw Normal View History

2016-01-12 02:03:02 +00:00
### Deprecate a method. ###
2015-11-09 10:09:22 +00:00
deprecate = (oldName, newName, fn) ->
warned = false
->
unless warned or process.noDeprecation
warned = true
deprecate.warn oldName, newName
fn.apply this, arguments
2016-01-12 02:03:02 +00:00
### The method is renamed. ###
2015-11-09 10:09:22 +00:00
deprecate.rename = (object, oldName, newName) ->
warned = false
newMethod = ->
unless warned or process.noDeprecation
warned = true
deprecate.warn oldName, newName
this[newName].apply this, arguments
if typeof object is 'function'
object.prototype[oldName] = newMethod
else
object[oldName] = newMethod
2016-01-12 02:03:02 +00:00
### Forward the method to member. ###
2015-11-09 10:09:22 +00:00
deprecate.member = (object, method, member) ->
warned = false
object.prototype[method] = ->
unless warned or process.noDeprecation
warned = true
deprecate.warn method, "#{member}.#{method}"
this[member][method].apply this[member], arguments
2016-01-12 02:03:02 +00:00
### Deprecate a property. ###
2015-11-09 10:09:22 +00:00
deprecate.property = (object, property, method) ->
Object.defineProperty object, property,
get: ->
warned = false
unless warned or process.noDeprecation
warned = true
deprecate.warn "#{property} property", "#{method} method"
this[method]()
2016-01-12 02:03:02 +00:00
### Deprecate an event. ###
2015-11-09 10:09:22 +00:00
deprecate.event = (emitter, oldName, newName, fn) ->
warned = false
emitter.on newName, (args...) ->
2016-01-12 02:03:02 +00:00
### there is listeners for old API. ###
if @listenerCount(oldName) > 0
2015-11-09 10:09:22 +00:00
unless warned or process.noDeprecation
warned = true
deprecate.warn "'#{oldName}' event", "'#{newName}' event"
if fn?
fn.apply this, arguments
else
@emit oldName, args...
2015-11-09 10:09:22 +00:00
2016-01-12 02:03:02 +00:00
### Print deprecation warning. ###
2015-11-09 10:09:22 +00:00
deprecate.warn = (oldName, newName) ->
2015-12-16 00:46:53 +00:00
deprecate.log "#{oldName} is deprecated. Use #{newName} instead."
2016-01-12 02:03:02 +00:00
### Print deprecation message. ###
deprecate.log = (message) ->
2015-11-09 10:09:22 +00:00
if process.throwDeprecation
throw new Error(message)
else if process.traceDeprecation
console.trace message
else
console.warn "(electron) #{message}"
module.exports = deprecate