Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49083

Aggrregating multiple service providers

$
0
0

Hi all,

I am looking for advice on the best pattern to do the following:

My app has 10 service providers to service the state of 10 different data types. Their classes are all extending a base generic class to cater for common CRUD operations on these entities (as simple variant of ngrx).

For cases like login/logout and save/load of data, I want to create a simple way to invoke same methods (e.g. getters) for all in one go.

What would be the most efficient way to achieve this?

What I have done now - with somehow satisfactory results (even though a bit unpredictable) is:
1 Create an service that injects all of the 10
2 The service holds an array of all services
3 I iterate the array to invoke methods like the getter

Sample code:

  constructor(
    private buyinginterestService: BuyinginterestService,
    private buyingopportunityService: BuyingOpportunityService,
    private buyingNeedService: BuyingNeedService,
    private commodityService: CommodityService,
    private countryService: CountryService,
    private converstationReviewService: ConversationReviewService,
    private plotService: PlotService,
    private priceService: PriceService,
    private productionService: ProductionService,
    private purchaseService: PurchaseService,
    private userService: UserService,

    private firebaseService: FirebaseService,
    private storage: Storage,
  ) {
    this.stateProviderList = [
      this.buyinginterestService,
      this.buyingopportunityService,
      this.buyingNeedService,
      this.commodityService,
      this.converstationReviewService,
      this.countryService,
      this.plotService,
      this.priceService,
      this.productionService,
      this.purchaseService,
      this.userService
    ];



and saving to localstorage

    this.stateProviderList
      .forEach(provider => {
        const savePath = this.localSavePath + '-' + provider.getUniqueKey();
        provider.getState$()
          .pipe(take(1))
          .toPromise()
          .then(state => {
            console.log('Save local state: ', savePath, state);
            return this.storage.set(savePath, state);
          });
      });

Issues I have with this pattern is that it seems that the array appears empty (console.log) but is filled (?), and only after a few other actions, these methods invoke. Probably I am assuming too much of what actually happens under the hood in Angular

Any thoughts are very welcome

Regards

Tom

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 49083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>