The SingletonFactory function or class whose
shared/cached value the accessor returns. See SingletonFactory for
important details on how the factory is run and the restrictions on what it
can do. Note that since the accessor is just shorthand for $(), its state
and implementation can be created, deleted, replaced, etc. using the
$cache APIs.
Note that if you want your code to be testable with newRoot, you should avoid storing the result of calling the service accessor anywhere it can outlive the root job, since you will otherwise end up with a stale reference to the previous service instance and fail to initialize the new instance.
Your services can detect this scenario, however, by having the factory wrap its return value with expiring(), which will make any access to a saved service value throw a TypeError after the root job ends. (Note that such a thing should not be necessary in your production builds, however, since at runtime you will normally only ever have one root job.)
Wrap a factory function to create a "service" accessor (a shorthand function for getting
$(factory)).This function returns an equivalent to
() => $(factory)or$.bind(null, factory), but optimized for raw access speed (close to a module export binding) at the expense of slightly-higher memory use.Every distinct
factorypassed toservice()consumes some bookkeeping space that will not be released until the application exits. (Or the next call to newRoot() happens, but that's normally only used for testing!)So this is for stable/static accessors: module-level constants, classes, etc., not factories that are created on the fly from closures in functions that get called a lot. (For those use cases, just substitute
() => $(factory)or$.bind(null, factory).)