Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

async_hooks.d.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Async Hooks module: https://nodejs.org/api/async_hooks.html
  3. */
  4. declare module "async_hooks" {
  5. /**
  6. * Returns the asyncId of the current execution context.
  7. */
  8. function executionAsyncId(): number;
  9. /**
  10. * Returns the ID of the resource responsible for calling the callback that is currently being executed.
  11. */
  12. function triggerAsyncId(): number;
  13. interface HookCallbacks {
  14. /**
  15. * Called when a class is constructed that has the possibility to emit an asynchronous event.
  16. * @param asyncId a unique ID for the async resource
  17. * @param type the type of the async resource
  18. * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created
  19. * @param resource reference to the resource representing the async operation, needs to be released during destroy
  20. */
  21. init?(asyncId: number, type: string, triggerAsyncId: number, resource: Object): void;
  22. /**
  23. * When an asynchronous operation is initiated or completes a callback is called to notify the user.
  24. * The before callback is called just before said callback is executed.
  25. * @param asyncId the unique identifier assigned to the resource about to execute the callback.
  26. */
  27. before?(asyncId: number): void;
  28. /**
  29. * Called immediately after the callback specified in before is completed.
  30. * @param asyncId the unique identifier assigned to the resource which has executed the callback.
  31. */
  32. after?(asyncId: number): void;
  33. /**
  34. * Called when a promise has resolve() called. This may not be in the same execution id
  35. * as the promise itself.
  36. * @param asyncId the unique id for the promise that was resolve()d.
  37. */
  38. promiseResolve?(asyncId: number): void;
  39. /**
  40. * Called after the resource corresponding to asyncId is destroyed
  41. * @param asyncId a unique ID for the async resource
  42. */
  43. destroy?(asyncId: number): void;
  44. }
  45. interface AsyncHook {
  46. /**
  47. * Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.
  48. */
  49. enable(): this;
  50. /**
  51. * Disable the callbacks for a given AsyncHook instance from the global pool of AsyncHook callbacks to be executed. Once a hook has been disabled it will not be called again until enabled.
  52. */
  53. disable(): this;
  54. }
  55. /**
  56. * Registers functions to be called for different lifetime events of each async operation.
  57. * @param options the callbacks to register
  58. * @return an AsyncHooks instance used for disabling and enabling hooks
  59. */
  60. function createHook(options: HookCallbacks): AsyncHook;
  61. interface AsyncResourceOptions {
  62. /**
  63. * The ID of the execution context that created this async event.
  64. * Default: `executionAsyncId()`
  65. */
  66. triggerAsyncId?: number;
  67. /**
  68. * Disables automatic `emitDestroy` when the object is garbage collected.
  69. * This usually does not need to be set (even if `emitDestroy` is called
  70. * manually), unless the resource's `asyncId` is retrieved and the
  71. * sensitive API's `emitDestroy` is called with it.
  72. * Default: `false`
  73. */
  74. requireManualDestroy?: boolean;
  75. }
  76. /**
  77. * The class AsyncResource was designed to be extended by the embedder's async resources.
  78. * Using this users can easily trigger the lifetime events of their own resources.
  79. */
  80. class AsyncResource {
  81. /**
  82. * AsyncResource() is meant to be extended. Instantiating a
  83. * new AsyncResource() also triggers init. If triggerAsyncId is omitted then
  84. * async_hook.executionAsyncId() is used.
  85. * @param type The type of async event.
  86. * @param triggerAsyncId The ID of the execution context that created
  87. * this async event (default: `executionAsyncId()`), or an
  88. * AsyncResourceOptions object (since 9.3)
  89. */
  90. constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions);
  91. /**
  92. * Call AsyncHooks before callbacks.
  93. * @deprecated since 9.6 - Use asyncResource.runInAsyncScope() instead.
  94. */
  95. emitBefore(): void;
  96. /**
  97. * Call AsyncHooks after callbacks.
  98. * @deprecated since 9.6 - Use asyncResource.runInAsyncScope() instead.
  99. */
  100. emitAfter(): void;
  101. /**
  102. * Call the provided function with the provided arguments in the
  103. * execution context of the async resource. This will establish the
  104. * context, trigger the AsyncHooks before callbacks, call the function,
  105. * trigger the AsyncHooks after callbacks, and then restore the original
  106. * execution context.
  107. * @param fn The function to call in the execution context of this
  108. * async resource.
  109. * @param thisArg The receiver to be used for the function call.
  110. * @param args Optional arguments to pass to the function.
  111. */
  112. runInAsyncScope<This, Result>(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result;
  113. /**
  114. * Call AsyncHooks destroy callbacks.
  115. */
  116. emitDestroy(): void;
  117. /**
  118. * @return the unique ID assigned to this AsyncResource instance.
  119. */
  120. asyncId(): number;
  121. /**
  122. * @return the trigger ID for this AsyncResource instance.
  123. */
  124. triggerAsyncId(): number;
  125. }
  126. }