mapped-disposable
v1.0.3
Published
Map-flavoured alternative to Atom's CompositeDisposable class.
Maintainers
Readme
MappedDisposable
Map-flavoured alternative to Atom's CompositeDisposable class. Intended for
use in Atom package development, but compatible with any project that uses event-kit.
Usage
This class simplifies scenarios when multiple Disposable instances are grouped together
and referenced by name. The existing practice was to create multiple CompositeDisposable objects,
each bound to a different property or variable name:
let editorDisposables = new CompositeDisposable();
let projectDisposables = new CompositeDisposable();
let paneDisposables = new CompositeDisposable();With a MappedDisposable, disposable groups become easier to manage:
let disposables = new MappedDisposable();
disposables.set("editor", new CompositeDisposable());
disposables.set("project", new CompositeDisposable());
disposables.set("pane", new CompositeDisposable());
disposables.add("editor", editor.onDidChange(…));
disposables.add("project", project.onDidChangePaths(…));Entries can be disposed of individually or altogether:
disposables.dispose("editor"); // Dispose only editor-related disposables
disposables.dispose(); // Dispose of everythingA MappedDisposable operates just like an ordinary Map. Anything works as an
entry key (not just strings), and values can be queried using the has() and get()
methods that you're used to:
const packageObject = atom.packages.getActivePackage("file-icons");
disposables.add(packageObject, new Disposable(…));
disposables.get(packageObject); // => CompositeDisposable;API
Constructor
new MappedDisposable([iterable])
Create a new instance, optionally with a list of keys and disposables.
new MappedDisposable([ [key1, disp1], [key2, disp2] ]);| Parameter | Type | Default | Attributes |
| ---------------- | ------------------ | -------- | ---------------------------- |
| key | Any | None | Required |
| value | Disposable | None | Required |
Instance properties
disposed
Default: false
Type: Boolean
Whether the instance has been irrevocably disposed of.
size
Default: 0
Type: Number
Read-only
Number of entries (key/disposable pairs) stored in the instance.
