| 361 | * store that has already been disposed of. |
| 362 | */ |
| 363 | export class DisposableStore implements IDisposable { |
| 364 | |
| 365 | static DISABLE_DISPOSED_WARNING = false; |
| 366 | |
| 367 | private readonly _toDispose = new Set<IDisposable>(); |
| 368 | private _isDisposed = false; |
| 369 | |
| 370 | constructor() { |
| 371 | trackDisposable(this); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Dispose of all registered disposables and mark this object as disposed. |
| 376 | * |
| 377 | * Any future disposables added to this object will be disposed of on `add`. |
| 378 | */ |
| 379 | public dispose(): void { |
| 380 | if (this._isDisposed) { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | markAsDisposed(this); |
| 385 | this._isDisposed = true; |
| 386 | this.clear(); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @return `true` if this object has been disposed of. |
| 391 | */ |
| 392 | public get isDisposed(): boolean { |
| 393 | return this._isDisposed; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Dispose of all registered disposables but do not mark this object as disposed. |
| 398 | */ |
| 399 | public clear(): void { |
| 400 | if (this._toDispose.size === 0) { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | try { |
| 405 | dispose(this._toDispose); |
| 406 | } finally { |
| 407 | this._toDispose.clear(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Add a new {@link IDisposable disposable} to the collection. |
| 413 | */ |
| 414 | public add<T extends IDisposable>(o: T): T { |
| 415 | if (!o) { |
| 416 | return o; |
| 417 | } |
| 418 | if ((o as unknown as DisposableStore) === this) { |
| 419 | throw new Error('Cannot register a disposable on itself!'); |
| 420 | } |
nothing calls this directly
no outgoing calls
no test coverage detected