-2

I have multiple requests with dynamic parameter array parameter in observable interval. So how can I return a subject based on the array parameter. Because of the BehaviorSubject contain all data in it

initialize subject

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

component

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];
    }
  });

service

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();
SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
  • If you can replicated your problem in stackblitz..? that would be awesome ! – user1608841 Oct 16 '18 at 11:19
  • in sort behavior subject contain an array object and for each request, there is a parameter in it. According to the parameter I have to filter behavior subject and return a result. it works correctly but when I request the services twice with different parameter then it overwrites the result of 1 request with 2 requests and returns a result of result 2 in both requests. my parameter is dynamic so i cannot create a custom behaviour for each parameter. – Venkatesh Parihar Oct 17 '18 at 13:33
  • [stackblitz](https://stackblitz.com) or it didn't happen. reproduce please. – Stavm Oct 22 '18 at 05:26

2 Answers2

1

Please go through below code:- Here i have mention some other method like debounceTime() and distinctUntilChanged() change according to your requirements.

@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);
        });
      }
Anup pandey
  • 437
  • 2
  • 9
1

You can try below one as we are having almost same implementation as you have :

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);
}

Here all request object will return whenever array is you call addRequest. You have to do some workaround in that method as per your requirement.

Vishw Patel
  • 529
  • 2
  • 9