mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-08-15 15:50:26 +02:00
v2.0.5: Add typed service lookup entrypoint and tests for Arcane.service
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -43,6 +43,9 @@ abstract class Arcane {
|
||||
/// Logger is not a service and is always the singleton.
|
||||
static ArcaneLogger get logger => ArcaneLogger.I;
|
||||
|
||||
/// Provides typed service lookups through `Arcane.service.ofType<T>(context)`.
|
||||
static const ArcaneServiceLookup service = ArcaneServiceLookup();
|
||||
|
||||
/// Provides access to the feature flags service instance registered in ArcaneApp, or the singleton if not present.
|
||||
static ArcaneFeatureFlagService get features =>
|
||||
services.whereType<ArcaneFeatureFlagService>().firstOrNull ??
|
||||
|
||||
@@ -144,3 +144,20 @@ class ArcaneServiceProvider
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed service lookup entrypoint for `Arcane.service`.
|
||||
class ArcaneServiceLookup {
|
||||
const ArcaneServiceLookup();
|
||||
|
||||
/// Returns a service of type `T` from the closest provider in [context].
|
||||
///
|
||||
/// Falls back to Arcane's built-in services when no provider instance exists.
|
||||
T? ofType<T extends ArcaneService>(BuildContext context) =>
|
||||
ArcaneService.ofType<T>(context);
|
||||
|
||||
/// Returns a service of type `T` from the closest provider in [context].
|
||||
///
|
||||
/// Throws an assertion error if no matching service is available.
|
||||
T requiredOfType<T extends ArcaneService>(BuildContext context) =>
|
||||
ArcaneService.requiredOfType<T>(context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user