Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lazy<T> spec and catalog scopes #14

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

sergiimk
Copy link
Member

@sergiimk sergiimk commented Dec 7, 2024

Introduces a new type of injectable:

struct Impl {
  lazy_a: Lazy<Arc<dyn A>>,
}

Lazy<T> delays the value creation from catalog until it's requested:

let a = self.lazy_a.get()?;

Lifetime of A in this case is still controlled by the Scope (e.g. could be a Singleton and reuse an instance), so user code is expected to get() instances and let them drop ASAP after use.

Lazy values will be created using a cloned instance of a catalog that Lazy<T> stores during its instantiation alongside the Impl instance.

In cases like DB transactions this is not great, as we may need a flexibility to add transaction dynamically into a chained catalog after the Lazy<Transaction> was created, yet Lazy will be bound to an instance of Catalog that doesn't have the transaction.

To solve this I'm introducing the idea of a task-local current catalog:

catalog.scope(async move {
  // Lazy::get() will use scoped catalog
  // Catalog::current() to access catalog in current scope
}).await

@sergiimk sergiimk force-pushed the feature/lazy_and_catalog_scopes branch 3 times, most recently from 65c315f to b327f57 Compare December 7, 2024 21:48
@sergiimk sergiimk force-pushed the feature/lazy_and_catalog_scopes branch from b327f57 to 36f3b37 Compare December 7, 2024 22:08
@sergiimk sergiimk marked this pull request as ready for review December 7, 2024 22:09
@zaychenko-sergei zaychenko-sergei merged commit 881e5fd into master Dec 9, 2024
1 check passed
@sergiimk sergiimk deleted the feature/lazy_and_catalog_scopes branch December 10, 2024 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants