Decision No. 1
Theme Usage: Fiddle
const state = new Rx.Subject() .debounceTime(1000) .scan((acc) => { return ++acc }, 0).do(::console.log) const handler = (e) => { state.next(e) } state.startWith(0).subscribe((clicks) => { ReactDOM.render(<button onClick={handler}>Clicked {clicks}</button>, document.querySelector('#app')) })
Decision number 2
Using rxjs fromEvent: Fiddle
// Intial render so element exists in dom (there is probably a better pattern) ReactDOM.render( <button id='clickMe'>Click Me</button>, document.querySelector('#app')) const clicks = Rx.Observable .fromEvent(document.getElementById('clickMe'), 'click') .do(::console.log) .debounceTime(1000) .scan((acc) => { return ++acc }, 0) clicks.subscribe((clicks) => { ReactDOM.render( <button id='clickMe'>Click Me {clicks}</button>, document.querySelector('#app')) })
Decision No. 3
Note: very experimental, and just something that I tried to do for fun.
This is more for action-based architecture where you have actions that change your state (thread). This is a handler that is fully autonomous. It is used with the custom operator 'fromEventArgs': Fiddle (look at the console)
const handler = (e) => { Rx.Observable .fromEventArgs(e, 'UniqueKey') .debounceTime(1000) .subscribe(x => console.log('Send an action', x)) }
source share