2

I have a sidebar, and when you make changes to it I need to subscribe to those changes and react to them so we are using a Subject.

The thing is we don't know the best architecture for a Subject, should it be a single string emitted or an object?

If we emit an object of all the changes as key:value pairs, how do we subscribe to just one of those changes elsewhere instead of reacting to the entire object?

I want to void polluting my code with a Subject emitter for every change or is that the "best practice"

Example of current implementation

Emitter

/**
 * A function that emits an RXJS event to any observers subscribed to this subjet
 * when the user changes the calendar in the sidebar.
 * @return {Observable} An Observable that, whenever subscribed, will execute the
 * specified function.
 * @static false
 */
public emitCalendar = (calendar: any): void => {
    this.selectedCalendar = calendar;
    this.onChangeLeftPane$.next({ calendar: calendar });
};

And then what is the best way to subscribe just to

onChangeLeftPane$.calendar
Anshul Riyal
  • 123
  • 1
  • 12
James
  • 235
  • 3
  • 15
  • You can subscribe to the whole object and use the "distinctUntilChanged" pipe to only receive a notification if your desired key changed https://www.learnrxjs.io/operators/filtering/distinctuntilchanged.html – Severin Klug May 06 '19 at 13:50

1 Answers1

0

Setup a separate Observable to listen to change with filter operator

    this.onChangeCalendar=this.onChangeLeftPane$.asObservable()
.pipe(filter(obj=>Objects.keys(obj).includes('calendar'))
this.onChangeLeftPane$.next({ calendar: calendar });
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39