Skip to content

Commit

Permalink
Merge pull request #3 from utopia-dart/feat-context
Browse files Browse the repository at this point in the history
Feat context support
  • Loading branch information
lohanidamodar committed Mar 10, 2024
2 parents 85d9f06 + a0d868a commit 4459ef1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
40 changes: 26 additions & 14 deletions lib/src/di.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class DI {
static DI? _instance;
final Map<String, dynamic> _resources = {};
static final Map<String, _ResourceCallback> _resourceCallbacks = {};
final Map<String, Map<String, dynamic>> _resources = {};
static final Map<String, Map<String, _ResourceCallback>> _resourceCallbacks =
{};

static DI get instance {
_instance ??= DI();
Expand All @@ -14,37 +15,48 @@ class DI {
String name,
Function callback, {
List<String> injections = const [],
String context = 'utopia',
}) {
_resourceCallbacks[name] =
_resourceCallbacks[context] ??= {};
_resourceCallbacks[context]![name] =
_ResourceCallback(name, injections, callback, reset: true);
}

Map<String, dynamic> getAll(List<String> names) {
Map<String, dynamic> getAll(List<String> names, {String context = 'utopia'}) {
final resources = <String, dynamic>{};
for (final name in names) {
resources[name] = get(name);
resources[name] = get(name, context: context);
}
return resources;
}

dynamic g<T>(String name, {bool fresh = false}) => get<T>(name, fresh: fresh);

dynamic get<T>(String name, {bool fresh = false}) {
if (_resources[name] == null ||
dynamic get<T>(String name, {String context = 'utopia', bool fresh = false}) {
_resources[context] ??= <String, dynamic>{};
_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;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
20 changes: 19 additions & 1 deletion test/unit/di_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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<Exception>().having(
(error) => error.toString(),
'',
'Exception: Failed to find resource: "third"',
),
),
);
});
});
}

0 comments on commit 4459ef1

Please sign in to comment.