How to debug rxjs5?

前端 未结 2 1439
礼貌的吻别
礼貌的吻别 2020-12-30 02:44

On RxJS - Goals I read that their goal is better debuggability:

Goals

Provide more debuggable call stacks than preceding versions of RxJS

相关标签:
2条回答
  • 2020-12-30 03:15
    import Rx, { Observable } from 'rxjs'
    
    const arrStream$ = Observable.of(1,2,3)
                        .do(x=>console.log('Before',x))  // 1, 2, 3
                        .map(x=>x*2)
                        .do(x=>console.log('After',x))   // 2, 4, 6
                        .subscribe(value=>doThingsWith(value))
    // real console output
    // Before 1
    // After  2
    // doThingsWith(2)
    // Before 2
    // After  4
    // doThingsWith(4)
    // Before 3
    // After  6
    // doThingsWith(6)
    

    .do(debugValue=> console.log(debugValue))

    0 讨论(0)
  • 2020-12-30 03:16

    It certainly is possible to debug RxJS code. I think it's probably safe to say hardly anyone would use it if that wasn't the case--Angular2 is heavily built on it too.

    The most common ways people use are the same ways they debug other JavaScript, breakpoints (e.g. debugger) and console.log()'s

    There are more advanced techniques some users use like drawing dependency graphs or marble diagrams. André Staltz wrote about this recently, so that might be a helpful resource.

    Ultimately, any kind of async programming is going to be harder to debug. This is not unique to redux-observable/RxJS; a quick search will reveal plenty of debugging concerns for redux-saga too.

    It turns out that redux-thunk is the best solution for a vast majority of applications built because a majority of them do not have complex side effect concerns that justify something like redux-observable or redux-saga. Though if you are already proficient in RxJS, there's nothing wrong with using redux-observable.

    redux-saga as a project has existed longer than redux-observable so that's certainly one major selling point. You'll find more documentation, examples, and likely are have a better community to get support from.

    The counter being that the operators and APIs you learn in redux-saga aren't nearly as transferable as learning RxJS, which is used all over the place. redux-observable is super super super simple internally, it's really just giving you a natural way for you to use RxJS. So if you know RxJS (or want to), it's an extremely natural fit.

    My advice at the moment for most people is that if you have to ask which one you should use, you probably should choose redux-saga.

    (disclaimer: I am one of the maintainers of redux-observable and RxJS v5)

    0 讨论(0)
提交回复
热议问题