ZupIT/beagle-web-angular

Improve Beagle module usage

carolinegoncalveszup opened this issue · 0 comments

What would you like to be added:
Considering an application with lazy loading, each container has its own module. In this situation, the Beagle module is imported in each module that will make use of it. This causes a problem is when using what was suppose to be a singletons, for example global context.
The implementation we have in the actual version, each Beagle module being imported creates a new instance of BeagleProvider, so if one of the containers sets the global context, the other container will not be able to access it because each container will have its own instance of this provider.
The idea is to create the forRoot and forChild methods in Beagle module, so the app.module would use forRoot that creates the BeagleProvider and the other containers module would use forChild . With this approach the BeagleProvider is created only once and shared between the modules, so the global context will be the same for the entire application.

A test that I did that worked and show that multiple instances are being created is:
Add providedIn: 'root' to the BeagleProvider and remove from the providers declaration in the BeagleModule. With this approach, there is an error logged on the console zup-it-beagle-angular.js:1731 Beagle service has already started! because now the BeagleProvider is shared between the entire application and is trying to start again. Although there is an error logged, the application works and the global context is shared between the different lazy load modules.