Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
escamoteur committed May 20, 2018
0 parents commit de04750
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
.dart_tool/

.packages
.pub/

build/
ios/.generated/
ios/Flutter/Generated.xcconfig
ios/Runner/GeneratedPluginRegistrant.*
19 changes: 19 additions & 0 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart"
}
]
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
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.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# get_it

A new flutter package project.

## Getting Started

For help getting started with Flutter, view our online [documentation](https://flutter.io/).

For help on editing package code, view the [documentation](https://flutter.io/developing-packages/).
19 changes: 19 additions & 0 deletions get_it.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart Packages" level="project" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
</component>
</module>
69 changes: 69 additions & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
library get_it;

typedef FactoryFunc = Object Function();

/// A Calculator.
class GetIt {
static Map<Type, _ServiceFactory> _factories = new Map<Type, _ServiceFactory>();

static T get<T>() {
var object = _factories[T];
if (object == null) {
throw new Exception("Object of type ${T.toString()} is not registered inside GetIt");
}
return object.getObject() as T;
}

static void register<T>(FactoryFunc func) {
var factory = new _ServiceFactory();
factory.create = func;
factory.type = ServiceFactoryType.alwaysNew;
_factories[T] = factory;
}

static void registerLazySingleton<T>(FactoryFunc func) {
var factory = new _ServiceFactory();
factory.create = func;
factory.type = ServiceFactoryType.lazy;
_factories[T] = factory;
}

static void registerConstant<T>(T instance) {
var factory = new _ServiceFactory();
factory.instance = instance;
factory.type = ServiceFactoryType.constant;
_factories[T] = factory;
}
}

enum ServiceFactoryType { alwaysNew, constant, lazy }

class _ServiceFactory {
ServiceFactoryType type;

FactoryFunc create;
Object instance;

T getObject<T>() {
try {
switch (type) {
case ServiceFactoryType.alwaysNew:
return create() as T;
break;
case ServiceFactoryType.constant:
return instance as T;
break;
case ServiceFactoryType.lazy:
if (instance == null) {
instance = create();
}
return instance as T;
break;
}
} catch (e, s) {
print("Error while creating ${T.toString()}");
print('Stack trace:\n $s');
rethrow;
}
}
}
Loading

0 comments on commit de04750

Please sign in to comment.