@types/node: Asynchooks promiseResolve and AsyncResource (#20540)

* add promiseResolve hook

* add AsyncResource

* AsyncResource jsdoc fixes

* test remaining AsyncResource methods
This commit is contained in:
Jarrad Whitaker
2017-10-16 13:42:29 -07:00
committed by Andy
parent 173d1d4e9e
commit d7f5d8fa90
2 changed files with 70 additions and 1 deletions
+47
View File
@@ -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" {
+23 -1
View File
@@ -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();
}
}
}
////////////////////////////////////////////////////