We are building a suite of products with module-based components, where each module is designed to be reusable across multiple products.
In one such module, we need to poll the server to track the status of certain processes. Multiple components within this module depend on this polling mechanism.
To handle this, I created a shared class inside the module’s component folder and exported a singleton instance. Components in the module import this instance and subscribe using a unique ID (which is the process ID). Whenever the status changes, the corresponding callback is triggered. This approach is working well so far.
However, I recently came across the recommendation in Ember.js that shared state or long-lived data should be managed using services.
This raised a few questions for me:
- My understanding is that services are primarily used to share data across the entire application.
- In my case, the polling logic is only relevant to a specific module, not the whole app.
- If I convert this into a service, I might end up creating many module-specific services, which feels excessive.
My Questions
-
Is it a best practice in Ember to always use services for shared state like polling?
-
Is my current approach (module-level singleton) acceptable in this scenario?
-
If I use services for module-specific logic, is having many such services considered okay?
Your feedback would be really helpful.