Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
francescovallone committed Sep 10, 2024
2 parents d85069c + e270793 commit e85a2c8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .website/core/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ class MyController extends Controller {
}
```

You can find more information about metadata in the [Metadata](/core/metadata.html) section.
You can find more information about metadata in the [Metadata](/core/metadata.html) section.
8 changes: 7 additions & 1 deletion .website/core/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class MyModule extends Module {
)
}
```

:::

## Global Providers
Expand All @@ -103,13 +104,18 @@ class MyProvider extends Provider {

## Deferred Providers

By default, all providers are created when the module is created. If you want to create the provider after all the modules are created, you can extend the `DeferredProvider` class.
By default, all providers are created when the module is created. If you want to create the provider after all the modules are registered, you can extend the `DeferredProvider` class.

This class has a `init` property that accepts a function that returns the provider.

Also the `init` function has access to the application context and contains all the providers initialized.
When a DeferredProvider is initialized, its provider is added to the application context so that it can be used as dependency by other providers. This grant a incremental initialization of the providers.

::: tip
You can also use a shorthand to create a DeferredProvider by using the `Provider.deferred` factory constructor.
This constructor uses the same parameters as the `DeferredProvider` class.
:::

::: code-group

```dart [Deferred Provider]
Expand Down
11 changes: 11 additions & 0 deletions packages/serinus/lib/src/core/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ abstract class Provider {

/// The [Provider] constructor is used to create a new instance of the [Provider] class.
const Provider({this.isGlobal = false});

/// The factory constructor [Provider.deferred] is used to create a new instance of the [Provider] class with dependencies.
/// It uses the [DeferredProvider] class to define a provider that is initialized asynchronously.
///
/// The [init] function is called when the provider is initialized.
/// The [inject] property contains the types of other [Provider]s that will be injected in the provider.
factory Provider.deferred(
Future<Provider> Function(ApplicationContext context) init, {
required List<Type> inject,
}) =>
DeferredProvider(init, inject: inject);
}

/// The [DeferredProvider] class is used to define a provider that is initialized asynchronously.
Expand Down
9 changes: 9 additions & 0 deletions packages/serinus/test/core/provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class TestProviderDependent extends Provider {
TestProviderDependent(TestProvider provider);
}

class TestProviderDependent2 extends Provider {
TestProviderDependent2(TestProvider provider);
}

class TestProviderOnInit extends Provider with OnApplicationInit {
bool isInitialized = false;

Expand Down Expand Up @@ -100,13 +104,18 @@ void main() async {
DeferredProvider((context) async {
final dep = context.use<TestProvider>();
return TestProviderDependent(dep);
}, inject: [TestProvider]),
Provider.deferred((context) async {
final dep = context.use<TestProvider>();
return TestProviderDependent2(dep);
}, inject: [TestProvider])
]);
await container.registerModules(module, Type);

await container.finalize(module);

expect(container.get<TestProviderDependent>(), isNotNull);
expect(container.get<TestProviderDependent2>(), isNotNull);
});

test(
Expand Down

0 comments on commit e85a2c8

Please sign in to comment.