简体   繁体   English

如何使用angular 4调试rxjs takeWhile命令?

[英]How to debug rxjs takeWhile command using angular 4?

I am using rxjs takeWhile method in my typescript to run a poll(periodic requests) while a condition is true. 我在条件为真时在打字稿中使用rxjs takeWhile方法运行轮询(定期请求)。 But the problem is that the poll is running continuously even after the condition is False. 但是问题在于,即使条件为False,轮询仍将连续运行。 I tried to debug it using the chrome developer tools by keeping the breakpoints, but unfortunately the breakpoint is not working for takeWhile method. 我尝试使用chrome开发人员工具通过保留断点来对其进行调试,但不幸的是,该断点不适用于takeWhile方法。 How do I print statements inside a takeWhile method or to put a breakpoint inside takeWhile method. 如何在takeWhile方法中打印语句或如何在takeWhile方法中放置断点。

Observable.interval(10000)
  .switchMap(() => http.get('/route1'))
  .map((data) => data)
  .takeWhile(() => this.obj.var === 'show_files')
  .subscribe((data: any) => {})

I am setting this "this.obj.var" to something else when I load another page to stop the polling. 当我加载另一个页面以停止轮询时,我将此“ this.obj.var”设置为其他内容。 I just want to poll if the user is in current page and stop polling when user navigated to any other page just to avoid overload on browser with conrinouos requests. 我只想轮询用户是否在当前页面中,并在用户导航到任何其他页面时停止轮询,以免浏览器出现conrinouos请求而导致过载。

Could you help me on how to debug rxjs methods using chrome developer tools 您能帮助我如何使用chrome开发人员工具调试rxjs方法吗

You can use do operator to log your result 您可以使用do运算符记录您的结果

Observable.interval(10000)
.switchMap(() => http.get('/route1')).map((data) =>data)
.do(value=>console.log(value))
.takeWhile(() => this.obj.var === 'show_files').subscribe((data: any) => {})

You can update the code of arrow-function that you pass to takeWhile() operator: 您可以更新传递给takeWhile()运算符的箭头函数的代码:

Observable.interval(10000)
  .switchMap(() => http.get('/route1'))
  .map((data) => data)
  .takeWhile(() => {
     // You can put a breakpoint on next row via chrome developer tools
     const condition = this.obj.var === 'show_files';
     console.log(condition);
     return condition;
   })
  .subscribe((data: any) => {})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM