mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 02:19:08 +02:00
Broke up arcane service into separate files
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -40,7 +40,7 @@ library;
|
||||
export "package:arcane_framework/src/arcane.dart";
|
||||
export "package:arcane_framework/src/arcane_app.dart";
|
||||
export "package:arcane_framework/src/providers/environment_provider.dart";
|
||||
export "package:arcane_framework/src/providers/service_provider.dart";
|
||||
export "package:arcane_framework/src/providers/service/arcane_service.dart";
|
||||
export "package:arcane_framework/src/services/authentication/authentication_service.dart";
|
||||
export "package:arcane_framework/src/services/feature_flags/feature_flags_service.dart";
|
||||
export "package:arcane_framework/src/services/logging/logging_service.dart";
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import "package:arcane_framework/arcane_framework.dart";
|
||||
import "package:collection/collection.dart";
|
||||
import "package:flutter/widgets.dart";
|
||||
|
||||
part "service_provider.dart";
|
||||
part "service_provider_extensions.dart";
|
||||
|
||||
/// An abstract class representing a service in the Arcane architecture.
|
||||
///
|
||||
/// Classes that extend `ArcaneService` can use `ChangeNotifier` functionality
|
||||
/// to notify listeners of changes. Services are typically registered in
|
||||
/// `ArcaneServiceProvider` and can be accessed using the `service`
|
||||
/// method on `BuildContext`.
|
||||
abstract class ArcaneService with ChangeNotifier {
|
||||
/// Retrieves a service of the specified type from the context.
|
||||
///
|
||||
/// Returns null if no service of type `T` is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = ArcaneService.ofType<MyService>(context);
|
||||
/// ```
|
||||
static T? ofType<T extends ArcaneService>(BuildContext context) =>
|
||||
context.service<T>();
|
||||
|
||||
/// Retrieves a service of the specified type from the context.
|
||||
///
|
||||
/// Throws an assertion error if no service of type `T` is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = ArcaneService.requiredOfType<MyService>(context);
|
||||
/// ```
|
||||
static T requiredOfType<T extends ArcaneService>(BuildContext context) =>
|
||||
context.requiredService<T>();
|
||||
}
|
||||
+1
-83
@@ -1,6 +1,4 @@
|
||||
import "package:arcane_framework/arcane_framework.dart";
|
||||
import "package:collection/collection.dart";
|
||||
import "package:flutter/widgets.dart";
|
||||
part of "arcane_service.dart";
|
||||
|
||||
/// A provider that makes a list of `ArcaneService` instances available to the widget tree.
|
||||
///
|
||||
@@ -122,83 +120,3 @@ class ArcaneServiceProvider
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An extension on `BuildContext` to provide easy access to `ArcaneService` instances
|
||||
/// that are registered in an `ArcaneServiceProvider`.
|
||||
///
|
||||
/// This extension provides methods for retrieving services in various ways.
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// final myService = context.service<MyService>();
|
||||
/// ```
|
||||
extension ServiceProviderExtension on BuildContext {
|
||||
/// Finds and returns the `ArcaneService` instance of type `T` that has been registered
|
||||
/// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`).
|
||||
///
|
||||
/// If no such service is found, it returns `null`.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = context.service<MyService>();
|
||||
/// ```
|
||||
T? service<T extends ArcaneService>() {
|
||||
// First check built-in services
|
||||
final builtInService = Arcane.services.whereType<T>().firstOrNull;
|
||||
if (builtInService != null) return builtInService;
|
||||
|
||||
// Then check provider
|
||||
return ArcaneServiceProvider.serviceOfType<T>(this);
|
||||
}
|
||||
|
||||
/// Finds and returns the `ArcaneService` instance of type `T` that has been registered
|
||||
/// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`).
|
||||
///
|
||||
/// Throws an assertion error if no service is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = context.requiredService<MyService>();
|
||||
/// ```
|
||||
T requiredService<T extends ArcaneService>() {
|
||||
final service = this.service<T>();
|
||||
assert(service != null, "No service of type $T found");
|
||||
return service!;
|
||||
}
|
||||
|
||||
/// Legacy method to maintain backward compatibility.
|
||||
///
|
||||
/// Prefer using `service<T>()` instead.
|
||||
@Deprecated("Use service<T>() instead")
|
||||
T? serviceOfType<T extends ArcaneService>() => service<T>();
|
||||
}
|
||||
|
||||
/// An abstract class representing a service in the Arcane architecture.
|
||||
///
|
||||
/// Classes that extend `ArcaneService` can use `ChangeNotifier` functionality
|
||||
/// to notify listeners of changes. Services are typically registered in
|
||||
/// `ArcaneServiceProvider` and can be accessed using the `service`
|
||||
/// method on `BuildContext`.
|
||||
abstract class ArcaneService with ChangeNotifier {
|
||||
/// Retrieves a service of the specified type from the context.
|
||||
///
|
||||
/// Returns null if no service of type `T` is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = ArcaneService.ofType<MyService>(context);
|
||||
/// ```
|
||||
static T? ofType<T extends ArcaneService>(BuildContext context) =>
|
||||
context.service<T>();
|
||||
|
||||
/// Retrieves a service of the specified type from the context.
|
||||
///
|
||||
/// Throws an assertion error if no service of type `T` is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = ArcaneService.requiredOfType<MyService>(context);
|
||||
/// ```
|
||||
static T requiredOfType<T extends ArcaneService>(BuildContext context) =>
|
||||
context.requiredService<T>();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
part of "arcane_service.dart";
|
||||
|
||||
/// An extension on `BuildContext` to provide easy access to `ArcaneService` instances
|
||||
/// that are registered in an `ArcaneServiceProvider`.
|
||||
///
|
||||
/// This extension provides methods for retrieving services in various ways.
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// final myService = context.service<MyService>();
|
||||
/// ```
|
||||
extension ServiceProviderExtension on BuildContext {
|
||||
/// Finds and returns the `ArcaneService` instance of type `T` that has been registered
|
||||
/// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`).
|
||||
///
|
||||
/// If no such service is found, it returns `null`.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = context.service<MyService>();
|
||||
/// ```
|
||||
T? service<T extends ArcaneService>() {
|
||||
// First check built-in services
|
||||
final builtInService = Arcane.services.whereType<T>().firstOrNull;
|
||||
if (builtInService != null) return builtInService;
|
||||
|
||||
// Then check provider
|
||||
return ArcaneServiceProvider.serviceOfType<T>(this);
|
||||
}
|
||||
|
||||
/// Finds and returns the `ArcaneService` instance of type `T` that has been registered
|
||||
/// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`).
|
||||
///
|
||||
/// Throws an assertion error if no service is found.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final myService = context.requiredService<MyService>();
|
||||
/// ```
|
||||
T requiredService<T extends ArcaneService>() {
|
||||
final service = this.service<T>();
|
||||
assert(service != null, "No service of type $T found");
|
||||
return service!;
|
||||
}
|
||||
|
||||
/// Legacy method to maintain backward compatibility.
|
||||
///
|
||||
/// Prefer using `service<T>()` instead.
|
||||
@Deprecated("Use service<T>() instead")
|
||||
T? serviceOfType<T extends ArcaneService>() => service<T>();
|
||||
}
|
||||
Reference in New Issue
Block a user