getOrProvide

fun <S, T : Any, PutParams> ScopedServiceLocator<S>.getOrProvide(key: ServiceKey<T, *, Unit, PutParams>, allowedScopes: (S) -> Boolean, putParams: PutParams): T

Fetches an item for the given key.

If the key has not been previously registered, will create a new entry using putParams. If allowedScopes returns false for this ServiceLocator's scope, the created entry will come from ScopedServiceLocator.onInvalidScope.

Parameters

key

The ServiceKey for the desired service.

allowedScopes

A predicate to check if the current ScopedServiceLocator.scope is valid for this provider.

putParams

The parameters needed to create a new ServiceEntry for key if one doesn't exist.


fun <S, T : Any> ScopedServiceLocator<S>.getOrProvide(key: LazyKey<T>, allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode = defaultLazyKeyThreadSafetyMode, provider: () -> T): T

Fetches an item for the given key.

If an entry for the key has not been previously registered, it creates and stores a new one using the provider lambda. Creation is subject to the allowedScopes predicate; if the current scope is not allowed, this delegates to ScopedServiceLocator.onInvalidScope.

The multi-thread behavior depends on threadSafetyMode.

Parameters

key

The ServiceKey for the desired service.

allowedScopes

A predicate to check if the current ScopedServiceLocator.scope is valid for this provider.

threadSafetyMode

The thread safety mode for the lazy initialization.

provider

A lambda that creates the service instance if one doesn't exist.