我尝试使用subscription.unsubsribe取消待处理的http请求,如下所示:
getAgentList(pageNumber: number, filter: string): any {
let requestUrl: string = 'api/service/agents_search?ACCT='
+this.accountId;
if ( this.subscription ) {
this.subscription.unsubscribe();
}
this.subscription = this.backEndCommService.getData(requestUrl)
.subscribe(
(res: any) => {
let serverResponse: ServerResponse = new
ServerResponse(this.accountId, pageNumber, res.search_results,
res.resultRows, res.pageSize, res.resultPages)
this._agentListData.next(serverResponse);
},
(err: HttpErrorResponse) => {
let errorMessage: string;
if (err instanceof TypeError) {
errorMessage = 'Script error: ' + err.message;
}
console.log(errorMessage);
});
}
我想知道如何将switchMap应用于此代码以杀死对URL的待处理请求(例如,当第一次搜索花费很多时间并且第二次输入时,自动完成搜索输入,我想要解除第一次。) 感谢
答案 0 :(得分:5)
基本示例:
export class MyComponent{
private $filter: Subject<string> = new Subject<String>();
constructor(){
this.$filter
.switchMap(filter => this.backEndCommService.getData(filter + this.baseUrl)
.subscribe(res => {//do something})
}
getAgentList(filterValue: string){
this.$filter.next(filterValue);
}
}
要使用switchmap取消先前的请求,我们需要一个hot observable来输出我们的过滤器值。我们使用一个主题。每次我们从某个地方获得新价值?我们将它推向主题。
答案 1 :(得分:0)
试过,它甚至没有出去服务器
getAgentList(pageNumber: number, filter: string): any {
let requestUrl: string = 'api/service/agents_search?ACCT='
+this.accountId;
if (filter) {
requestUrl = requestUrl + '&filter=' + filter;
}
if (pageNumber) {
requestUrl = requestUrl + '&pageNumber=' + pageNumber;
}
this.$filter.next(requestUrl)
this.$filter.switchMap(requestUrl =>
this.backEndCommService.getData(requestUrl))
.subscribe(
(res: any) => {
let serverResponse: ServerResponse = new
ServerResponse(this.accountId, pageNumber,
res.search_results,
res.resultRows, res.pageSize, res.resultPages)
this._agentListData$.next(serverResponse);
},
(err: HttpErrorResponse) => {
console.log('handling error');
});
}
此行switchMap(requestUrl =&gt; this.backEndCommService.getData(requestUrl))没有进行调用 悄无声息..... mmmm
答案 2 :(得分:0)
看起来问题是addEventListener
是在getAgentList(pageNumber: number, filter: string): any {
let requestUrl: string = ‘...’
// assuming this.$filter is a Subject
// subscription to it first
this.$filter.switchMap(requestUrl => this.backEndCommService.getData(requestUrl))
.subscribe(
(res: any) => {
let serverResponse: ServerResponse = new
ServerResponse(this.accountId, pageNumber,
res.search_results,
res.resultRows, res.pageSize, res.resultPages)
this._agentListData$.next(serverResponse);
},
(err: HttpErrorResponse) => {
console.log('handling error')
}
);
// then call next()
this.$filter.next(requestUrl)
}
之后完成的。可以把它想象成DOM上的this.$filter
。添加侦听器后,或者在此情况下是订阅者,您将收到事件。
// these are going to be used as 2 bit fields,
// so we can only go to 3.
enum addresses
{
x = 0, // 00
y = 1, // 01
z = 2, // 10
w = 3 // 11
};
int val_to_send;
// set the value to send, and shift it 2 bits left.
val_to_send = 1234;
// bit pattern: 0000 0100 1101 0010
// bit shift left by 2 bits
val_to_send = val_to_send << 2;
// bit pattern: 0001 0011 0100 1000
// set the address to the last 2 bits.
// this value is address w (bit pattern 11) for example...
val_to_send |= w;
// bit pattern: 0001 0011 0100 1011
send_value(val_to_send);
那可能就是这样。除非receive_value(&rx_value);
// pick off the address by masking with the low 2 bits
address = rx_value & 0x3;
// address now = 3 (w)
// bit shift right to restore the value
rx_value = rx_value >> 2;
// rx_value = 1234 again.
是BehaviorSubject或ReplaySubject,否则我们可能会遇到其他问题。