7

I want to clear an observable object array. What I did:

private pages: Observable<CustomPage[]>;
//clear
this.pages.map(p => p = []);

Is there a direct Observable method(rxjs) to do this without mapping?

Maddy
  • 2,114
  • 7
  • 30
  • 50
  • https://stackoverflow.com/a/40971371/5468463 ? – Vega Aug 26 '17 at 07:52
  • 1
    Maybe you can explain what you're trying to achieve. An observable is a stream of events, so you cannot actually clear it. By mapping it to an empty array you'll transform all the emitted values to that and I'm not sure this is what you want. – Adrian Fâciu Aug 26 '17 at 08:41
  • Sorry about that. My goals is: A service returns a set of Custom pages for a given date selected using a date picker. So let's say I get custom pages for day d1 and when I select another day d2 from date picker, it should clear the Custom page array, make a new http request and populate the new values. – Maddy Aug 26 '17 at 08:47

3 Answers3

8

I found this works really well and is very straight forward.

this.someObservable = of([]);  <-- Assigning empty array to an observable.

The ' of ' is a part of the rxjs library and returns an observable so you can use it here with the assignment operator.

Hope this helps :)

Alan Richards
  • 283
  • 5
  • 17
2

If you're coming from the tour of heroes tutorial your understanding may be limited by the pretty basic services they show there. Here is a quick sketch of a more fully fleshed out service that does what you want...

export class PageService {

  private _pages = new ReplaySubject<CustomPage[]>(1);

  private _dateFilter = new ReplaySubject<DateFilterCriteria>(1);

  //Subscribe to this in your component
  get pages(): Observable<CustomPage[]> {
    return this._pages;
  }

  //Use this if you want to display the currently selected date
  //filter somewhere (e.g. on your date filter control)
  get dateFilter(): Observable<DateFilterCriteria> {
    return this._dateFilter;
  }

  //PageAccess encapsulates requests against the Http service
  constructor(private pageAccess: PageAccess) {
    this._dateFilter.subscribe(dateFilter => this.fetchPages(dateFilter));
    this.setNewDateFilter(null);
  }

  //Clears out the current list of pages, issues a new request
  //for pages
  private fetchPages(dateFilter: DateFilter) {
    this._pages.next([]);
    this.pageAccess.fetchPages(dateFilter).subscribe(pages => {
      this._pages.next(pages);
    });
  }

  //When your date filter control changes it should call this
  //method
  setNewDateFilter(dateFilter: DateFilter) {
    this._dateFilter.next(dateFilter);
  }

}

Also, I think I used DateFilter and DateFilterCriteria interchangeably to represent some class that you use to indicate what dates of pages you want.

Pace
  • 41,875
  • 13
  • 113
  • 156
2

Short and Sweet

new Observable<[]>();
async_soul
  • 143
  • 1
  • 8
  • This is the only one that worked for me e.g: `this.list$ = new Observable` as when I search it starts observing again and populate the DOM. `of([])` didn't – Juliano Vargas May 16 '22 at 14:21