Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5f0d512
Showing
16 changed files
with
668 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
**/doc/api/ | ||
.dart_tool/ | ||
.flutter-plugins | ||
.packages | ||
.pub-cache/ | ||
.pub/ | ||
build/ | ||
|
||
# Android related | ||
**/android/**/gradle-wrapper.jar | ||
**/android/.gradle | ||
**/android/captures/ | ||
**/android/gradlew | ||
**/android/gradlew.bat | ||
**/android/local.properties | ||
**/android/**/GeneratedPluginRegistrant.java | ||
|
||
# iOS/XCode related | ||
**/ios/**/*.mode1v3 | ||
**/ios/**/*.mode2v3 | ||
**/ios/**/*.moved-aside | ||
**/ios/**/*.pbxuser | ||
**/ios/**/*.perspectivev3 | ||
**/ios/**/*sync/ | ||
**/ios/**/.sconsign.dblite | ||
**/ios/**/.tags* | ||
**/ios/**/.vagrant/ | ||
**/ios/**/DerivedData/ | ||
**/ios/**/Icon? | ||
**/ios/**/Pods/ | ||
**/ios/**/.symlinks/ | ||
**/ios/**/profile | ||
**/ios/**/xcuserdata | ||
**/ios/.generated/ | ||
**/ios/Flutter/App.framework | ||
**/ios/Flutter/Flutter.framework | ||
**/ios/Flutter/Flutter.podspec | ||
**/ios/Flutter/Generated.xcconfig | ||
**/ios/Flutter/app.flx | ||
**/ios/Flutter/app.zip | ||
**/ios/Flutter/flutter_assets/ | ||
**/ios/Flutter/flutter_export_environment.sh | ||
**/ios/ServiceDefinitions.json | ||
**/ios/Runner/GeneratedPluginRegistrant.* | ||
|
||
# Exceptions to above rules. | ||
!**/ios/**/default.mode1v3 | ||
!**/ios/**/default.mode2v3 | ||
!**/ios/**/default.pbxuser | ||
!**/ios/**/default.perspectivev3 | ||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 856a90e67c9284124d44d2be6c785bacd3a1c772 | ||
channel: beta | ||
|
||
project_type: package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## [0.0.1] - TODO: Add release date. | ||
|
||
* TODO: Describe initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: Add your license here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# daggerito | ||
|
||
A new Flutter package project. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Dart | ||
[package](https://flutter.dev/developing-packages/), | ||
a library module containing code that can be shared easily across | ||
multiple Flutter or Dart projects. | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
library daggerito; | ||
|
||
export 'package:daggerito/src/di/component.dart'; | ||
export 'package:daggerito/src/dependency_container.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'dependency_container.dart'; | ||
|
||
/// Signature for a builder which creates an object of type [T]. | ||
typedef T Factory<T>(DependencyContainer container); | ||
|
||
class Definition<T> { | ||
Definition.instance(this.object) | ||
: instanceBuilder = null, | ||
_oneTime = false; | ||
|
||
Definition.factory(this.instanceBuilder) : _oneTime = false; | ||
|
||
Definition.singleton(this.instanceBuilder) : _oneTime = true; | ||
|
||
final Factory<T> instanceBuilder; | ||
T object; | ||
bool _oneTime = false; | ||
|
||
T get(DependencyContainer container) { | ||
if (_oneTime && instanceBuilder != null) { | ||
object = instanceBuilder(container); | ||
_oneTime = false; | ||
} | ||
|
||
if (object != null) { | ||
return object; | ||
} | ||
|
||
if (instanceBuilder != null) { | ||
return instanceBuilder(container); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'definition.dart'; | ||
|
||
|
||
|
||
/// A simple service container. | ||
class DependencyContainer { | ||
/// Creates a scoped container. | ||
DependencyContainer() | ||
: namedProviders = Map<String, Map<Type, Definition<Object>>>(); | ||
|
||
final Map<String, Map<Type, Definition<Object>>> namedProviders; | ||
|
||
/// Whether ignoring assertion errors in the following cases: | ||
/// * if you register the same type under the same name a second time. | ||
/// * if you try to resolve or unregister a type that was not | ||
/// previously registered. | ||
/// | ||
/// Defaults to true. | ||
bool silent = true; | ||
|
||
/// Colaborators | ||
List<DependencyContainer> collaborators = []; | ||
/// Removes all instances and builders from the container. | ||
/// | ||
/// After this, the container is empty. | ||
void clear() { | ||
namedProviders.clear(); | ||
} | ||
|
||
void setProvider<T>(String name, Definition<T> provider) { | ||
assert( | ||
silent || | ||
(!namedProviders.containsKey(name) || | ||
!namedProviders[name].containsKey(T)), | ||
assertRegisterMessage<T>('already', name),); | ||
|
||
namedProviders.putIfAbsent(name, () => Map<Type, Definition<Object>>())[T] = | ||
provider; | ||
} | ||
|
||
String assertRegisterMessage<T>(String word, String name) { | ||
return 'The type $T was $word registered${name == null ? '' : ' for the name $name'}'; | ||
} | ||
} | ||
|
||
// | ||
|
||
extension DependencyContainerExtensions on DependencyContainer{ | ||
void collaborate(List<DependencyContainer> containers){ | ||
this.collaborators = [...this.collaborators, ...containers]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:daggerito/src/dependency_container.dart'; | ||
|
||
|
||
abstract class Component { | ||
DependencyContainer container(); | ||
} | ||
|
||
abstract class Module { | ||
void register(DependencyContainer container); | ||
} | ||
|
||
abstract class ChildComponent implements Component { | ||
final DependencyContainer _container; | ||
|
||
ChildComponent( | ||
Component parentComponent, { | ||
Module module, | ||
}) : _container = DependencyContainer() | ||
..collaborate( | ||
[parentComponent.container()], | ||
) { | ||
if (module != null) module.register(_container); | ||
} | ||
|
||
@override | ||
DependencyContainer container() => _container; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'dependency_container.dart'; | ||
import 'definition.dart'; | ||
|
||
extension DependencyContainerExtensions on DependencyContainer{ | ||
|
||
/// Registers an instance into the container. | ||
/// | ||
/// An instance of type [T] can be registered with a | ||
/// supertype [S] if specified. | ||
/// | ||
/// If [tag] is set, the instance will be registered under this name. | ||
/// To retrieve the same instance, the same name should be provided | ||
/// to [DependencyContainer.resolve]. | ||
void registerInstance<S, T extends S>( | ||
S instance, { | ||
String tag, | ||
}) { | ||
setProvider(tag, Definition<S>.instance(instance)); | ||
} | ||
|
||
/// Registers a factory into the container. | ||
/// | ||
/// A factory returning an object of type [T] can be registered with a | ||
/// supertype [S] if specified. | ||
/// | ||
/// If [tag] is set, the factory will be registered under this name. | ||
/// To retrieve the same factory, the same name should be provided | ||
/// to [DependencyContainer.resolve]. | ||
void register<S, T extends S>( | ||
Factory<S> factory, { | ||
String tag, | ||
}) { | ||
setProvider(tag, Definition<S>.factory(factory)); | ||
} | ||
|
||
/// Registers a factory that will be called only only when | ||
/// accessing it for the first time, into the container. | ||
/// | ||
/// A factory returning an object of type [T] can be registered with a | ||
/// supertype [S] if specified. | ||
/// | ||
/// If [tag] is set, the factory will be registered under this name. | ||
/// To retrieve the same factory, the same name should be provided | ||
/// to [DependencyContainer.resolve]. | ||
void registerSingleton<S, T extends S>( | ||
Factory<S> factory, { | ||
String tag, | ||
}) { | ||
setProvider(tag, Definition<S>.singleton(factory)); | ||
} | ||
|
||
/// Removes the entry previously registered for the type [T]. | ||
/// | ||
/// If [tag] is set, removes the one registered for that name. | ||
void unregister<T>([String tag]) { | ||
assert(silent || (namedProviders[tag]?.containsKey(T) ?? false), | ||
assertRegisterMessage<T>('not', tag)); | ||
namedProviders[tag]?.remove(T); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'dependency_container.dart'; | ||
import 'definition.dart'; | ||
|
||
extension DependencyContainerExtensions on DependencyContainer{ | ||
/// Attemps to resolve the type [T]. | ||
/// | ||
/// If [tag] is set, the instance or builder registered with this | ||
/// name will be get. | ||
/// | ||
/// See also: | ||
/// | ||
/// * [DependencyContainer.registerFactory] for register a builder function. | ||
/// * [DependencyContainer.registerInstance] for register an instance. | ||
T resolve<T>([String tag]) => _resolve([this,...this.collaborators],tag); | ||
|
||
T _resolve<T>(List<DependencyContainer> containers ,[String tag]) { | ||
if(containers.length == 0) | ||
return null; | ||
|
||
T value; | ||
|
||
containers.forEach((dc){ | ||
Map<Type, Definition<Object>> providers = dc.namedProviders[tag]; | ||
|
||
if(providers != null && value == null){ | ||
value = providers[T]?.get(this); | ||
} | ||
if(value == null){ | ||
value = _resolve(dc.collaborators, tag); | ||
} | ||
}); | ||
return value; | ||
|
||
} | ||
|
||
T call<T>([String tag]) => resolve<T>(tag); | ||
} |
Oops, something went wrong.