mercredi 17 avril 2019

How to design NGRX effect to react to signalR as well as call methods without having a circular dependency?

How to design NGRX effect to react to signalR as well as call methods without having a circular dependency?

I have the following:

// Sends and receives data via SignalR to Server
export class EntityDataService{ 

   constructor(private ngrxStore: Store<fromStore.DataState>,       
      private signalRHub: SignalRHub) { }    

   public getEntity<T>(id: string): Promise<T> {
     // Requests entity from signalRHub and returns result.
   }

   public subscribeToEntity<T>(id: string): Promise<void> {
     // Notifies server to listen to any changes of entity on server.
   }

   public onEntityUpdateFromServer<T>(someEntity: T) {
     // Receives a modified entity from the server periodically, will 
     // need to forward to ngrx effect corresponding to entity some how.
   }

}

And:

@Injectable()
export class CustomersEffects {

   constructor(private actions$: Actions,    
     private entityDataSvc: EntityDataService) { }

   @Effect()
   getCustomer$ = this.actions$.pipe(
      ... 
      return from(this.entityDataSvc.getEntity(Customer, custId));
      ... )

   @Effect()
    subscribeToCustomer$ ...

   //  This is called by EntityDataService.onEntityUpdateFromServer() somehow.
   @Effect()
   reactToServerUpdateOfCustomer$ ...
}

I have considered:

  1. Remove Store from EntityDataService, would prefer EntityDataService not coupled to its consumers.
    • EntityDataService would emit / call next on an entityUpdate: Subject<EntityUpdate>; All n effects in the store, 1 for each type of Entity would subscribe to EntityDataService and filter on instanceOf(Customer) for customer effect example.
    • This results in a lot of CPU cycles for every inbound entity update. Given we have ~100 models. Thats 100 observables doing a filter call for each update.
  2. Create a single effect that subscribes to EntityDataService.onUpdate: Subject, make determination and trigger new action / effect with appropriate model.
    • This effect is basically a singleton. Does that fit with ngrx / effect pattern?

Additional info: We will use code generation to maintain the 1:1 relationship between a server model and its effect.

Aucun commentaire:

Enregistrer un commentaire