如何编写此 RxJs 代码,以便输出之间的延迟是动态的?
How do I write this RxJs code so that the delay between outputs is dynamic?
我正在写一个游戏,有一个事件队列。我已经使用 RxJs 修改了一个 Subject 以使其“速率受限”,即它每 2500 秒才输出一个新值:
this.q = new Subject();
this.q
.pipe(
concatMap(item => of(item).pipe(
concat(
of('ignored').pipe(
delay(2500),
ignoreElements()
)
)
))
)
.subscribe(evt => {
this.processEvent(evt);
});
网络代码在收到来自服务器的消息时调用 this.q.next(evt)
。
这适用于固定数量的静态速率限制。但我意识到有些事件在下一个事件之前应该有更短的延迟,特别是有些事件应该有更长的延迟,以便让 UI 动画某些东西或让用户消化信息。
有没有办法将该常量 2500
更改为从 item
中提取的值,例如 item.delay
?
谢谢!
您可能想尝试以下方法:
this.q
.pipe(
concatMap(item => of(item).pipe(delay(item.delay))))
)
)
.subscribe();
我正在写一个游戏,有一个事件队列。我已经使用 RxJs 修改了一个 Subject 以使其“速率受限”,即它每 2500 秒才输出一个新值:
this.q = new Subject();
this.q
.pipe(
concatMap(item => of(item).pipe(
concat(
of('ignored').pipe(
delay(2500),
ignoreElements()
)
)
))
)
.subscribe(evt => {
this.processEvent(evt);
});
网络代码在收到来自服务器的消息时调用 this.q.next(evt)
。
这适用于固定数量的静态速率限制。但我意识到有些事件在下一个事件之前应该有更短的延迟,特别是有些事件应该有更长的延迟,以便让 UI 动画某些东西或让用户消化信息。
有没有办法将该常量 2500
更改为从 item
中提取的值,例如 item.delay
?
谢谢!
您可能想尝试以下方法:
this.q
.pipe(
concatMap(item => of(item).pipe(delay(item.delay))))
)
)
.subscribe();