deprecate.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict'
  2. let deprecationHandler = null
  3. function warnOnce (oldName, newName) {
  4. let warned = false
  5. const msg = newName
  6. ? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
  7. : `'${oldName}' is deprecated and will be removed.`
  8. return () => {
  9. if (!warned && !process.noDeprecation) {
  10. warned = true
  11. deprecate.log(msg)
  12. }
  13. }
  14. }
  15. const deprecate = {
  16. setHandler: (handler) => { deprecationHandler = handler },
  17. getHandler: () => deprecationHandler,
  18. warn: (oldName, newName) => {
  19. if (!process.noDeprecation) {
  20. deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
  21. }
  22. },
  23. log: (message) => {
  24. if (typeof deprecationHandler === 'function') {
  25. deprecationHandler(message)
  26. } else if (process.throwDeprecation) {
  27. throw new Error(message)
  28. } else if (process.traceDeprecation) {
  29. return console.trace(message)
  30. } else {
  31. return console.warn(`(electron) ${message}`)
  32. }
  33. },
  34. function: (fn, newName) => {
  35. const warn = warnOnce(fn.name, newName)
  36. return function () {
  37. warn()
  38. fn.apply(this, arguments)
  39. }
  40. },
  41. event: (emitter, oldName, newName) => {
  42. const warn = newName.startsWith('-') /* internal event */
  43. ? warnOnce(`${oldName} event`)
  44. : warnOnce(`${oldName} event`, `${newName} event`)
  45. return emitter.on(newName, function (...args) {
  46. if (this.listenerCount(oldName) !== 0) {
  47. warn()
  48. this.emit(oldName, ...args)
  49. }
  50. })
  51. },
  52. removeProperty: (o, removedName) => {
  53. // if the property's already been removed, warn about it
  54. if (!(removedName in o)) {
  55. deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`)
  56. }
  57. // wrap the deprecated property in an accessor to warn
  58. const warn = warnOnce(removedName)
  59. let val = o[removedName]
  60. return Object.defineProperty(o, removedName, {
  61. configurable: true,
  62. get: () => {
  63. warn()
  64. return val
  65. },
  66. set: newVal => {
  67. warn()
  68. val = newVal
  69. }
  70. })
  71. },
  72. promisify: (fn) => {
  73. const fnName = fn.name || 'function'
  74. const oldName = `${fnName} with callbacks`
  75. const newName = `${fnName} with Promises`
  76. const warn = warnOnce(oldName, newName)
  77. return function (...params) {
  78. let cb
  79. if (params.length > 0 && typeof params[params.length - 1] === 'function') {
  80. cb = params.pop()
  81. }
  82. const promise = fn.apply(this, params)
  83. if (!cb) return promise
  84. if (process.enablePromiseAPIs) warn()
  85. return promise
  86. .then(res => {
  87. process.nextTick(() => {
  88. cb.length === 2 ? cb(null, res) : cb(res)
  89. })
  90. }, err => {
  91. process.nextTick(() => {
  92. cb.length === 2 ? cb(err) : cb()
  93. })
  94. })
  95. }
  96. },
  97. renameProperty: (o, oldName, newName) => {
  98. const warn = warnOnce(oldName, newName)
  99. // if the new property isn't there yet,
  100. // inject it and warn about it
  101. if ((oldName in o) && !(newName in o)) {
  102. warn()
  103. o[newName] = o[oldName]
  104. }
  105. // wrap the deprecated property in an accessor to warn
  106. // and redirect to the new property
  107. return Object.defineProperty(o, oldName, {
  108. get: () => {
  109. warn()
  110. return o[newName]
  111. },
  112. set: value => {
  113. warn()
  114. o[newName] = value
  115. }
  116. })
  117. }
  118. }
  119. module.exports = deprecate