Manuel Schiller Manuel Schiller - 3 months ago 12
AngularJS Question

RXJS - dinstinctUntilChange with multiple keys

I want to fire

.subscribe()
for my oberservable if one of three object keys have changed.

it works, if I copy & paste the method for each key:

this.myService.loadData(this.dataContainer.id, true)
.distinctUntilChanged((updatedData) => { return updatedData.relations; })
.subscribe(updatedData => {
console.log("relations changed",updatedData);
});

this.myService.loadData(this.dataContainer.id, true)
.distinctUntilChanged((updatedData) => { return updatedData.parent; })
.subscribe(updatedData => {
console.log("parent changed",updatedData);
});

this.myService.loadData(this.dataContainer.id, true)
.distinctUntilChanged((updatedData) => { return updatedData.children; })
.subscribe(updatedData => {
console.log("children changed",updatedData);
});


if I set the
distinctUntilChanged
comparer to return the whole
updatedData
object, my subscribe never fires.

How can I combine the three
dinstinctUntilChanged
into one reducer?

Answer

One way:

const data$ = this.myService.loadData(this.dataContainer.id, true);

Observable
  .combineLatest(
    $data.distinctUntilChanged(data => updatedData.relations),
    $data.distinctUntilChanged(data => updatedData.parent),
    $data.distinctUntilChanged(data => updatedData.children),
    data => data
  )
  .subscribe(updatedData => {
    console.log("relation|parent|children changed", updatedData);
  });
Comments