mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 20:40:20 +00:00
@types/node: Asynchooks promiseResolve and AsyncResource (#20540)
* add promiseResolve hook * add AsyncResource * AsyncResource jsdoc fixes * test remaining AsyncResource methods
This commit is contained in:
Vendored
+47
@@ -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" {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user