gpt4 book ai didi

angular - Rxjs 获取不到过去的值

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:23 25 4
gpt4 key购买 nike

我正在尝试获取发出的最后 3 个值,使用下面的代码我期望在填充 uiOrder 并调用 cancelOrderItem() 几次后,我可以使用 getHistory() 访问订单的最后 3 个修订,但是我实际上得到了最后一个(当前)值 3 次,我尝试过 replaySubject(3) 得到相同的结果。这是我的代码:

export class OrdersService {

public readonly orders: Observable<Order[]>;
public readonly uiOrder: Observable<Order>;

private _orders: BehaviorSubject<Order[]>;
private _uiOrder: BehaviorSubject<Order>;

private dataStore: {
uiOrder: Order,
orders: Order[]
};


constructor() {

this.dataStore = {
uiOrder: null,
orders: []
};

this._orders = <BehaviorSubject<Order[]>>new BehaviorSubject([]);
this._uiOrder = <BehaviorSubject<Order>>new BehaviorSubject({});
this.orders = this._orders.asObservable();
this.uiOrder = this._uiOrder.asObservable();
}

getOrder(orderId: number | string) {
for (let i = 0; i < this.dataStore.orders.length; i++) {
if (this.dataStore.orders[i].id == orderId) {
this.dataStore.orders[i].lastAccess = moment().format().slice(0, 19) + 'Z';
this.dataStore.uiOrder = this.dataStore.orders[i];
this.updateUiOrder();
}
}
}


cancelOrderItem(action) {
this.dataStore.uiOrder.sections[action.sectionIndex].orderDetails.splice(action.orderDetailsIndex, 1);
this.updateUiOrder()
}

getHistory() {
this.uiOrder.take(3).subscribe((res) => {
console.log('uiOrder', res);
}).unsubscribe()
}

updateUiOrder() {
console.log('updating ui order');
this._uiOrder.next(this.dataStore.uiOrder);
}

}

我做错了什么?

最佳答案

您要找的接线员是bufferCount(3,1)它创建了一个包含最后 3 个值的滑动窗口。缺点:它只会在累积 3 个值后才开始发射。如果你想获得第一个值,你可以这样做:

Rx.Observable.from([null,null])
.concat(this.uiOrder)
.bufferCount(3,1)
.subscribe(uiOrderHistory => ... );

这样您的弹珠图将如下所示:

---Order1-----------------Order2--------------------Order3
bufferCount(3,1)
---[null,null,Order1]-----[null,Order1,Order2]------[Order1,Order2,Order3]

关于angular - Rxjs 获取不到过去的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858962/

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