Skip to content

Commit

Permalink
Merge pull request #86 from francescovallone/feat/77
Browse files Browse the repository at this point in the history
feat(#77)!: remove ApplicationContext and replace it with a new interface for DeferredProvider. (closes #77)
  • Loading branch information
francescovallone authored Sep 11, 2024
2 parents e85a2c8 + a62c390 commit 4971bb2
Show file tree
Hide file tree
Showing 19 changed files with 548 additions and 660 deletions.
48 changes: 0 additions & 48 deletions .website/core/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,6 @@ class AppModule extends Module {
| `middlewares` | A list of `Middleware`s you want to include in the module. |
| `exports` | A list of `Provider`s you want to export to other modules. |

## Creating a DeferredModule

Modules are loaded instantly when the application starts, but if you need to create a module dependent on providers within the application, you can wrap a `Module` with the `DeferredModule` class.

The `DeferredModule` class takes two parameters:

| Parameter | Description |
| --- | --- |
| `inject` | A list of providers that the module needs to load. |
| `init` | A function that returns a `Module` object. |

It also has access to all the properties of the `Module` class, so you can pass the same parameters to the `super` constructor.

```dart
import 'package:serinus/serinus.dart';
class ModuleWithDependencies extends Module {
final TestProvider testProvider;
ModuleWithDependencies(this.testProvider) : super(
imports: [],
controllers: [],
providers: [],
middlewares: []
);
}
class AppModule extends Module {
AppModule(): super(
imports: [
DeferredModule(
inject: [TestProvider],
(context) async {
final prov = context.use<TestProvider>();
return ModuleWithDependencies(prov);
}
)
],
controllers: [],
providers: [TestProvider(isGlobal: true)],
middlewares: []
);
}
```

::: warning
The entry point of the application must be a module that extends `Module` and cannot be wrapped by the `DeferredModule` class.
:::

## Register components asynchronously

Modules can also use the `registerAsync` method to register controllers, providers, and other modules asynchronously. This is useful when you need to perform asynchronous operations to register the components of the module.
Expand Down
19 changes: 11 additions & 8 deletions .website/core/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ 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 registered, you can extend the `DeferredProvider` class.
By default, all providers are registered when the module is registeredd. However, you can defer the registration of a provider until it can be resolved. This is useful when you need to perform asynchronous operations to create the provider or you want to inject another provider into the provider.

This class has a `init` property that accepts a function that returns the provider.
These use cases can be achieved by using the `DeferredProvider` class.

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.
The `DeferredProvider` class has the following properties:

- `inject`: A list of providers that you want to inject into the provider (Only the types of the provider).
- `init`: A function that returns the provider, it receives the list of providers that you want to inject.
- `type`: The type of the provider that you want to create.

::: tip
You can also use a shorthand to create a DeferredProvider by using the `Provider.deferred` factory constructor.
Expand Down Expand Up @@ -137,10 +140,10 @@ class MyModule extends Module {
TestProvider(),
DeferredProvider(
inject: [TestProvider],
init: (context) async {
final prov = context.use<TestProvider>();
return MyProvider(prov);
}
init: (TestProvider provider) async {
return MyProvider(provider);
},
type: MyProvider,
),
],
);
Expand Down
3 changes: 0 additions & 3 deletions examples/ping_pong/lib/app_controller.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import 'package:jaspr/server.dart';
import 'package:serinus/serinus.dart';

class AppController extends Controller {

late Handler handler;

AppController({super.path = '/'}) {
on(Route.get('/'), _handleHelloWorld);
}
Expand Down
Loading

0 comments on commit 4971bb2

Please sign in to comment.