Skip to content

Commit

Permalink
fix: Store singleton instances in the root provider
Browse files Browse the repository at this point in the history
Previously each ServiceProvider had a map of service instances. If a singleton was created, the provider stored the
instance in the map and returned the instance the next time it is requested.

If you're working with enhanced providers (`ServiceProvider.enhance`), the singletons created in the EnhancedProvider
 wasn't stored in the root provider which causes that a singleton will be created again if it's resolved in the root
provider.

To solve this problem, the instances of the map is now a reference to the original instances map of the parent provider.
  • Loading branch information
devtronic committed Aug 14, 2024
1 parent 53bf65c commit 90a2039
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 3.5.1

### Singleton instances on enhanced providers
Previously each ServiceProvider had a map of service instances. If a singleton was created, the provider stored the
instance in the map and returned the instance the next time it is requested.

If you're working with enhanced providers (`ServiceProvider.enhance`), the singletons created in the EnhancedProvider
wasn't stored in the root provider which causes that a singleton will be created again if it's resolved in the root
provider.

To solve this problem, the instances of the map is now a reference to the original instances map of the parent provider.


## 3.5.0

Updated the version constraint of the analyzer package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ import '../../symbols.dart';
final serviceInstancesTemplate = cb.Field((f) {
f
..name = serviceInstances$.symbol
..modifier = cb.FieldModifier.final$
..assignment = cb.literalMap({}, typeT, dynamicT).code;
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ cb.Method enhanceTemplate(String providerClassName) {
initVar(enhancedV, cb.refer(providerClassName).newInstance([])),
enhancedV
.property(serviceInstances$.symbol!)
.property('addAll')
.call([serviceInstances$]).statement,
.assign(serviceInstances$)
.statement,
_addKnownServices(enhancedV),
enhancedV
.property(exposeMap$.symbol!)
Expand Down
14 changes: 14 additions & 0 deletions test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,18 @@ void main() {
enhanced.resolve<ServiceThatDependOnEnhancedService>().dependency.foo,
equals('bar'));
});

test('enhance should register singletons in the root provider', () {
if (serviceProvider is! EnhanceableProvider) {
fail('Service provider is not a EnhanceableProvider');
}

var enhanced1 = (serviceProvider as EnhanceableProvider).enhance();
expect(enhanced1.resolve<SingletonThatShouldBeRegisteredInRoot>().count,
equals(1));

var enhanced2 = (serviceProvider as EnhanceableProvider).enhance();
expect(enhanced2.resolve<SingletonThatShouldBeRegisteredInRoot>().count,
equals(1));
});
}
11 changes: 11 additions & 0 deletions test/third_party_dependency/lib/third_party_dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ class ServiceThatDependOnEnhancedService {
class ServiceOnlyProvidedInEnhanced {
final String foo = 'bar';
}

@Service()
class SingletonThatShouldBeRegisteredInRoot {
static var _count = 0;

int get count => _count;

SingletonThatShouldBeRegisteredInRoot() {
_count++;
}
}

0 comments on commit 90a2039

Please sign in to comment.