Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Latest commit

 

History

History
203 lines (131 loc) · 4.29 KB

compositedisposable.md

File metadata and controls

203 lines (131 loc) · 4.29 KB

CompositeDisposable class

Represents a group of disposable resources that are disposed together that can be added to and removed from.

Usage

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

CompositeDisposable Constructor

CompositeDisposable Instance Methods

CompositeDisposable Instance Properties

CompositeDisposable Constructor

CompositeDisposable(...args)

Initializes a new instance of the CompositeDisposable class from a group of disposables.

Arguments

  1. args: Array|arguments - Disposables that will be disposed together.

Example

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

CompositeDisposable Instance Methods

CompositeDisposable.prototype.add(item)

Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.

Arguments

  1. item Disposable: Disposable to add.

Example

const disposables = new CompositeDisposable();

const d1 = Disposable.create(() => console.log('one'));

disposables.add(d1);

disposables.dispose();
// => one

CompositeDisposable.prototype.clear()

Disposes all disposables in the group and removes them from the group but does not dispose the CompositeDisposable.

Example

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

CompositeDisposable.prototype.dispose()

Disposes all disposables in the group and removes them from the group.

Example

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

CompositeDisposable.prototype.remove(item)

Removes and disposes the first occurrence of a disposable from the CompositeDisposable.

Arguments

  1. item Disposable: Disposable to remove.

Returns

Boolean: true if the disposable was found and disposed; otherwise, false.

Example

const disposables = new CompositeDisposable();

const d1 = Disposable.create(function () {
     console.log('one');
});

disposables.add(d1);

console.log(disposables.remove(d1));
// => true

CompositeDisposable Instance Properties

isDisposed

#

Gets a value that indicates whether the object is disposed.

Example

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

length

#

Gets the number of disposables in the CompositeDisposable.

Example

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