deprecate.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Deprecate a method.
  2. const deprecate = function (oldName, newName, fn) {
  3. var warned
  4. warned = false
  5. return function () {
  6. if (!(warned || process.noDeprecation)) {
  7. warned = true
  8. deprecate.warn(oldName, newName)
  9. }
  10. return fn.apply(this, arguments)
  11. }
  12. }
  13. // The method is renamed.
  14. deprecate.rename = function (object, oldName, newName) {
  15. var newMethod, warned
  16. warned = false
  17. newMethod = function () {
  18. if (!(warned || process.noDeprecation)) {
  19. warned = true
  20. deprecate.warn(oldName, newName)
  21. }
  22. return this[newName].apply(this, arguments)
  23. }
  24. if (typeof object === 'function') {
  25. object.prototype[oldName] = newMethod
  26. } else {
  27. object[oldName] = newMethod
  28. }
  29. }
  30. // Forward the method to member.
  31. deprecate.member = function (object, method, member) {
  32. var warned
  33. warned = false
  34. object.prototype[method] = function () {
  35. if (!(warned || process.noDeprecation)) {
  36. warned = true
  37. deprecate.warn(method, member + '.' + method)
  38. }
  39. return this[member][method].apply(this[member], arguments)
  40. }
  41. }
  42. // Deprecate a property.
  43. deprecate.property = function (object, property, method) {
  44. return Object.defineProperty(object, property, {
  45. get: function () {
  46. var warned
  47. warned = false
  48. if (!(warned || process.noDeprecation)) {
  49. warned = true
  50. deprecate.warn(property + ' property', method + ' method')
  51. }
  52. return this[method]()
  53. }
  54. })
  55. }
  56. // Deprecate an event.
  57. deprecate.event = function (emitter, oldName, newName, fn) {
  58. var warned = false
  59. return emitter.on(newName, function (...args) {
  60. // there is listeners for old API.
  61. if (this.listenerCount(oldName) > 0) {
  62. if (!(warned || process.noDeprecation)) {
  63. warned = true
  64. deprecate.warn("'" + oldName + "' event", "'" + newName + "' event")
  65. }
  66. if (fn != null) {
  67. fn.apply(this, arguments)
  68. } else {
  69. this.emit.apply(this, [oldName].concat(args))
  70. }
  71. }
  72. })
  73. }
  74. // Print deprecation warning.
  75. deprecate.warn = function (oldName, newName) {
  76. return deprecate.log(oldName + ' is deprecated. Use ' + newName + ' instead.')
  77. }
  78. var deprecationHandler = null
  79. // Print deprecation message.
  80. deprecate.log = function (message) {
  81. if (typeof deprecationHandler === 'function') {
  82. deprecationHandler(message)
  83. } else if (process.throwDeprecation) {
  84. throw new Error(message)
  85. } else if (process.traceDeprecation) {
  86. return console.trace(message)
  87. } else {
  88. return console.warn('(electron) ' + message)
  89. }
  90. }
  91. deprecate.setHandler = function (handler) {
  92. deprecationHandler = handler
  93. }
  94. deprecate.getHandler = function () {
  95. return deprecationHandler
  96. }
  97. module.exports = deprecate