我正在尝试获取最后发布的3个值,代码如下:我希望在填充uiOrder并调用cancelOrderItem()几次后,我可以使用getHistory()访问订单的最后3个版本,但是我实际上得到了最后(当前)值3次,我尝试了重播主题(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);
}
}
我做错了什么?
答案 0 :(得分:3)
您要查找的运算符是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]
答案 1 :(得分:1)
你可能会过度思考它。如果您需要随时提供最新的3个订单,为什么不将它们存储在每次// fld seems to be the class of the input field but looking at all input elements should work too
List<WebElement> inputs = driver.findElements(By.className("fld"));
for (WebElement input: inputs) {
if (inputs.getAttribute("placeholder") == "Email or username") {
// ...
}
}
发出新订单时都会更新的数组中?
uiOrder
现在,只要您想知道最近的3个订单,只需查看orderHistory:Order[] = [];
orders$ = this.uiOrder.subscribe(order =>
let h = this.orderHistory;
//add the order to history array:
h = [...h, order];
//splice the array to keep only the last 3 orders:
if (h.length > 3) h.splice(0,h.length-3)
);
即可。作为奖励,该查找是同步操作。
答案 2 :(得分:-1)
getHistory() {
this.uiOrder.takeLast(3).subscribe((res) => {
console.log('uiOrder', res);
}).unsubscribe() }
takeLast运算符检索从源可观察对象发出的最后三个值。