rx-handler-decorator
A decorator for making an RxJS 5 Subject into a handler for user events
Installation
npm i -S rx-handler-decorator
Usage
By decorating any property that returns a Subject
(or BehaviorSubject
, et al)
with @rxHandler
, it converts that property into a handler function, that also has all of the
methods of the original subject reference.
This can be used in any framework or environment that might require this behavior.
It also automatically binds the handler function to the reference, so there's no
need to use something like .bind()
or core-decorators
autobind on the resulting
handler method.
Angular 2 Basic Example
import { Component } from 'angular/core';import { rxHandler } from 'rx-handler-decorator';import * as Rx from 'rxjs'; @Component({ selector: 'my-app', template: `<div> <button (click)="clicks()">Click Me</button> <div>count: {{count | async}}</div> </div>`})class MyComponent { @rxHandler clicks = new Rx.Subject(); get count() { return this.clicks .scan(x => x + 1, 0); }}
React.js Basic Example
;;; Component { superprops; thisstate = count: 0 ; } { thissub = thisclicks ; } { thissub; } // convert the `clicks` property to a function-subject! @rxHandler clicks = ; { // now we can use `{this.clicks}` directly as a handler return <div> <button onClick=thisclicks>Click Me</button> <div>count: thisstatecount</div> </div>; }