There is some (for arguments sake) “global” application state that I am interested in injecting into components.
Each candidate component should receive an immutable snapshot of the state as it existed at the time that its constructor was invoked (for absolute clarity, the state snapshot that is injected into each component’s constructor must not change when the global state changes).
I cannot (and do not wish to) use a root-level service or global constant from which components request a state snapshot. Such a solution would require that a component must explicitly request a state snapshot (from the injected service or global constant) from within its constructor, because if it attempted to retrieve it later in its lifecycle, the state might have changed and no longer be relevant to it.
I want to avoid having to declare an injected service locally within each client component’s providers array. This is unnecessarily burdensome. Clients should be able to simply inject a type (or injection token) and receive a state snapshot.
Although an injection factory method seemed on the surface to be an obvious solution (it would copy the global state and return it to the injector, which would in turn pass it to the component’s constructor), the factory is only invoked once (for each level in the injection hierarchy for which a provider has been configured). I understand that this is by design, however I think that there is a good use-case for the functionality I have described.
What would potentially work is an injection factory implementation similar in concept to Angular pure and impure pipes, where a factory (or potentially any injection configuration) could be declared “impure”, which would result in a new instance being injected into every dependent component.
Surely there is a way to do this?