Skip to content

Commit

Permalink
feat(#66): add OnApplicationBootstrap, add OnApplicationReady lifecyc…
Browse files Browse the repository at this point in the history
…le hooks
  • Loading branch information
francescovallone committed Sep 21, 2024
1 parent 24aa1df commit 6f1dd5b
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/serinus/bin/serinus.dart
Original file line number Diff line number Diff line change
@@ -67,7 +67,6 @@ class TestProviderFour extends Provider with OnApplicationInit {
class CircularDependencyModule extends Module {
CircularDependencyModule()
: super(imports: [
AnotherModule()
], controllers: [], providers: [
Provider.deferred((TestProvider tp) => TestProviderThree(tp),
inject: [TestProvider], type: TestProviderThree),
9 changes: 9 additions & 0 deletions packages/serinus/lib/src/core/application.dart
Original file line number Diff line number Diff line change
@@ -161,6 +161,10 @@ class SerinusApplication extends Application {
},
errorHandler: (e, stackTrace) => _logger.severe(e, stackTrace),
);
final providers = modulesContainer.getAll<OnApplicationReady>();
for (final provider in providers) {
await provider.onApplicationReady();
}
} on SocketException catch (e) {
_logger.severe('Failed to start server on ${e.address}:${e.port}');
await close();
@@ -185,6 +189,11 @@ class SerinusApplication extends Application {
final explorer = Explorer(modulesContainer, router, config);
explorer.resolveRoutes();
await modulesContainer.finalize(entrypoint);
final registeredProviders =
modulesContainer.getAll<OnApplicationBootstrap>();
for (final provider in registeredProviders) {
await provider.onApplicationBootstrap();
}
}

@override
16 changes: 16 additions & 0 deletions packages/serinus/lib/src/mixins/provider_mixins.dart
Original file line number Diff line number Diff line change
@@ -15,3 +15,19 @@ mixin OnApplicationShutdown on Provider {
/// The [onApplicationShutdown] method is called in the providers when the application is shutting down.
Future<void> onApplicationShutdown();
}

/// The mixin [OnApplicationReady] is used to define the method [onApplicationReady].
///
/// The method [onApplicationReady] is called in the providers after the [Application.serve] method has been executed.
mixin OnApplicationReady on Provider {
/// The [onApplicationReady] method is called in the providers after the [Application.serve] method has been executed.
Future<void> onApplicationReady();
}

/// The mixin [OnApplicationBootstrap] is used to define the method [onApplicationBootstrap].
///
/// The method [onApplicationBootstrap] is called in the providers after the [finalize] method of the ModulesContainer has been executed.
mixin OnApplicationBootstrap on Provider {
/// The [onApplicationBootstrap] method is called in the providers after the [finalize] method of the ModulesContainer has been executed.
Future<void> onApplicationBootstrap();
}

0 comments on commit 6f1dd5b

Please sign in to comment.