Repositories
bevy.repository.Repository
Repository(parent: Repository | None = None, *, providers: tuple[Provider[_K, _V]] = ())
Bases: _NullRepository[_K, _V]
The Bevy repository manages instance providers and caching the results that the providers create.
add_providers
add_providers(*providers: Provider[_K, _V])
Adds providers to the repository at a higher priority than existing providers. These providers will be used to lookup and create instances that will be stored and returned by the repository.
branch
branch() -> Repository[_K, _V]
Creates a new repository that inherits the providers from the current repository. Dependencies not found on the new repository can be propagated to the branched parent repository. This allows the branch repository to inherit dependencies from the parent repository and protects the parent from changes to the branch.
create
create(key: _K) -> Option[_V]
Attempts to create an instance that adheres to the type _V and that corresponds to the key by looking for a provider that supports the key. Returns a Null option when no provider is found for the key, otherwise returns a Value option containing the instance created by the provider.
find
find(key: _K, *, allow_propagation: bool = True) -> Option[_V]
Searches all providers for a cached instance that adheres to the type _V and that corresponds to the key. A Value option containing the matching instance is returned, when no match is found a Null option is returned.
When allow_propagation
is set to True (default) this will search any parent repositories for matching cached
values.
fork_context
fork_context() -> Repository[_K, _V]
Branches the repository and sets the branch as the repository for the current context.
get
get(key: _K, default: _V | None = None, *, allow_propagation: bool = True) -> _V
Attempts to get an instance adhering to the type _V that corresponds to the key. It first attempts to find a matching instance that is cached on any provider. If no cached instances are found, it attempts to create an instance that is then cached. When a match is found or created it is returned, otherwise the default is used.
When allow_propagation
is set to True (default) this will search any parent repositories for matching cached
values. If no matches are found on this repository or any of it's parents, it will attempt to create the value
in this repository's cache.
get_repository
classmethod
get_repository() -> Repository[_K, _V]
Retrieves the repository instance that is assigned to the current context.
set
set(key: _K, value: _V) -> Option[_V]
Attempts to cache a value. A Null option is returned when no providers support the key, otherwise a Value option containing the value that was placed in the cache is returned.
set_repository
classmethod
set_repository(repository: Repository[_K, _V])
Assigns a new repository instance to the current context.