widjet-disposables
Lightweight disposable pattern implementation.
Installation
npm install --save widjet-disposables
Usage
Disposable
The Disposable
class is the most basic class of the package. It takes a function as the sole argument when created. This function will be called when the dispose
method of the disposable is called.
{ // subscribe to some event // ... // return a new disposable that will perform the unsubscription return { // unsubscribe from the events }}
CompositeDisposable
The CompositeDisposable
class is a Disposable
that composes other disposables. It can be created with an array of disposables.
// creates a composite with an array of existing disposablesconst composite = disposables const disposable = {} // adds a new disposable in the compositecomposite // removes the added disposable from the compositecomposite
DisposableEvent
The DisposableEvent
is a specific disposable aimed to ease the registration of event listener. It handles either objects with addEventListener/removeEventListener
methods or objects with on/off
methods.
// creating a disposable event automatically registers the event listenerconst subscription = source 'event' {} // disposing the subscription automatically unregisters the event listenersubscription