Represents a group of disposable resources that are disposed together that can be added to and removed from.
The follow example shows the basic usage of a CompositeDisposable
.
const d1 = Disposable.create(() => console.log('one'));
const d2 = Disposable.create(() => console.log('two'));
// Initialize with two disposables
const disposables = new CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
Initializes a new instance of the CompositeDisposable
class from a group of disposables.
args
:Array|arguments
- Disposables that will be disposed together.
const d1 = Disposable.create(() => console.log('one'));
const d2 = Disposable.create(() => console.log('two'));
// Initialize with two disposables
const disposables = new CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
item
Disposable
: Disposable to add.
const disposables = new CompositeDisposable();
const d1 = Disposable.create(() => console.log('one'));
disposables.add(d1);
disposables.dispose();
// => one
Disposes all disposables in the group and removes them from the group but does not dispose the CompositeDisposable.
const d1 = Disposable.create(() => console.log('one'));
const d2 = Disposable.create(() => console.log('two'));
const disposables = new CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
console.log(disposables.isDisposed);
// => false
Disposes all disposables in the group and removes them from the group.
const d1 = Disposable.create(() => console.log('one'));
const d2 = Disposable.create(() => console.log('two'));
const disposables = new CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
console.log(disposables.length);
// => 0
Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
item
Disposable
: Disposable to remove.
Boolean
: true
if the disposable was found and disposed; otherwise, false
.
const disposables = new CompositeDisposable();
const d1 = Disposable.create(function () {
console.log('one');
});
disposables.add(d1);
console.log(disposables.remove(d1));
// => true
Gets a value that indicates whether the object is disposed.
const disposables = new CompositeDisposable();
const d1 = Disposable.create(() => console.log('disposed'));
disposables.add(d1);
console.log(disposables.isDisposed);
// => false
disposables.dispose();
// => disposed
console.log(disposables.isDisposed);
// => true
Gets the number of disposables in the CompositeDisposable.
const disposables = new CompositeDisposable();
const d1 = Disposable.create(() => console.log('disposed'));
disposables.add(d1);
console.log(disposables.length);
// => 1
disposables.dispose();
// => disposed
console.log(disposables.length);
// => 0