MCPcopy Create free account
hub / github.com/gaearon/disposables

github.com/gaearon/disposables @v1.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.1 ↗ · + Follow
18 symbols 35 edges 9 files ⚖ Apache-2.0 6 documented · 33% updated 8y agov1.0.1 · 2015-04-09★ 621 open issues

Browse by type

Functions 12 Types & classes 6
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

disposables

Disposables let you safely compose resource disposal semantics. Think DOM nodes, event handlers, socket connections.

This tiny package includes several disposables:

This implementation of disposables is extracted from RxJS. I took the liberty to tweak the code style to my liking and provide this as a standalone package.

The API is mostly the same as RxJS except stricter in a few places. It does not strive for 100% API compatibility with RxJS, but general disposable behavior should match.

It's best if you consult the source and tests, as classes are small and few.

Usage

Importing

import { Disposable, CompositeDisposable, SerialDisposable } from 'disposables';

// or you can import just the ones you need to keep it even tinier
// import SerialDisposable from 'disposables/modules/SerialDisposable';

function attachHandlers(node) {
    let someHandler = ...;
    node.addEventHandler(someHandler);

    // use Disposable to guarantee single execution
    return new Disposable(() => {
      node.removeEventHandler(someHandler);
    });
}

// CompositeDisposable lets you compose several disposables...
let nodes = ...;
let compositeDisp = new CompositeDisposable(nodes.map(attachHandlers));

// and more later...
let moreNodes = ...
moreNodes.map(attachHandlers).forEach(d => compositeDisp.add(d));

// and dispose them at once!
function goodbye() {
    compositeDisp.dispose();
}

// ... or replace with a bunch of new ones ...
let serialDisp = new SerialDisposable();
serialDisp.setDisposable(compositeDisp);

function replaceNodes(newNodes) {
    let nextCompositeDisp = newNodes.map(attachHandlers);

    // release all the previous disposables:
    serialDisp.setDisposable(nextCompositeDisp);
}

// with a guarantee of each dispose() called only once.

Why Use This Over RxJS

Really, there are no other reasons.

License

Like the original RxJS code, it is licened under Apache 2.0.

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 10
Class 6
Function 2

Languages

TypeScript100%

Modules by API surface

src/SerialDisposable.js6 symbols
src/CompositeDisposable.js6 symbols
src/Disposable.js5 symbols
src/isDisposable.js1 symbols

For agents

$ claude mcp add disposables \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page