RXJS主体和行为主体

RXJS subject and behaviour subject

我在可观察的时间间隔内有多个带有动态参数数组参数的请求。那么我如何return一个基于数组参数的主题。因为BehaviorSubject里面包含了所有的数据

初始化主题

getSchedularData: BehaviorSubject < any > = new BehaviorSubject < any > (null);
constructor(private httpClient: HttpClient, ) {
  SchedulerStore.select('jobSchedulerState')
    .subscribe(response => {
      this.schedularDataCollector = response;
    });
}

分量

this.schedulerService.startScheduler(this.channelList1)
  .subscribe((value) => {
    // console.log(value);
    // tslint:disable-next-line:forin
    for (const keys in value) {
      this.schedularData[keys] = value[keys];
    }
  });

服务

Observable.interval((!this.backChannelEnvironment.schedularInterval) ? 10000 : this.backChannelEnvironment.schedularInterval)
  .pipe(
    map(() => {
      /**
       * dispatch request for schedular for requesting http request for channel
       */
      this.SchedulerStore.dispatch(new SchedulerAction.GetScheduler(Channels));
    })
  )
  .subscribe(() => {
    /**
     * get data from schedular store and return it at schedular interval
     */
    if (this.schedularDataCollector != null) {
      if (JSON.stringify(this.schedularDataCollector['jobScheduler']) !==
        JSON.stringify(this.getSchedularData.value)) {

        this.getSchedularData.next(this.schedularDataCollector['jobScheduler']);
      }
    }
  });
return this.getSchedularData.asObservable();

请通过以下代码:- 在这里我提到了一些其他方法,如 debounceTime() 和 distinctUntilChanged() 根据您的要求更改。

@Output() completeMethod: EventEmitter<any> = new EventEmitter();
customEvent: Event;
    private yourSubject = new Subject<string>();

    constructor(){
    this.yourSubject.asObservable().pipe(filter(data => data != null),
         debounceTime(1000), distinctUntilChanged()).subscribe(value => {
          this.loading = true;
          this.completeMethod.emit({
            originalEvent: this,
            query: value
          });
        });
        }

        ngAfterViewInit() {
        this.inputEL.nativeElement.addEventListener('keyup', (event) => {
          this.customEvent = event;
          this.yourSubject.next(event.target.value);
        });
      }

您可以尝试下面的一个,因为我们的实现与您几乎相同:

private requests: Request[];
private observableRequests: BehaviorSubject<Request[]>;  

constructor() {
  this.requests = new Array<Request>;  
  this.observableRequests = <BehaviorSubject<Request[]>>new BehaviorSubject([]);
}

get requests() {
  return this.observableRequests.asObservable();
}

addRequest(request: Request) {
  this.requests.push(request);
  this.observableRequests.next(this.requests);
}

只要您调用 addRequest,所有请求对象都会 return。 您必须根据您的要求在该方法中做一些解决方法。