Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # NOT ACTIVELY MAINTAINED
  2. >**This project works fine but is not actively maintained.**
  3. >**For the new code, you might want to try the new official [rx.disposables](https://github.com/Reactive-Extensions/rx.disposables) package instead.**
  4. # disposables [![npm package](https://img.shields.io/npm/v/disposables.svg?style=flat-square)](https://www.npmjs.org/package/disposables)
  5. Disposables let you safely compose resource disposal semantics.
  6. Think DOM nodes, event handlers, socket connections.
  7. **This implementation of disposables is extracted from [RxJS](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/disposables).**
  8. I took the liberty to tweak the code style to my liking and provide this as a standalone package.
  9. This tiny package includes several disposables:
  10. * [`Disposable`](https://github.com/gaearon/disposables/blob/master/src/Disposable.js) ensures its `dispose` action runs only once;
  11. * [`CompositeDisposable`](https://github.com/gaearon/disposables/blob/master/src/CompositeDisposable.js) ensures a group of disposables are disposed together;
  12. * [`SerialDisposable`](https://github.com/gaearon/disposables/blob/master/src/SerialDisposable.js) switches underlying disposables on the fly and disposes them.
  13. The API is *mostly* the same as RxJS except stricter in a few places.
  14. It does not strive for 100% API compatibility with RxJS, but generally behavior is the same.
  15. It's best if you consult the [source](https://github.com/gaearon/disposables/tree/master/src/) and [tests](https://github.com/gaearon/disposables/tree/master/src/__tests__), as classes are small and few.
  16. ### Usage
  17. ```js
  18. import { Disposable, CompositeDisposable, SerialDisposable } from 'disposables';
  19. // or you can import just the ones you need to keep it even tinier
  20. // import SerialDisposable from 'disposables/modules/SerialDisposable';
  21. function attachHandlers(node) {
  22. let someHandler = ...;
  23. node.addEventHandler(someHandler);
  24. // use Disposable to guarantee single execution
  25. return new Disposable(() => {
  26. node.removeEventHandler(someHandler);
  27. });
  28. }
  29. // CompositeDisposable lets you compose several disposables...
  30. let nodes = ...;
  31. let compositeDisp = new CompositeDisposable(nodes.map(attachHandlers));
  32. // and more later...
  33. let moreNodes = ...
  34. moreNodes.map(attachHandlers).forEach(d => compositeDisp.add(d));
  35. // and dispose them at once!
  36. function goodbye() {
  37. compositeDisp.dispose();
  38. }
  39. // ... or replace with a bunch of new ones ...
  40. let serialDisp = new SerialDisposable();
  41. serialDisp.setDisposable(compositeDisp);
  42. function replaceNodes(newNodes) {
  43. let nextCompositeDisp = new CompositeDisposable(newNodes.map(attachHandlers));
  44. // release all the previous disposables:
  45. serialDisp.setDisposable(nextCompositeDisp);
  46. }
  47. // with a guarantee of each dispose() called only once.
  48. ```
  49. ### License
  50. Like the original RxJS code, it is licensed under Apache 2.0.