gpt4 book ai didi

javascript - rxjs ReplaySubject 句柄

转载 作者:行者123 更新时间:2023-12-03 01:33:04 26 4
gpt4 key购买 nike

我在 Angular 4 中使用的模板有问题。该模板实现了一个通知系统,您可以在其中添加新通知,但文档没有指定如何删除观察者 ReplaySubject 的元素。

模板将其实现为服务,如下所示:

private notificationsList: Notification[] = [];
// a stream that publishes new notifications only once
public newNotifications: Subject<Notification> = new Subject<Notification>();

// `notifications` is a stream that emits an array of the most up to date notifications
public notifications: ReplaySubject<Notification[]> =
new ReplaySubject<Notification[]>(1);

// `updates` receives _operations_ to be applied to our `notifications`
// it's a way we can perform changes on *all* notifications (that are currently
// stored in `notifications`)
public updates: Subject<any> = new Subject<any>();

// action streams
public create: Subject<Notification> = new Subject<Notification>();
// public markThreadAsRead: Subject<any> = new Subject<any>();

constructor() {
// recois des operation, et les fais sur la liste interne, puis diffuse le
// resultat sur notifications
this.updates.subscribe((ope) => {
this.notificationsList = ope(this.notificationsList);
console.log(this.notificationsList);
this.notifications.next(this.notificationsList);
});

this.newNotifications
.map(function(notification: Notification): INotificationsOperation {
return (notifications: Notification[]) => {
return notifications.concat(notification);
};
})
.subscribe(this.updates);

}

// an imperative function call to this action stream
public addNotification(notification: Notification): void {
this.newNotifications.next(notification);
}

我尝试询问所有者如何删除通知列表的实际元素,但他只是告诉我可以访问“通知”主题以接收其最新版本。但不要提及我如何实际删除列表中的元素。

有人知道吗?

谢谢!

最佳答案

我添加了一个您可以使用的公共(public)函数。我添加了一条注释,让您了解如果您想按名称删除元素,或者不想调整列表大小,可以修改代码的哪一部分。解释在我的帖子末尾。

  private notificationsList: Notification[] = [];
// a stream that publishes new notifications only once
public newNotifications: Subject<Notification> = new Subject<Notification>();
public removeNotificationByIndex$ : Subject<number> = new Subject<number>();
// `notifications` is a stream that emits an array of the most up to date notifications
public notifications: ReplaySubject<Notification[]> =
new ReplaySubject<Notification[]>(1);

// `updates` receives _operations_ to be applied to our `notifications`
// it's a way we can perform changes on *all* notifications (that are currently
// stored in `notifications`)
public updates: Subject<any> = new Subject<any>();

// action streams
public create: Subject<Notification> = new Subject<Notification>();
// public markThreadAsRead: Subject<any> = new Subject<any>();

constructor() {
// recois des operation, et les fais sur la liste interne, puis diffuse le
// resultat sur notifications
this.updates.subscribe((ope) => {
this.notificationsList = ope(this.notificationsList);
console.log(this.notificationsList);
this.notifications.next(this.notificationsList);
});

this.newNotifications
.map(function(notification: Notification): INotificationsOperation {
return (notifications: Notification[]) => {
return notifications.concat(notification);
};
})
.subscribe(this.updates);

this.removeNotificationByIndex$
.map(function(index: number){
return (notifications: Notification[]) => {
// >>>> DELETE METHOD IS TO BE DEFINED DOWN HERE !
notifications.splice(index,1);
// >>>> DELETE METHOD IS TO BE DEFINED UP HERE !
return notifications
};
})
.subscribe(this.updates);

}

// an imperative function call to this action stream
public addNotification(notification: Notification): void {
this.newNotifications.next(notification);
}

// delete the element in the "index" position of the list.
// /!\ Resizes the list
public removeNotificationsByIndex(index: number): void {
this.removeNotificationByIndex$.next(index);
}

有哪些变化?

public removeNotificationByIndex$ : Subject<number> = new Subject<number>();

该主题将(异步)接收一个索引,并使用该索引触发一个进程。

 this.removeNotificationByIndex$
.map(function(index: number){
return (notifications: Notification[]) => {
// >>>> DELETE METHOD IS TO BE DEFINED DOWN HERE !
notifications.splice(index,1);
// >>>> DELETE METHOD IS TO BE DEFINED UP HERE !
return notifications
};
})
.subscribe(this.updates);

当发出索引时(即您使用关联的命令式函数),会从中生成一个函数(ES6 箭头函数)。就是这样:

(notifications: Notification[]) => {
// >>>> DELETE METHOD IS TO BE DEFINED DOWN HERE !
notifications.splice(index,1);
// >>>> DELETE METHOD IS TO BE DEFINED UP HERE !
return notifications
};

此函数被传递给 this.update,后者将应用它。在这种情况下,ope 就是这个函数。收到后,this.notificationList 修改如下:

this.notificationsList = ope(this.notificationsList);

最后,这个新列表被发布到 ReplaySubject notifications:

this.notifications.next(this.notificationsList);

这会将这个新列表传播给其所有订阅者。

瞧:)。祝你好运!

关于javascript - rxjs ReplaySubject 句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51194015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com