lib.esnext.disposable.d.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*! *****************************************************************************
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  4. this file except in compliance with the License. You may obtain a copy of the
  5. License at http://www.apache.org/licenses/LICENSE-2.0
  6. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  7. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  8. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  9. MERCHANTABLITY OR NON-INFRINGEMENT.
  10. See the Apache Version 2.0 License for specific language governing permissions
  11. and limitations under the License.
  12. ***************************************************************************** */
  13. /// <reference no-default-lib="true"/>
  14. /// <reference lib="es2015.symbol" />
  15. interface SymbolConstructor {
  16. /**
  17. * A method that is used to release resources held by an object. Called by the semantics of the `using` statement.
  18. */
  19. readonly dispose: unique symbol;
  20. /**
  21. * A method that is used to asynchronously release resources held by an object. Called by the semantics of the `await using` statement.
  22. */
  23. readonly asyncDispose: unique symbol;
  24. }
  25. interface Disposable {
  26. [Symbol.dispose](): void;
  27. }
  28. interface AsyncDisposable {
  29. [Symbol.asyncDispose](): PromiseLike<void>;
  30. }
  31. interface SuppressedError extends Error {
  32. error: any;
  33. suppressed: any;
  34. }
  35. interface SuppressedErrorConstructor {
  36. new (error: any, suppressed: any, message?: string): SuppressedError;
  37. (error: any, suppressed: any, message?: string): SuppressedError;
  38. readonly prototype: SuppressedError;
  39. }
  40. declare var SuppressedError: SuppressedErrorConstructor;
  41. interface DisposableStack {
  42. /**
  43. * Returns a value indicating whether this stack has been disposed.
  44. */
  45. readonly disposed: boolean;
  46. /**
  47. * Disposes each resource in the stack in the reverse order that they were added.
  48. */
  49. dispose(): void;
  50. /**
  51. * Adds a disposable resource to the stack, returning the resource.
  52. * @param value The resource to add. `null` and `undefined` will not be added, but will be returned.
  53. * @returns The provided {@link value}.
  54. */
  55. use<T extends Disposable | null | undefined>(value: T): T;
  56. /**
  57. * Adds a value and associated disposal callback as a resource to the stack.
  58. * @param value The value to add.
  59. * @param onDispose The callback to use in place of a `[Symbol.dispose]()` method. Will be invoked with `value`
  60. * as the first parameter.
  61. * @returns The provided {@link value}.
  62. */
  63. adopt<T>(value: T, onDispose: (value: T) => void): T;
  64. /**
  65. * Adds a callback to be invoked when the stack is disposed.
  66. */
  67. defer(onDispose: () => void): void;
  68. /**
  69. * Move all resources out of this stack and into a new `DisposableStack`, and marks this stack as disposed.
  70. * @example
  71. * ```ts
  72. * class C {
  73. * #res1: Disposable;
  74. * #res2: Disposable;
  75. * #disposables: DisposableStack;
  76. * constructor() {
  77. * // stack will be disposed when exiting constructor for any reason
  78. * using stack = new DisposableStack();
  79. *
  80. * // get first resource
  81. * this.#res1 = stack.use(getResource1());
  82. *
  83. * // get second resource. If this fails, both `stack` and `#res1` will be disposed.
  84. * this.#res2 = stack.use(getResource2());
  85. *
  86. * // all operations succeeded, move resources out of `stack` so that they aren't disposed
  87. * // when constructor exits
  88. * this.#disposables = stack.move();
  89. * }
  90. *
  91. * [Symbol.dispose]() {
  92. * this.#disposables.dispose();
  93. * }
  94. * }
  95. * ```
  96. */
  97. move(): DisposableStack;
  98. [Symbol.dispose](): void;
  99. readonly [Symbol.toStringTag]: string;
  100. }
  101. interface DisposableStackConstructor {
  102. new (): DisposableStack;
  103. readonly prototype: DisposableStack;
  104. }
  105. declare var DisposableStack: DisposableStackConstructor;
  106. interface AsyncDisposableStack {
  107. /**
  108. * Returns a value indicating whether this stack has been disposed.
  109. */
  110. readonly disposed: boolean;
  111. /**
  112. * Disposes each resource in the stack in the reverse order that they were added.
  113. */
  114. disposeAsync(): Promise<void>;
  115. /**
  116. * Adds a disposable resource to the stack, returning the resource.
  117. * @param value The resource to add. `null` and `undefined` will not be added, but will be returned.
  118. * @returns The provided {@link value}.
  119. */
  120. use<T extends AsyncDisposable | Disposable | null | undefined>(value: T): T;
  121. /**
  122. * Adds a value and associated disposal callback as a resource to the stack.
  123. * @param value The value to add.
  124. * @param onDisposeAsync The callback to use in place of a `[Symbol.asyncDispose]()` method. Will be invoked with `value`
  125. * as the first parameter.
  126. * @returns The provided {@link value}.
  127. */
  128. adopt<T>(value: T, onDisposeAsync: (value: T) => PromiseLike<void> | void): T;
  129. /**
  130. * Adds a callback to be invoked when the stack is disposed.
  131. */
  132. defer(onDisposeAsync: () => PromiseLike<void> | void): void;
  133. /**
  134. * Move all resources out of this stack and into a new `DisposableStack`, and marks this stack as disposed.
  135. * @example
  136. * ```ts
  137. * class C {
  138. * #res1: Disposable;
  139. * #res2: Disposable;
  140. * #disposables: DisposableStack;
  141. * constructor() {
  142. * // stack will be disposed when exiting constructor for any reason
  143. * using stack = new DisposableStack();
  144. *
  145. * // get first resource
  146. * this.#res1 = stack.use(getResource1());
  147. *
  148. * // get second resource. If this fails, both `stack` and `#res1` will be disposed.
  149. * this.#res2 = stack.use(getResource2());
  150. *
  151. * // all operations succeeded, move resources out of `stack` so that they aren't disposed
  152. * // when constructor exits
  153. * this.#disposables = stack.move();
  154. * }
  155. *
  156. * [Symbol.dispose]() {
  157. * this.#disposables.dispose();
  158. * }
  159. * }
  160. * ```
  161. */
  162. move(): AsyncDisposableStack;
  163. [Symbol.asyncDispose](): Promise<void>;
  164. readonly [Symbol.toStringTag]: string;
  165. }
  166. interface AsyncDisposableStackConstructor {
  167. new (): AsyncDisposableStack;
  168. readonly prototype: AsyncDisposableStack;
  169. }
  170. declare var AsyncDisposableStack: AsyncDisposableStackConstructor;