Rx.CompositeDisposable
class
Represents a group of disposable resources that are disposed together.
Usage
The follow example shows the basic usage of an Rx.CompositeDisposable.
var d1 = Rx.Disposable.create(() => console.log('one'));
var d2 = Rx.Disposable.create(() => console.log('two'));
// Initialize with two disposables
var disposables = new Rx.CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
Location
- rx.js
CompositeDisposable Constructor
CompositeDisposable Instance Methods
CompositeDisposable Instance Properties
CompositeDisposable Constructor
Rx.CompositeDisposable(...args)
Initializes a new instance of the Rx.CompositeDisposable
class from a group of disposables.
参数
args
(Array|arguments): Disposables that will be disposed together.
例
var d1 = Rx.Disposable.create(() => console.log('one'));
var d2 = Rx.Disposable.create(() => console.log('two'));
// Initialize with two disposables
var disposables = new Rx.CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
Location
- rx.js
CompositeDisposable Instance Methods
Rx.CompositeDisposable.prototype.add(item)
Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
参数
item
(Disposable): Disposable to add.
例
var disposables = new Rx.CompositeDisposable();
var d1 = Rx.Disposable.create(() => console.log('one'));
disposables.add(d1);
disposables.dispose();
// => one
Location
- rx.js
Rx.CompositeDisposable.prototype.clear()
Removes and disposes all disposables from the CompositeDisposable, but does not dispose the CompositeDisposable.
例
var d1 = Rx.Disposable.create(() => console.log('one'));
var disposables = new Rx.CompositeDisposable(d1);
console.log(disposables.length);
// => 1
disposables.clear();
// => one
console.log(disposables.length);
// => 0
Location
- rx.js
Rx.CompositeDisposable.prototype.contains(item)
Determines whether the CompositeDisposable contains a specific disposable.
参数
item
(Disposable): Disposable to search for.
返回值
(Boolean): true
if the disposable was found; otherwise, false
.
例
var disposables = new Rx.CompositeDisposable();
var d1 = Rx.Disposable.create(() => console.log('one'));
disposables.add(d1);
console.log(disposables.contains(d1));
// => true
Location
- rx.js
Rx.CompositeDisposable.prototype.dispose()
Disposes all disposables in the group and removes them from the group.
例
var d1 = Rx.Disposable.create(() => console.log('one'));
var d2 = Rx.Disposable.create(() => console.log('two'));
var disposables = new Rx.CompositeDisposable(d1, d2);
disposables.dispose();
// => one
// => two
console.log(disposables.length);
// => 0
Location
- rx.js
Rx.CompositeDisposable.prototype.remove(item)
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
.
例
var disposables = new Rx.CompositeDisposable();
var d1 = Rx.Disposable.create(() => console.log('one'));
disposables.add(d1);
console.log(disposables.remove(d1));
// => true
Location
- rx.js
Rx.CompositeDisposable.prototype.rxcompositedisposableprototypetoarray()
Converts the existing CompositeDisposable to an array of disposables
返回值
(Array): An array of disposable objects.
例
var d1 = Rx.Disposable.create(() => console.log('one'));
var d2 = Rx.Disposable.create(() => console.log('two'));
var disposables = new Rx.CompositeDisposable(d1, d2);
var array = disposables.toArray();
console.log(array.length);
// => 2
Location
- rx.js
CompositeDisposable Instance Properties
isDisposed
Gets a value that indicates whether the object is disposed.
例
var disposables = new Rx.CompositeDisposable();
var d1 = Rx.Disposable.create(() => console.log('disposed'));
disposables.add(d1);
console.log(disposables.isDisposed);
// => false
disposables.dispose();
// => disposed
console.log(disposables.isDisposed);
// => true
Location
- rx.js
length
Gets the number of disposables in the CompositeDisposable.
例
var disposables = new Rx.CompositeDisposable();
var d1 = Rx.Disposable.create(() => console.log('disposed'));
disposables.add(d1);
console.log(disposables.length);
// => 1
disposables.dispose();
// => disposed
console.log(disposables.length);
// => 0
Location
- rx.js