From d7f5d8fa9009f7ddf4fce473ffb2662d7010d905 Mon Sep 17 00:00:00 2001 From: Jarrad Whitaker Date: Tue, 17 Oct 2017 07:42:29 +1100 Subject: [PATCH] @types/node: Asynchooks promiseResolve and AsyncResource (#20540) * add promiseResolve hook * add AsyncResource * AsyncResource jsdoc fixes * test remaining AsyncResource methods --- types/node/index.d.ts | 47 ++++++++++++++++++++++++++++++++++++++++ types/node/node-tests.ts | 24 +++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 91514bbd53..6189d0b486 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -5940,6 +5940,13 @@ declare module "async_hooks" { */ after?(asyncId: number): void; + /** + * Called when a promise has resolve() called. This may not be in the same execution id + * as the promise itself. + * @param asyncId the unique id for the promise that was resolve()d. + */ + promiseResolve?(asyncId: number): void; + /** * Called after the resource corresponding to asyncId is destroyed * @param asyncId a unique ID for the async resource @@ -5965,6 +5972,46 @@ declare module "async_hooks" { * @return an AsyncHooks instance used for disabling and enabling hooks */ export function createHook(options: HookCallbacks): AsyncHook; + + /** + * 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. + */ + export class AsyncResource { + /** + * 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 + */ + constructor(type: string, triggerAsyncId?: number) + + /** + * Call AsyncHooks before callbacks. + */ + emitBefore(): void; + + /** + * Call AsyncHooks after callbacks + */ + emitAfter(): void; + + /** + * Call AsyncHooks destroy callbacks. + */ + emitDestroy(): void; + + /** + * @return the unique ID assigned to this AsyncResource instance. + */ + asyncId(): number; + + /** + * @return the trigger ID for this AsyncResource instance. + */ + triggerAsyncId(): number; + } } declare module "http2" { diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index d9f77c51bb..0e2823ac1c 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -3012,7 +3012,8 @@ namespace async_hooks_tests { init: (asyncId: number, type: string, triggerAsyncId: number, resource: object) => void {}, before: (asyncId: number) => void {}, after: (asyncId: number) => void {}, - destroy: (asyncId: number) => void {} + destroy: (asyncId: number) => void {}, + promiseResolve: (asyncId: number) => void {} }; const asyncHook = async_hooks.createHook(hooks); @@ -3021,6 +3022,27 @@ namespace async_hooks_tests { const tId: number = async_hooks.triggerAsyncId(); const eId: number = async_hooks.executionAsyncId(); + + class TestResource extends async_hooks.AsyncResource { + constructor() { + super('TEST_RESOURCE'); + } + } + + class AnotherTestResource extends async_hooks.AsyncResource { + constructor() { + super('TEST_RESOURCE', 42); + const aId: number = this.asyncId(); + const tId: number = this.triggerAsyncId(); + } + run() { + this.emitBefore(); + this.emitAfter(); + } + destroy() { + this.emitDestroy(); + } + } } ////////////////////////////////////////////////////