diff --git a/lib/src/di.dart b/lib/src/di.dart index a2aced8..62a67ef 100644 --- a/lib/src/di.dart +++ b/lib/src/di.dart @@ -1,7 +1,8 @@ class DI { static DI? _instance; - final Map _resources = {}; - static final Map _resourceCallbacks = {}; + final Map> _resources = {}; + static final Map> _resourceCallbacks = + {}; static DI get instance { _instance ??= DI(); @@ -14,37 +15,48 @@ class DI { String name, Function callback, { List injections = const [], + String context = 'utopia', }) { - _resourceCallbacks[name] = + _resourceCallbacks[context] ??= {}; + _resourceCallbacks[context]![name] = _ResourceCallback(name, injections, callback, reset: true); } - Map getAll(List names) { + Map getAll(List names, {String context = 'utopia'}) { final resources = {}; for (final name in names) { - resources[name] = get(name); + resources[name] = get(name, context: context); } return resources; } dynamic g(String name, {bool fresh = false}) => get(name, fresh: fresh); - dynamic get(String name, {bool fresh = false}) { - if (_resources[name] == null || + dynamic get(String name, {String context = 'utopia', bool fresh = false}) { + _resources[context] ??= {}; + _resourceCallbacks[context] ??= {}; + + final resources = _resources[context]!; + var resourceCallbacks = _resourceCallbacks[context]!; + if (resourceCallbacks.isEmpty || resourceCallbacks[name] == null) { + // use default context when not found in the context + resourceCallbacks = _resourceCallbacks['utopia'] ?? {}; + } + if (resources[name] == null || fresh || - (_resourceCallbacks[name]?.reset ?? true)) { - if (_resourceCallbacks[name] == null) { + (resourceCallbacks[name]?.reset ?? true)) { + if (resourceCallbacks[name] == null) { throw Exception('Failed to find resource: "$name"'); } - final params = getAll(_resourceCallbacks[name]!.injections); - _resources[name] = Function.apply( - _resourceCallbacks[name]!.callback, + final params = getAll(resourceCallbacks[name]!.injections); + resources[name] = Function.apply( + resourceCallbacks[name]!.callback, [...params.values], ); } - _resourceCallbacks[name] = _resourceCallbacks[name]!.copyWith(reset: false); - final resource = _resources[name]; + resourceCallbacks[name] = resourceCallbacks[name]!.copyWith(reset: false); + final resource = resources[name]; if (resource is T) { return resource; } diff --git a/pubspec.yaml b/pubspec.yaml index ffd7673..770dd11 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: utopia_di description: Light & Fast Dart Dependency Injection library. Easy to use and powerful for all kinds of needs, with no external dependencies. -version: 0.0.2 +version: 0.1.0 repository: https://github.com/utopia-dart/utopia_di environment: diff --git a/test/unit/di_test.dart b/test/unit/di_test.dart index 1360949..7c2bed7 100644 --- a/test/unit/di_test.dart +++ b/test/unit/di_test.dart @@ -13,7 +13,10 @@ void main() async { ); di.set('second', () => 'second'); - group('App', () { + di.set('second', () => 'another second', context: 'another'); + di.set('third', () => 'another third', context: 'another'); + + group('Utopia DI', () { test('resource', () async { expect(di.get('second'), 'second'); expect(di.get('first'), 'first-second'); @@ -23,5 +26,20 @@ void main() async { expect(di.get('rand'), resource); expect(di.get('rand'), resource); }); + + test('context resource', () async { + expect(di.get('second', context: 'another'), 'another second'); + expect(di.get('third', context: 'another'), 'another third'); + expect( + () => di.get('third'), + throwsA( + isA().having( + (error) => error.toString(), + '', + 'Exception: Failed to find resource: "third"', + ), + ), + ); + }); }); }