From 3ef93bb3f93d40df8d74686525476117daf74b6a Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Tue, 29 Apr 2025 18:56:48 +0200 Subject: [PATCH] Renamed serviceInstances => registeredServices and added a removeService method Signed-off-by: Hans Kokx --- CHANGELOG.md | 4 +++- lib/src/providers/service_provider.dart | 25 +++++++++++++++++++---- test/providers/service_provider_test.dart | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef1806a..ee90f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,13 @@ - [NEW] Added a new `ArcaneServiceProvider.maybeOf(context)` getter which returns a nullable `ArcaneServiceProvider` instance. - [NEW] `ArcaneServiceProvider` now includes a `serviceOfType(context)` getter to retrieve a nullable registered service instance. -- [NEW] An `addService` method was added to `ArcaneServiceProvider` +- [NEW] An `addService` method was added to `ArcaneServiceProvider`. +- [NEW] A `removeService` method was added to `ArcaneServiceProvider`. - [NEW] A `setServices` method was added to `ArcaneServiceProvider`. Invoking this method with a list of `ArcaneService` instances will replace all existing services in the `ArcaneServiceProvider`. - [DEPRECATED] `context.serviceOfType` has been deprecated in favor of `context.service`. - [NEW] `context.requiredService` has been added to provide a mechanism for ensuring a particular service has been registered. - [NEW] Added `ArcaneService.ofType(context)` and `ArcaneService.requiredOfType(context)` locators, returning a nullable and non-nullable instance of a given service, respectively. +- [BREAKING] Renamed `serviceInstances` to `registeredServices`. ### Authentication Service (ArcaneAuth) diff --git a/lib/src/providers/service_provider.dart b/lib/src/providers/service_provider.dart index 33d105e..a7ad80c 100644 --- a/lib/src/providers/service_provider.dart +++ b/lib/src/providers/service_provider.dart @@ -22,7 +22,8 @@ import "package:flutter/widgets.dart"; class ArcaneServiceProvider extends InheritedNotifier>> { /// A list of `ArcaneService` instances available through the provider. - List get serviceInstances => notifier!.value; + List get registeredServices => + List.from(notifier?.value ?? []); /// Creates an `ArcaneServiceProvider` that provides [serviceInstances] to the widget tree. /// @@ -75,7 +76,7 @@ class ArcaneServiceProvider final provider = maybeOf(context); if (provider == null) return null; - return provider.serviceInstances.whereType().firstOrNull; + return provider.registeredServices.whereType().firstOrNull; } /// Updates the service instances in this provider. @@ -89,12 +90,12 @@ class ArcaneServiceProvider /// /// If a service of the same type already exists, it will be replaced. void addService(ArcaneService service) { - final int existingIndex = serviceInstances.indexWhere( + final int existingIndex = registeredServices.indexWhere( (s) => s.runtimeType == service.runtimeType, ); final List newList = - List.from(serviceInstances); + List.from(registeredServices); if (existingIndex >= 0) { newList[existingIndex] = service; @@ -104,6 +105,22 @@ class ArcaneServiceProvider notifier?.value = newList; } + + /// Removes all services of the specified type from the registry. + /// Returns true if any services were removed, false otherwise. + void removeService() { + final int existingIndex = registeredServices.indexWhere( + (s) => s.runtimeType == T, + ); + + if (existingIndex >= 0) { + final List newList = + List.from(registeredServices); + + newList.removeAt(existingIndex); + notifier?.value = newList; + } + } } /// An extension on `BuildContext` to provide easy access to `ArcaneService` instances diff --git a/test/providers/service_provider_test.dart b/test/providers/service_provider_test.dart index 4285f7b..bf2c4ca 100644 --- a/test/providers/service_provider_test.dart +++ b/test/providers/service_provider_test.dart @@ -20,7 +20,7 @@ void main() { child: Builder( builder: (context) { final provider = ArcaneServiceProvider.of(context); - expect(provider.serviceInstances, equals(testServices)); + expect(provider.registeredServices, equals(testServices)); return const SizedBox(); }, ),