Skip to content

Commit

Permalink
Merge pull request #2 from utopia-dart/feat-refactor
Browse files Browse the repository at this point in the history
Feat refactor
  • Loading branch information
lohanidamodar committed Sep 23, 2023
2 parents 999d392 + 7f54d29 commit daa7d03
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.2

- Refactor methods

## 0.0.1

- Initial version.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ void main() {

## Copyright and license

The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
The MIT License (MIT) [https://www.opensource.org/licenses/mit-license.php](https://www.opensource.org/licenses/mit-license.php)
12 changes: 6 additions & 6 deletions example/utopia_di_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import 'package:utopia_di/utopia_di.dart';

void main() {
final di = DI.i;
di.setResource('resource1', () => 'this is resource 1');
di.setResource('number1', () => 10);
di.setResource(
di.set('resource1', () => 'this is resource 1');
di.set('number1', () => 10);
di.set(
'dependentResource',
(String resource1, int number1) => '$resource1 and $number1',
injections: ['resource1', 'number1'],
);

print(di.getResource('resource1'));
print(di.getResource('number1'));
print(di.getResource('dependentResource'));
print(di.get('resource1'));
print(di.get('number1'));
print(di.get('dependentResource'));
}
12 changes: 7 additions & 5 deletions lib/src/di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DI {

static DI get i => DI.instance;

void setResource(
void set(
String name,
Function callback, {
List<String> injections = const [],
Expand All @@ -19,23 +19,25 @@ class DI {
_ResourceCallback(name, injections, callback, reset: true);
}

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

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

dynamic get(String name, {bool fresh = false}) {
if (_resources[name] == null ||
fresh ||
(_resourceCallbacks[name]?.reset ?? true)) {
if (_resourceCallbacks[name] == null) {
throw Exception('Failed to find resource: "$name"');
}

final params = getResources(_resourceCallbacks[name]!.injections);
final params = getAll(_resourceCallbacks[name]!.injections);
_resources[name] = Function.apply(
_resourceCallbacks[name]!.callback,
[...params.values],
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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.1
version: 0.0.2
repository: https://github.com/utopia-dart/utopia_di

environment:
sdk: '>=2.19.2 <3.0.0'

dev_dependencies:
lints: ^2.0.0
test: ^1.21.0
lints: ^2.1.1
test: ^1.24.6
18 changes: 9 additions & 9 deletions test/unit/di_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import 'package:utopia_di/utopia_di.dart';

void main() async {
final di = DI();
di.setResource('rand', () => Random().nextInt(100));
di.setResource(
di.set('rand', () => Random().nextInt(100));
di.set(
'first',
(String second) => 'first-$second',
injections: ['second'],
);
di.setResource('second', () => 'second');
di.set('second', () => 'second');

group('App', () {
test('resource', () async {
expect(di.getResource('second'), 'second');
expect(di.getResource('first'), 'first-second');
final resource = di.getResource('rand');
expect(di.get('second'), 'second');
expect(di.get('first'), 'first-second');
final resource = di.get('rand');
assert(resource != null);
expect(di.getResource('rand'), resource);
expect(di.getResource('rand'), resource);
expect(di.getResource('rand'), resource);
expect(di.get('rand'), resource);
expect(di.get('rand'), resource);
expect(di.get('rand'), resource);
});
});
}

0 comments on commit daa7d03

Please sign in to comment.