Represents a disposable resource which only allows a single assignment of its underlying disposable resource. If an underlying disposable resource has already been set, future attempts to set the underlying disposable resource will throw an Error.
The follow example shows the basic usage of a SingleAssignmentDisposable
.
const singleDisposable = new SingleAssignmentDisposable();
const disposable = Disposable.create(() => console.log('disposed'));
singleDisposable.setDisposable(disposable);
singleDisposable.dispose();
// => disposed
Initializes a new instance of the SingleAssignmentDisposable
class.
const singleDisposable = new SingleAssignmentDisposable();
console.log(singleDisposable.isDisposed);
// => false
Disposes the underlying disposable.
const singleDisposable = new SingleAssignmentDisposable();
const disposable = Disposable.create(() => console.log('disposed'));
singleDisposable.setDisposable(disposable);
console.log(singleDisposable.isDisposed);
// => false
singleDisposable.dispose();
// => disposed
console.log(singleDisposable.isDisposed);
// => true
Gets the underlying disposable. After disposal, the result of getting this method is undefined.
Disposable
- The underlying disposable.
const singleDisposable = new SingleAssignmentDisposable();
const disposable = Disposable.create(() => console.log('disposed'));
singleDisposable.setDisposable(disposable);
console.log(singleDisposable.getDisposable() === disposable);
Sets the underlying disposable.
value
:Disposable
: The new underlying disposable.
const singleDisposable = new SingleAssignmentDisposable();
const d1 = Disposable.create(() => console.log('one'));
singleDisposable.setDisposable(d1);
const d2 = Disposable.create(() => console.log('two'));
try {
singleDisposable.setDisposable(d2);
} catch (e) {
console.log(e.message);
}
// => Disposable has already been assigned
Gets a value that indicates whether the object is disposed.
const singleDisposable = new SingleAssignmentDisposable();
const disposable = Disposable.create(() => console.log('disposed'));
singleDisposable.setDisposable(disposable);
console.log(singleDisposable.isDisposed);
// => false
singleDisposable.dispose();
// => disposed
console.log(singleDisposable.isDisposed);
// => true