mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
Merge from vscode c873727e8bac95e7cbf5b154a9e6ae0986f2ce18 (#6446)
This commit is contained in:
@@ -207,6 +207,43 @@ export class MutableDisposable<T extends IDisposable> implements IDisposable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper class that stores a disposable that is not currently "owned" by anyone.
|
||||
*
|
||||
* Example use cases:
|
||||
*
|
||||
* - Express that a function/method will take ownership of a disposable parameter.
|
||||
* - Express that a function returns a disposable that the caller must explicitly take ownership of.
|
||||
*/
|
||||
export class UnownedDisposable<T extends IDisposable> extends Disposable {
|
||||
private _hasBeenAcquired = false;
|
||||
private _value?: T;
|
||||
|
||||
public constructor(value: T) {
|
||||
super();
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
public acquire(): T {
|
||||
if (this._hasBeenAcquired) {
|
||||
throw new Error('This disposable has already been acquired');
|
||||
}
|
||||
this._hasBeenAcquired = true;
|
||||
const value = this._value!;
|
||||
this._value = undefined;
|
||||
return value;
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
super.dispose();
|
||||
if (!this._hasBeenAcquired) {
|
||||
this._hasBeenAcquired = true;
|
||||
this._value!.dispose();
|
||||
this._value = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface IReference<T> extends IDisposable {
|
||||
readonly object: T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user