UNPKG

1.58 kBTypeScriptView Raw
1// Type definitions for continuation-local-storage 3.2
2// Project: https://github.com/othiym23/node-continuation-local-storage
3// Definitions by: Jang-Ho Hwang <https://github.com/rath>
4// Kei Son <https://github.com/heycalmdown>
5// Dmitry Kudryavtsev <https://github.com/skwee357>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.1
8
9/// <reference types="node" />
10
11export type Context = {
12 [key: string]: any
13};
14
15export type BindCallbackFn<T> = (...args: any[]) => T;
16export type RunCallbackFn<T> = (context: Context) => T;
17
18export interface Namespace {
19 readonly name: string; // Note: this is readonly because changing it does not actually rename it
20
21 readonly active: Context; // Note: this is readonly because changing it manually will break functionality
22 createContext(): Context;
23
24 set<T>(key: string, value: T): T;
25 get<T>(key: string): T | undefined;
26
27 run<T = void>(callback: RunCallbackFn<T>): Context;
28 runAndReturn<T>(callback: RunCallbackFn<T>): T;
29
30 bind<T = void>(callback: BindCallbackFn<T>, context?: Context): BindCallbackFn<T>;
31 bindEmitter(emitter: NodeJS.EventEmitter): void;
32
33 enter(context: Context): void;
34 exit(context: Context): void;
35}
36
37export function createNamespace(name: string): Namespace;
38export function getNamespace(name: string): Namespace | undefined;
39export function destroyNamespace(name: string): void;
40export function reset(): void;
41
42// declare namespace process {
43// var namespaces: ContinuationLocalStorage.Namespace[];
44// }