【发布时间】:2019-02-24 11:35:41
【问题描述】:
我正在构建一个通知组件,它应该向用户显示通知。当一次创建多个通知时,它应该将它们排队。
现在它可以很好地显示第一个通知,但之后它会同时触发 2 个通知(请参阅下面的当前输出)。它不会等待上一个通知显示然后在显示下一个通知之前再次隐藏。
notifications.api.ts:
public notifications = new Subject<INotificationEvent>();
public notifications$ = this.notifications.asObservable();
notifications.component.ts:
private finished = new Subject();
constructor(private notifications: NotificationsApi) {}
zip(this.notificationsApi.notifications$, this.notificationsApi.notifications, (i, s) => s).pipe(
tap(() => {
if (!this.isActive) {
this.finished.next();
}
}),
delayWhen(() => this.finished),
delay(450),
tap((event: INotificationEvent) => {
this.notification = event;
this.isActive = true;
this.cd.markForCheck();
console.log(this.notification);
console.log('showing');
}),
delay(this.hideAfter),
tap(() => {
this.isActive = false;
this.cd.markForCheck();
console.log('closing');
}),
delay(450)
).subscribe(() => {
console.log('finishing');
this.finished.next();
});
app.component.ts:
let i = 0;
setInterval(() => {
this.notifications.newNotification({message: `${i}`, theme: 'primary'});
i++;
}, 2000);
电流输出:
{message: "0", theme: "primary"}
showing
closing
finishing
{message: "1", theme: "primary"}
showing
{message: "2", theme: "primary"}
showing
closing
finishing
{message: "3", theme: "primary"}
showing
{message: "4", theme: "primary"}
showing
closing
closing
finishing
finishing
{message: "5", theme: "primary"}
showing
{message: "6", theme: "primary"}
期望的输出:
{message: "0", theme: "primary"}
showing
closing
finishing
{message: "1", theme: "primary"}
showing
closing
finishing
{message: "2", theme: "primary"}
showing
closing
finishing
{message: "3", theme: "primary"}
showing
closing
finishing
{message: "4", theme: "primary"}
showing
closing
finishing
{message: "5", theme: "primary"}
showing
closing
finishing
{message: "6", theme: "primary"}
showing
closing
finishing
我该如何解决这个问题?
【问题讨论】:
-
所以你想为
450ms显示一个通知然后隐藏它并在有任何排队时显示另一个? -
@martin 每个通知总共显示 3900 毫秒,450 毫秒是隐藏/显示的动画,然后在 3000 毫秒内可见。
标签: angular rxjs angular-services rxjs-pipeable-operators