Composite Disposable

An object that aggregates multiple Disposable instances together into a single disposable, so they can all be disposed as a group.

These are very useful when subscribing to multiple events.

import { CompositeDisposable } from 'event-kit'

class Something {
  constructor() {
    this.disposables = new CompositeDisposable();
    const db = inkdrop.main.dataStore.getLocalDB();

    this.disposables.add(db.onChange(() => {}));
    this.disposables.add(db.onNoteChange(() => {}));
  }

  destroy() {
    this.disposables.dispose();
  }
}

constructor()

Construction

Construct an instance, optionally with one or more disposables.

Example

const subscriptions = new CompositeDisposable();

dispose()

Destruction

Dispose all disposables added to this composite disposable. If this object has already been disposed, this method has no effect.

Example

const subscriptions = new CompositeDisposable();

// Later, dispose all
subscriptions.dispose();

add(...disposables)

Add multiple Disposables

Add disposables to be disposed when the composite is disposed. If this object has already been disposed, this method has no effect.

Parameters

  • Name
    disposables
    Type
    DisposableLike[]
    Required
    Description

    Disposable instances or any objects with .dispose()

Example

const subscriptions = new CompositeDisposable()

const db = inkdrop.main.dataStore.getLocalDB();
subscriptions.add(db.onChange(() => {}));
subscriptions.add(db.onNoteChange(() => {}));

remove(disposable)

Remove a Disposable

Remove a previously added disposable.

Parameters

  • Name
    disposable
    Type
    DisposableLike
    Required
    Description

    Disposable instances or any objects with .dispose()

Example

const subscriptions = new CompositeDisposable()

const db = inkdrop.main.dataStore.getLocalDB();
const disposable = db.onNoteChange(() => {});
subscriptions.add(disposable);

// Later, remove the disposable
subscriptions.remove(disposable);

clear()

Clear all disposables

Clear all disposables. They will not be disposed by the next call to dispose.

Example

const subscriptions = new CompositeDisposable()

// ...

subscriptions.clear();
Can you help us improve the docs? 🙏

The source of these docs is here on GitHub. If you see a way these docs can be improved, please fork us!