diff --git a/types/node/index.d.ts b/types/node/index.d.ts index b2e310cffa..fe2086cf82 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Node.js 9.4.x +// Type definitions for Node.js 9.6.x // Project: http://nodejs.org/ // Definitions by: Microsoft TypeScript // DefinitelyTyped @@ -6080,6 +6080,23 @@ declare module "async_hooks" { */ export function createHook(options: HookCallbacks): AsyncHook; + export interface AsyncResourceOptions { + /** + * The ID of the execution context that created this async event. + * Default: `executionAsyncId()` + */ + triggerAsyncId?: number; + + /** + * Disables automatic `emitDestroy` when the object is garbage collected. + * This usually does not need to be set (even if `emitDestroy` is called + * manually), unless the resource's `asyncId` is retrieved and the + * sensitive API's `emitDestroy` is called with it. + * Default: `false` + */ + requireManualDestroy?: boolean; + } + /** * The class AsyncResource was designed to be extended by the embedder's async resources. * Using this users can easily trigger the lifetime events of their own resources. @@ -6089,21 +6106,38 @@ declare module "async_hooks" { * AsyncResource() is meant to be extended. Instantiating a * new AsyncResource() also triggers init. If triggerAsyncId is omitted then * async_hook.executionAsyncId() is used. - * @param type the name of this async resource type - * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created + * @param type The type of async event. + * @param triggerAsyncId The ID of the execution context that created + * this async event (default: `executionAsyncId()`), or an + * AsyncResourceOptions object (since 9.3) */ - constructor(type: string, triggerAsyncId?: number) + constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions); /** * Call AsyncHooks before callbacks. + * @deprecated since 9.6 - Use asyncResource.runInAsyncScope() instead. */ emitBefore(): void; /** - * Call AsyncHooks after callbacks + * Call AsyncHooks after callbacks. + * @deprecated since 9.6 - Use asyncResource.runInAsyncScope() instead. */ emitAfter(): void; + /** + * Call the provided function with the provided arguments in the + * execution context of the async resource. This will establish the + * context, trigger the AsyncHooks before callbacks, call the function, + * trigger the AsyncHooks after callbacks, and then restore the original + * execution context. + * @param fn The function to call in the execution context of this + * async resource. + * @param thisArg The receiver to be used for the function call. + * @param args Optional arguments to pass to the function. + */ + runInAsyncScope(fn: (this: This, ...args: any[]) => Result, thisArg?: This, ...args: any[]): Result; + /** * Call AsyncHooks destroy callbacks. */ diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 1eb6539b09..08678990a8 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -3333,13 +3333,23 @@ namespace async_hooks_tests { const tId: number = this.triggerAsyncId(); } run() { - this.emitBefore(); - this.emitAfter(); + this.runInAsyncScope(() => {}); + this.runInAsyncScope(Array.prototype.find, [], () => true); } destroy() { this.emitDestroy(); } } + + // check AsyncResource constructor options. + new async_hooks.AsyncResource(''); + new async_hooks.AsyncResource('', 0); + new async_hooks.AsyncResource('', {}); + new async_hooks.AsyncResource('', { triggerAsyncId: 0 }); + new async_hooks.AsyncResource('', { + triggerAsyncId: 0, + requireManualDestroy: true + }); } //////////////////////////////////////////////////// diff --git a/types/node/v8/index.d.ts b/types/node/v8/index.d.ts index f28c0959a4..7d8455b828 100644 --- a/types/node/v8/index.d.ts +++ b/types/node/v8/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Node.js 8.9.x +// Type definitions for Node.js 8.10.x // Project: http://nodejs.org/ // Definitions by: Microsoft TypeScript // DefinitelyTyped @@ -6052,6 +6052,23 @@ declare module "async_hooks" { */ export function createHook(options: HookCallbacks): AsyncHook; + export interface AsyncResourceOptions { + /** + * The ID of the execution context that created this async event. + * Default: `executionAsyncId()` + */ + triggerAsyncId?: number; + + /** + * Disables automatic `emitDestroy` when the object is garbage collected. + * This usually does not need to be set (even if `emitDestroy` is called + * manually), unless the resource's `asyncId` is retrieved and the + * sensitive API's `emitDestroy` is called with it. + * Default: `false` + */ + requireManualDestroy?: boolean; + } + /** * The class AsyncResource was designed to be extended by the embedder's async resources. * Using this users can easily trigger the lifetime events of their own resources. @@ -6061,10 +6078,12 @@ declare module "async_hooks" { * AsyncResource() is meant to be extended. Instantiating a * new AsyncResource() also triggers init. If triggerAsyncId is omitted then * async_hook.executionAsyncId() is used. - * @param type the name of this async resource type - * @param triggerAsyncId the unique ID of the async resource in whose execution context this async resource was created + * @param type The type of async event. + * @param triggerAsyncId The ID of the execution context that created + * this async event (default: `executionAsyncId()`), or an + * AsyncResourceOptions object (since 8.10) */ - constructor(type: string, triggerAsyncId?: number) + constructor(type: string, triggerAsyncId?: number|AsyncResourceOptions); /** * Call AsyncHooks before callbacks. diff --git a/types/node/v8/node-tests.ts b/types/node/v8/node-tests.ts index 74d4c5c222..6292629915 100644 --- a/types/node/v8/node-tests.ts +++ b/types/node/v8/node-tests.ts @@ -3304,6 +3304,16 @@ namespace async_hooks_tests { this.emitDestroy(); } } + + // check AsyncResource constructor options. + new async_hooks.AsyncResource(''); + new async_hooks.AsyncResource('', 0); + new async_hooks.AsyncResource('', {}); + new async_hooks.AsyncResource('', { triggerAsyncId: 0 }); + new async_hooks.AsyncResource('', { + triggerAsyncId: 0, + requireManualDestroy: true + }); } ////////////////////////////////////////////////////