Uneventful
    Preparing search index...

    Function service

    • Define a configurable service accessor

      The returned accessor function, when called, will run the factory in the root job and cache the result until/unless a new root is started.

      The factory and result can be overridden at runtime using the accessor's methods: i.e. myService.set(obj) sets the instance that myService() will return, myService.replace(otherFactory) changes the factory used to create it, and so on.

      Type Parameters

      • T

      Parameters

      • defaultFactory: ServiceFactory<T>

        The ServiceFactory function or class whose shared/cached result the accessor returns by default. See ServiceFactory for important details on how the factory is run and the restrictions on what it can do.

      Returns ServiceAccessor<T>

      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.)