Skip to content

Commit

Permalink
fix: rename refresh_xx methods to flush_xx in Context
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihanType committed Sep 11, 2023
1 parent b4c9620 commit 56fe1a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions rudi/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Context {
});
}

/// Refresh the context.
/// Flush the context.
///
/// This method has two purposes:
///
Expand Down Expand Up @@ -428,7 +428,7 @@ impl Context {
/// assert!(!cx.contains_provider::<A>());
/// assert!(!cx.contains_singleton::<B>());
///
/// cx.refresh();
/// cx.flush();
///
/// // evaluate condition
/// assert!(cx.contains_provider::<A>());
Expand Down Expand Up @@ -474,46 +474,46 @@ impl Context {
/// fn main() {
/// let mut cx = Context::default();
///
/// // Method 1, call `load_modules` and then call `refresh` immediately
/// // Method 1, call `load_modules` and then call `flush` immediately
/// cx.load_modules(modules![AModule]);
/// cx.refresh();
/// cx.flush();
/// cx.load_modules(modules![BModule]);
/// cx.refresh();
/// cx.flush();
///
/// // The evaluation result of `A`'s `condition` is `false`, so `A` will not be created
/// assert!(!cx.contains_provider::<A>());
///
/// let mut cx = Context::default();
///
/// // Method 2, call all `load_modules` first, then call `refresh`
/// // Method 2, call all `load_modules` first, then call `flush`
/// cx.load_modules(modules![AModule]);
/// cx.load_modules(modules![BModule]);
/// cx.refresh();
/// cx.flush();
///
/// // The evaluation result of `A`'s `condition` is `true`, so `A` will be created
/// assert!(cx.contains_provider::<A>());
/// }
/// ```
#[track_caller]
pub fn refresh(&mut self) {
pub fn flush(&mut self) {
self.create_eager_instances();

self.evaluate_providers();
self.create_eager_instances();
}

/// Async version of [`Context::refresh`].
/// Async version of [`Context::flush`].
///
/// If no provider in the context has an async constructor and that provider needs to be eagerly created,
/// this method is the same as [`Context::refresh`].
/// this method is the same as [`Context::flush`].
///
/// See [`Context::refresh`] for more details.
/// See [`Context::flush`] for more details.
///
/// # Panics
///
/// - Panics if there are multiple providers with the same key and the context's [`allow_override`](Context::allow_override) is false.
/// - Panics if there is a provider that panics on construction.
pub async fn refresh_async(&mut self) {
pub async fn flush_async(&mut self) {
self.create_eager_instances_async().await;

self.evaluate_providers();
Expand Down Expand Up @@ -2130,7 +2130,7 @@ impl ContextOptions {
#[track_caller]
pub fn create(self, modules: Vec<ResolveModule>) -> Context {
let mut cx = self.inner_create(|cx| cx.load_modules(modules));
cx.refresh();
cx.flush();
cx
}

Expand Down Expand Up @@ -2159,7 +2159,7 @@ impl ContextOptions {
cx.load_providers(module.eager_create(), module.providers())
});

cx.refresh();
cx.flush();
cx
}

Expand All @@ -2176,7 +2176,7 @@ impl ContextOptions {
/// - Panics if there is a provider that panics on construction.
pub async fn create_async(self, modules: Vec<ResolveModule>) -> Context {
let mut cx = self.inner_create(|cx| cx.load_modules(modules));
cx.refresh_async().await;
cx.flush_async().await;
cx
}

Expand All @@ -2202,7 +2202,7 @@ impl ContextOptions {
cx.load_providers(module.eager_create(), module.providers())
});

cx.refresh_async().await;
cx.flush_async().await;
cx
}
}
Expand Down
4 changes: 2 additions & 2 deletions rudi/tests/feat_eager_create_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn eager_create_module_twice() {
assert!(COUNT.with(|created| *created.borrow() == 1));
assert!(cx.singleton_registry().len() == 1);

cx.refresh();
cx.flush();

assert!(COUNT.with(|created| *created.borrow() == 1));
assert!(cx.singleton_registry().len() == 1);
Expand Down Expand Up @@ -443,7 +443,7 @@ async fn eager_create_module_twice_async() {
assert!(COUNT.with(|created| *created.borrow() == 1));
assert!(cx.singleton_registry().len() == 1);

cx.refresh_async().await;
cx.flush_async().await;

assert!(COUNT.with(|created| *created.borrow() == 1));
assert!(cx.singleton_registry().len() == 1);
Expand Down

0 comments on commit 56fe1a1

Please sign in to comment.