deprecate.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. let deprecationHandler: ElectronInternal.DeprecationHandler | null = null;
  2. function warnOnce (oldName: string, newName?: string) {
  3. let warned = false;
  4. const msg = newName
  5. ? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
  6. : `'${oldName}' is deprecated and will be removed.`;
  7. return () => {
  8. if (!warned && !process.noDeprecation) {
  9. warned = true;
  10. deprecate.log(msg);
  11. }
  12. };
  13. }
  14. const deprecate: ElectronInternal.DeprecationUtil = {
  15. warnOnce,
  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. // remove a function with no replacement
  35. removeFunction: (fn, removedName) => {
  36. if (!fn) { throw Error(`'${removedName} function' is invalid or does not exist.`); }
  37. // wrap the deprecated function to warn user
  38. const warn = warnOnce(`${fn.name} function`);
  39. return function (this: any) {
  40. warn();
  41. fn.apply(this, arguments);
  42. };
  43. },
  44. // change the name of a function
  45. renameFunction: (fn, newName) => {
  46. const warn = warnOnce(`${fn.name} function`, `${newName} function`);
  47. return function (this: any) {
  48. warn();
  49. return fn.apply(this, arguments);
  50. };
  51. },
  52. moveAPI: (fn: Function, oldUsage: string, newUsage: string) => {
  53. const warn = warnOnce(oldUsage, newUsage);
  54. return function (this: any) {
  55. warn();
  56. return fn.apply(this, arguments);
  57. };
  58. },
  59. // change the name of an event
  60. event: (emitter, oldName, newName) => {
  61. const warn = newName.startsWith('-') /* internal event */
  62. ? warnOnce(`${oldName} event`)
  63. : warnOnce(`${oldName} event`, `${newName} event`);
  64. return emitter.on(newName, function (this: NodeJS.EventEmitter, ...args) {
  65. if (this.listenerCount(oldName) !== 0) {
  66. warn();
  67. this.emit(oldName, ...args);
  68. }
  69. });
  70. },
  71. // deprecate a getter/setter function pair in favor of a property
  72. fnToProperty: (prototype: any, prop: string, getter: string, setter?: string) => {
  73. const withWarnOnce = function (obj: any, key: any, oldName: string, newName: string) {
  74. const warn = warnOnce(oldName, newName);
  75. const method = obj[key];
  76. return function (this: any, ...args: any) {
  77. warn();
  78. return method.apply(this, args);
  79. };
  80. };
  81. prototype[getter.substr(1)] = withWarnOnce(prototype, getter, `${getter.substr(1)} function`, `${prop} property`);
  82. if (setter) {
  83. prototype[setter.substr(1)] = withWarnOnce(prototype, setter, `${setter.substr(1)} function`, `${prop} property`);
  84. }
  85. },
  86. // remove a property with no replacement
  87. removeProperty: (o, removedName) => {
  88. // if the property's already been removed, warn about it
  89. if (!(removedName in o)) {
  90. deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`);
  91. }
  92. // wrap the deprecated property in an accessor to warn
  93. const warn = warnOnce(removedName);
  94. let val = o[removedName];
  95. return Object.defineProperty(o, removedName, {
  96. configurable: true,
  97. get: () => {
  98. warn();
  99. return val;
  100. },
  101. set: newVal => {
  102. warn();
  103. val = newVal;
  104. }
  105. });
  106. },
  107. // deprecate a callback-based function in favor of one returning a Promise
  108. promisify: <T extends (...args: any[]) => any>(fn: T): T => {
  109. const fnName = fn.name || 'function';
  110. const oldName = `${fnName} with callbacks`;
  111. const newName = `${fnName} with Promises`;
  112. const warn = warnOnce(oldName, newName);
  113. return function (this: any, ...params: any[]) {
  114. let cb: Function | undefined;
  115. if (params.length > 0 && typeof params[params.length - 1] === 'function') {
  116. cb = params.pop();
  117. }
  118. const promise = fn.apply(this, params);
  119. if (!cb) return promise;
  120. if (process.enablePromiseAPIs) warn();
  121. return promise
  122. .then((res: any) => {
  123. process.nextTick(() => {
  124. cb!.length === 2 ? cb!(null, res) : cb!(res);
  125. });
  126. return res;
  127. }, (err: Error) => {
  128. process.nextTick(() => {
  129. cb!.length === 2 ? cb!(err) : cb!();
  130. });
  131. throw err;
  132. });
  133. } as T;
  134. },
  135. // convertPromiseValue: Temporarily disabled until it's used
  136. // deprecate a callback-based function in favor of one returning a Promise
  137. promisifyMultiArg: <T extends (...args: any[]) => any>(fn: T /* convertPromiseValue: (v: any) => any */): T => {
  138. const fnName = fn.name || 'function';
  139. const oldName = `${fnName} with callbacks`;
  140. const newName = `${fnName} with Promises`;
  141. const warn = warnOnce(oldName, newName);
  142. return function (this: any, ...params) {
  143. let cb: Function | undefined;
  144. if (params.length > 0 && typeof params[params.length - 1] === 'function') {
  145. cb = params.pop();
  146. }
  147. const promise = fn.apply(this, params);
  148. if (!cb) return promise;
  149. if (process.enablePromiseAPIs) warn();
  150. return promise
  151. .then((res: any) => {
  152. process.nextTick(() => {
  153. // eslint-disable-next-line standard/no-callback-literal
  154. cb!.length > 2 ? cb!(null, ...res) : cb!(...res);
  155. });
  156. }, (err: Error) => {
  157. process.nextTick(() => cb!(err));
  158. });
  159. } as T;
  160. },
  161. // change the name of a property
  162. renameProperty: (o, oldName, newName) => {
  163. const warn = warnOnce(oldName, newName);
  164. // if the new property isn't there yet,
  165. // inject it and warn about it
  166. if ((oldName in o) && !(newName in o)) {
  167. warn();
  168. o[newName] = (o as any)[oldName];
  169. }
  170. // wrap the deprecated property in an accessor to warn
  171. // and redirect to the new property
  172. return Object.defineProperty(o, oldName, {
  173. get: () => {
  174. warn();
  175. return o[newName];
  176. },
  177. set: value => {
  178. warn();
  179. o[newName] = value;
  180. }
  181. });
  182. }
  183. };
  184. export default deprecate;