UNPKG

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