forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesnext.disposable.d.ts
167 lines (157 loc) · 5.75 KB
/
esnext.disposable.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// <reference lib="es2015.symbol" />
interface SymbolConstructor {
/**
* A method that is used to release resources held by an object. Called by the semantics of the `using` statement.
*/
readonly dispose: unique symbol;
/**
* A method that is used to asynchronously release resources held by an object. Called by the semantics of the `await using` statement.
*/
readonly asyncDispose: unique symbol;
}
interface Disposable {
[Symbol.dispose](): void;
}
interface AsyncDisposable {
[Symbol.asyncDispose](): PromiseLike<void>;
}
interface SuppressedError extends Error {
error: any;
suppressed: any;
}
interface SuppressedErrorConstructor {
new (error: any, suppressed: any, message?: string): SuppressedError;
(error: any, suppressed: any, message?: string): SuppressedError;
readonly prototype: SuppressedError;
}
declare var SuppressedError: SuppressedErrorConstructor;
interface DisposableStack {
/**
* Returns a value indicating whether this stack has been disposed.
*/
readonly disposed: boolean;
/**
* Disposes each resource in the stack in the reverse order that they were added.
*/
dispose(): void;
/**
* Adds a disposable resource to the stack, returning the resource.
* @param value The resource to add. `null` and `undefined` will not be added, but will be returned.
* @returns The provided {@link value}.
*/
use<T extends Disposable | null | undefined>(value: T): T;
/**
* Adds a value and associated disposal callback as a resource to the stack.
* @param value The value to add.
* @param onDispose The callback to use in place of a `[Symbol.dispose]()` method. Will be invoked with `value`
* as the first parameter.
* @returns The provided {@link value}.
*/
adopt<T>(value: T, onDispose: (value: T) => void): T;
/**
* Adds a callback to be invoked when the stack is disposed.
*/
defer(onDispose: () => void): void;
/**
* Move all resources out of this stack and into a new `DisposableStack`, and marks this stack as disposed.
* @example
* ```ts
* class C {
* #res1: Disposable;
* #res2: Disposable;
* #disposables: DisposableStack;
* constructor() {
* // stack will be disposed when exiting constructor for any reason
* using stack = new DisposableStack();
*
* // get first resource
* this.#res1 = stack.use(getResource1());
*
* // get second resource. If this fails, both `stack` and `#res1` will be disposed.
* this.#res2 = stack.use(getResource2());
*
* // all operations succeeded, move resources out of `stack` so that they aren't disposed
* // when constructor exits
* this.#disposables = stack.move();
* }
*
* [Symbol.dispose]() {
* this.#disposables.dispose();
* }
* }
* ```
*/
move(): DisposableStack;
[Symbol.dispose](): void;
readonly [Symbol.toStringTag]: string;
}
interface DisposableStackConstructor {
new (): DisposableStack;
readonly prototype: DisposableStack;
}
declare var DisposableStack: DisposableStackConstructor;
interface AsyncDisposableStack {
/**
* Returns a value indicating whether this stack has been disposed.
*/
readonly disposed: boolean;
/**
* Disposes each resource in the stack in the reverse order that they were added.
*/
disposeAsync(): Promise<void>;
/**
* Adds a disposable resource to the stack, returning the resource.
* @param value The resource to add. `null` and `undefined` will not be added, but will be returned.
* @returns The provided {@link value}.
*/
use<T extends AsyncDisposable | Disposable | null | undefined>(value: T): T;
/**
* Adds a value and associated disposal callback as a resource to the stack.
* @param value The value to add.
* @param onDisposeAsync The callback to use in place of a `[Symbol.asyncDispose]()` method. Will be invoked with `value`
* as the first parameter.
* @returns The provided {@link value}.
*/
adopt<T>(value: T, onDisposeAsync: (value: T) => PromiseLike<void> | void): T;
/**
* Adds a callback to be invoked when the stack is disposed.
*/
defer(onDisposeAsync: () => PromiseLike<void> | void): void;
/**
* Move all resources out of this stack and into a new `DisposableStack`, and marks this stack as disposed.
* @example
* ```ts
* class C {
* #res1: Disposable;
* #res2: Disposable;
* #disposables: DisposableStack;
* constructor() {
* // stack will be disposed when exiting constructor for any reason
* using stack = new DisposableStack();
*
* // get first resource
* this.#res1 = stack.use(getResource1());
*
* // get second resource. If this fails, both `stack` and `#res1` will be disposed.
* this.#res2 = stack.use(getResource2());
*
* // all operations succeeded, move resources out of `stack` so that they aren't disposed
* // when constructor exits
* this.#disposables = stack.move();
* }
*
* [Symbol.dispose]() {
* this.#disposables.dispose();
* }
* }
* ```
*/
move(): AsyncDisposableStack;
[Symbol.asyncDispose](): Promise<void>;
readonly [Symbol.toStringTag]: string;
}
interface AsyncDisposableStackConstructor {
new (): AsyncDisposableStack;
readonly prototype: AsyncDisposableStack;
}
declare var AsyncDisposableStack: AsyncDisposableStackConstructor;