| 711 | * A map the manages the lifecycle of the values that it stores. |
| 712 | */ |
| 713 | export class DisposableMap<K, V extends IDisposable = IDisposable> implements IDisposable { |
| 714 | |
| 715 | private readonly _store = new Map<K, V>(); |
| 716 | private _isDisposed = false; |
| 717 | |
| 718 | constructor() { |
| 719 | trackDisposable(this); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Disposes of all stored values and mark this object as disposed. |
| 724 | * |
| 725 | * Trying to use this object after it has been disposed of is an error. |
| 726 | */ |
| 727 | dispose(): void { |
| 728 | markAsDisposed(this); |
| 729 | this._isDisposed = true; |
| 730 | this.clearAndDisposeAll(); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Disposes of all stored values and clear the map, but DO NOT mark this object as disposed. |
| 735 | */ |
| 736 | clearAndDisposeAll(): void { |
| 737 | if (!this._store.size) { |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | try { |
| 742 | dispose(this._store.values()); |
| 743 | } finally { |
| 744 | this._store.clear(); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | has(key: K): boolean { |
| 749 | return this._store.has(key); |
| 750 | } |
| 751 | |
| 752 | get size(): number { |
| 753 | return this._store.size; |
| 754 | } |
| 755 | |
| 756 | get(key: K): V | undefined { |
| 757 | return this._store.get(key); |
| 758 | } |
| 759 | |
| 760 | set(key: K, value: V, skipDisposeOnOverwrite = false): void { |
| 761 | if (this._isDisposed) { |
| 762 | console.warn(new Error('Trying to add a disposable to a DisposableMap that has already been disposed of. The added object will be leaked!').stack); |
| 763 | } |
| 764 | |
| 765 | if (!skipDisposeOnOverwrite) { |
| 766 | this._store.get(key)?.dispose(); |
| 767 | } |
| 768 | |
| 769 | this._store.set(key, value); |
| 770 | } |
nothing calls this directly
no outgoing calls
no test coverage detected