dimanche 13 décembre 2020

Observable value-by-X patterns

I have noticed a pattern developing in a project I'm working on, and I'm looking for some criticism or support for it here.

We have a hash of Objects in a Service keyed by some unique ID, let's say username. We create a BehaviorSubject about this table, and whenever any object in this hash changes for some reason, we next() the Object hash so that all the things observing the Object hash are delivered the new state of that hash in its entirety.

There are then times when we will do something like this (reductive for the sake of simplicity):

<ng-container *ngIf="(service.objectHash$ | async) as objectHash">
  <div *ngFor="let username of usernames">
    
  </div>
</ng-container> 

That *ngFor is really only interested in some of the entries in that Object hash generally, which is to say Object.keys(objectHash) is a superset of usernames.

Furthermore, we are thinking of making a bunch of different services that retain different compartmentalized information about each username, and then using combineLatest downstream to 'merge' information about users from some of them where that information (or information derived from it) is needed, despite the fact that we could have a single Service encapsulate a more complex / comprehensive Object for each username, which would obviate the need for any kind of combineLatest mechanics downstream at the observers.

  1. Is any of this a sound way to handle the situation I'm describing or have I walked myself into an anti-pattern? Is there a better / more canonical way of approaching this problem of observing properties of an collection of things as they change?

  2. Is one of the two approaches to compartmentalizing this kind of information better than the other (one service to rule them all versus several smaller services that are 'combined' downstream?

Aucun commentaire:

Enregistrer un commentaire