RxJS (5.0rc4):暂停和恢复间隔计时器 [英] RxJS (5.0rc4): Pause and resume an interval timer

查看:61
本文介绍了RxJS (5.0rc4):暂停和恢复间隔计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Rx 来保存动画时钟.每个动画帧,它将间隔刻度映射到该刻度的新值.

I am using Rx to keep an animation clock. Every animation frame, it maps an interval tick to the new values for that tick.

假设我想暂停动画.最自然的方法是以某种方式暂停时钟 rx,然后在稍后恢复它.

Suppose I want to pause the animation. The most natural way would be to somehow suspend the clock rx and then resume it at a later time.

取消订阅然后重新订阅不是一个自然的选择,因为这个动画时钟是一个冷可观察对象.我不想在它们恢复时重新启动动画.如果我寻求一种变通方法,我将不得不生成一个新的简历 rx,这会使所有公开的 API 变得非常复杂.

Unsubscribe then resubscribe isn't a natural fit, because this animation clock is a cold observable. I don't want to restart the animation when they resume. If I go for a workaround method, I will have to generate a new resume rx, vastly complicating all of the exposed APIs.

背压方法似乎没有希望:

Backpressure methods don't seem promising:

pause 不起作用,因为我想从中断的地方继续,而不是向前跳.换句话说,我不想在它关闭时丢弃滴答声.

pause doesn't work, because I want to resume where I left off, not jump forward. In other words I don't want to drop ticks while it's off.

pausableBuffered 不起作用,因为在恢复时,它会尽可能快地耗尽所有累积的滴答声.

pausableBuffered doesn't work, because on resume, it will drain all of the accumulated ticks as fast as it can.

使用某种虚拟时间调度程序完全停止时间然后恢复正常时间可能是可能的(?)

Using some sort of a virtual time scheduler to completely stop time and then resume normal time might be possible(?)

我使用的是 RxJS 5.0rc4,但我也不知道如何在 RxJS 4 上执行此操作.任何版本的任何建议将不胜感激.

I am on RxJS 5.0rc4, but I wouldn't know how to do this on RxJS 4, either. Any advice for either version would be appreciated.

推荐答案

pauser 流上使用 switchMap 在原始源和 Observable.never 之间进行选择.如果您不想让计时器向前跳,请自行管理(使用下面的 x 变量).

Use switchMap on a pauser stream to choose between the original source and Observable.never. If you don't want the timer to jump ahead, then manage it yourself (using the x variable in the below).

function pausableInterval(ms, pauser) {
  let x = 0;
  const source = IntervalObservable.create(ms);

  return pauser.switchMap(paused => paused ? Observable.never() : source.map(() => x++);
}

pauser 流应该发出布尔值.

未测试.

这篇关于RxJS (5.0rc4):暂停和恢复间隔计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆