Skip to content

Commit

Permalink
Merge pull request #4 from utopia-dart/fix-context
Browse files Browse the repository at this point in the history
Fixes, docs and improvements
  • Loading branch information
lohanidamodar authored Mar 11, 2024
2 parents 3d0d3b7 + daef3b5 commit e399bae
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 47 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.2.0

- Improve library namings
- Fixes for reset methods
- Add doc comments
- Support context while resetting cached resources

## 0.1.0

- Support optional context while setting and getting dependencies
Expand Down
58 changes: 32 additions & 26 deletions lib/src/di.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
import 'resource_callback.dart';

/// Dependency injection
///
/// Simple and easy dependency injection
/// for your Dart applications.
///
/// Part of Utopia Dart ecosystem,
/// but can be used independently.
class DI {
static const defaultContext = 'utopia';
static DI? _instance;
final Map<String, Map<String, dynamic>> _resources = {};
static final Map<String, Map<String, _ResourceCallback>> _resourceCallbacks =
static final Map<String, Map<String, ResourceCallback>> _resourceCallbacks =
{};

/// Get singleton instance
static DI get instance {
_instance ??= DI();
return _instance!;
}

/// Get singleton instance
static DI get i => DI.instance;

/// Set a resource callback
void set(
String name,
Function callback, {
List<String> injections = const [],
String context = 'utopia',
String context = defaultContext,
}) {
_resourceCallbacks[context] ??= {};
_resourceCallbacks[context]![name] =
_ResourceCallback(name, injections, callback, reset: true);
ResourceCallback(name, injections, callback, reset: true);
}

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

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

dynamic get<T>(String name, {String context = 'utopia', bool fresh = false}) {
/// Get a resource
dynamic get<T>(String name,
{String context = defaultContext, 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'] ?? {};
resourceCallbacks = _resourceCallbacks[defaultContext] ?? {};
}
if (resources[name] == null ||
fresh ||
Expand All @@ -63,29 +81,17 @@ class DI {
throw Exception('Resource type doesn\'t match');
}

void reset() {
/// Reset cached resources
void resetResources([String? context]) {
if (context != null) {
(_resources[context] ?? {}).clear();
return;
}
_resources.clear();
}

void resetResources() {
/// Resets all the dependencies
void reset() {
_resourceCallbacks.clear();
}
}

class _ResourceCallback {
final String name;
final List<String> injections;
final Function callback;
final bool reset;

_ResourceCallback(
this.name,
this.injections,
this.callback, {
this.reset = false,
});

_ResourceCallback copyWith({bool? reset}) {
return _ResourceCallback(name, injections, callback, reset: reset ?? false);
}
}
42 changes: 23 additions & 19 deletions lib/src/hook.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
import 'param.dart';
import 'validators/validator.dart';

/// Utopia Hook, extended by various other
/// utopia libraries.
///
/// A hook provides an action to be executed
class Hook {
/// Description
String description = '';
List<String> _groups = [];
static int counter = 0;
static int _counter = 0;
final Map<String, Param> _params = {};
final List<String> _injections = [];
late int order;
late Function _action;

final List<String> _argsOrder = [];

/// Order of arguments
List<String> get argsOrder => _argsOrder;

/// Injections
List<String> get injections => _injections;

/// Parameters
Map<String, Param> get params => _params;

/// Get groups
List<String> getGroups() => _groups;

/// Get action
Function getAction() => _action;

Hook() {
Hook.counter++;
order = counter;
Hook._counter++;
order = _counter;
_action = () {};
}

/// Hooks action to be executed
/// when executing hook
Hook action(Function action) {
_action = action;
return this;
}

/// Set hook description
Hook desc(String description) {
this.description = description;
return this;
}

/// Set hook groups
Hook groups(List<String> groups) {
_groups = groups;
return this;
}

/// Inject dependencies
Hook inject(String injection) {
if (_injections.contains(injection)) {
throw Exception("Injection already declared for $injection");
Expand All @@ -48,6 +67,7 @@ class Hook {
return this;
}

/// Set hook param
Hook param({
required String key,
dynamic defaultValue,
Expand All @@ -66,19 +86,3 @@ class Hook {
return this;
}
}

class Param {
final dynamic defaultValue;
final Validator? validator;
final String description;
final dynamic value;
final bool optional;

Param({
required this.defaultValue,
required this.validator,
required this.description,
required this.value,
required this.optional,
});
}
18 changes: 18 additions & 0 deletions lib/src/param.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'validators/validator.dart';

/// Param
class Param {
final dynamic defaultValue;
final Validator? validator;
final String description;
final dynamic value;
final bool optional;

Param({
required this.defaultValue,
required this.validator,
required this.description,
required this.value,
required this.optional,
});
}
18 changes: 18 additions & 0 deletions lib/src/resource_callback.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Resource callback
class ResourceCallback {
final String name;
final List<String> injections;
final Function callback;
final bool reset;

ResourceCallback(
this.name,
this.injections,
this.callback, {
this.reset = false,
});

ResourceCallback copyWith({bool? reset}) {
return ResourceCallback(name, injections, callback, reset: reset ?? false);
}
}
8 changes: 8 additions & 0 deletions lib/src/validators/allow_list.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import 'validator.dart';

/// Allow list validator
class AllowList<T> extends Validator {
final List<T> _list;

/// List of allowed values
List<T> get list => _list;

AllowList(this._list, {bool strict = false});

/// Error description
@override
String getDescription() {
return 'Value must of one of (${_list.join(", ")})';
}

/// Type of list
@override
String getType() {
return T.toString();
}

/// Is array
@override
bool isArray() {
return false;
}

/// Is valid
/// Returns true if the given value
/// is valid or false otherwise
@override
bool isValid(value) {
if (value is List) return false;
Expand Down
3 changes: 2 additions & 1 deletion lib/utopia_di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/// More dartdocs go here.
library utopia_di;

export 'src/hook.dart';
export 'src/di.dart';
export 'src/hook.dart';
export 'src/param.dart';
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.1.0
version: 0.2.0
repository: https://github.com/utopia-dart/utopia_di

environment:
Expand Down

0 comments on commit e399bae

Please sign in to comment.