Giter Site home page Giter Site logo

disposables's Introduction

NOT ACTIVELY MAINTAINED

This project works fine but is not actively maintained.
For the new code, you might want to try the new official rx.disposables package instead.

disposables npm package

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

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.

This tiny package includes several disposables:

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 generally behavior is the same.

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

Usage

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 = new CompositeDisposable(newNodes.map(attachHandlers));

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

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

License

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

disposables's People

Contributors

chrisjshull avatar gaearon avatar ianobermiller avatar jfmengels avatar petermoreira18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

disposables's Issues

404 in readme

Hey. Just FYI some of the links in the README return 404.

Fails to build on Node v4.2.2, npm v3.4.1 Windows 7

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'build' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle [email protected]~prebuild: [email protected]
6 silly lifecycle [email protected]~prebuild: no script for prebuild, continuing
7 info lifecycle [email protected]~build: [email protected]
8 verbose lifecycle [email protected]~build: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~build: PATH: C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;C:\Development\disposables-1.0.1\node_modules\.bin;%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\Perl64\site\bin;C:\Perl64\bin;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Sublime Text 3\;C:\Ruby22-x64\bin\;C:\Program Files\Perforce;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\nodejs\;C:\Program Files\Microsoft System Center 2012 R2\Service Manager\;C:\Program Files (x86)\Gource\cmd;C:\Program Files (x86)\Microsoft VS Code\bin;C:\Users\username\AppData\Local\atom\bin;.bin;C:\Users\username\AppData\Roaming\npm
10 verbose lifecycle [email protected]~build: CWD: C:\Development\disposables-1.0.1
11 silly lifecycle [email protected]~build: Args: [ '/d /s /c', './scripts/build' ]
12 silly lifecycle [email protected]~build: Returned: code: 1  signal: null
13 info lifecycle [email protected]~build: Failed to exec build script
14 verbose stack Error: [email protected] build: `./scripts/build`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:232:16)
14 verbose stack     at emitTwo (events.js:87:13)
14 verbose stack     at EventEmitter.emit (events.js:172:7)
14 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\spawn.js:24:14)
14 verbose stack     at emitTwo (events.js:87:13)
14 verbose stack     at ChildProcess.emit (events.js:172:7)
14 verbose stack     at maybeClose (internal/child_process.js:818:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
15 verbose pkgid [email protected]
16 verbose cwd C:\Development\disposables-1.0.1
17 error Windows_NT 6.1.7601
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
19 error node v4.2.2
20 error npm  v3.4.1
21 error code ELIFECYCLE
22 error [email protected] build: `./scripts/build`
22 error Exit status 1
23 error Failed at the [email protected] build script './scripts/build'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the disposables package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     ./scripts/build
23 error You can get their info via:
23 error     npm owner ls disposables
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.