Compare commits

..

6 Commits
0.1.2 ... 1.0.0

Author SHA1 Message Date
Sergey Penkovsky
643a830d2d changed package version 2021-10-20 10:18:46 +03:00
Sergey Penkovsky
c44abaaedb added experimental api 2021-10-20 10:17:48 +03:00
Sergey Penkovsky
e2cc712840 updated doc 2021-10-20 09:17:10 +03:00
Sergey Penkovsky
c49c9012ac refactored code 2021-10-20 09:15:51 +03:00
Sergey Penkovsky
4cb210d0c2 changed build version and updated changelog 2021-04-30 16:43:42 +03:00
Sergey Penkovsky
8f2ae95b8e fixed initialization error for singeltone provider 2021-04-30 16:42:39 +03:00
16 changed files with 135 additions and 68 deletions

1
AUTHORS.md Normal file
View File

@@ -0,0 +1 @@
Sergey Penkovsky <sergey.penkovsky@gmail.com>

View File

@@ -1,6 +1,9 @@
# Changelog
0.1.2+1 Fixed initializtaion error
---
0.1.2 Fixed warnings in code
---

View File

@@ -85,8 +85,7 @@ Example:
```dart
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/module.dart';
import 'package:cherrypick/cherrypick.dart';
class AppModule extends Module {
@override

View File

@@ -85,8 +85,7 @@ Scope - это контейнер, который хранит все дерев
```dart
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/module.dart';
import 'package:cherrypick/cherrypick.dart';
class AppModule extends Module {
@override

View File

@@ -1,7 +1,7 @@
import 'dart:async';
import 'package:cherrypick/cherrypick.dart';
import 'package:meta/meta.dart';
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/module.dart';
class AppModule extends Module {
@override

View File

@@ -1,6 +1,5 @@
name: example
version: 1.0.0
author: "Sergey Penkovsky <sergey.penkovsky@gmail.com>"
homepage: localhost
publish_to: none

View File

@@ -11,9 +11,9 @@
/// limitations under the License.
///
library dart_di;
library cherrypick;
export 'package:cherrypick/scope.dart';
export 'package:cherrypick/module.dart';
export 'package:cherrypick/binding.dart';
export 'package:cherrypick/di.dart';
export 'package:cherrypick/src/binding.dart';
export 'package:cherrypick/src/helper.dart';
export 'package:cherrypick/src/module.dart';
export 'package:cherrypick/src/scope.dart';

View File

@@ -1,37 +0,0 @@
///
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// http://www.apache.org/licenses/LICENSE-2.0
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import 'package:cherrypick/scope.dart';
Scope? _rootScope;
class CherryPick {
/// RU: Метод открывает главный [Scope].
/// ENG: The method opens the main [Scope].
///
/// return
static Scope openRootScope() {
_rootScope ??= Scope(null);
return _rootScope!;
}
/// RU: Метод закрывает главный [Scope].
/// ENG: The method close the main [Scope].
///
///
static void closeRootScope() {
if (_rootScope != null) {
_rootScope = null;
}
}
}

View File

@@ -96,9 +96,6 @@ class Binding<T> {
///
/// return [Binding]
Binding<T> singeltone() {
if (_mode == Mode.PROVIDER_INSTANCE) {
_instance = _provider?.call();
}
_isSingeltone = true;
return this;
}
@@ -113,5 +110,11 @@ class Binding<T> {
/// ENG: Resolve instance.
///
/// return [T]
T? get provider => _provider?.call();
T? get provider {
if (_isSingeltone) {
_instance ??= _provider?.call();
return _instance;
}
return _provider?.call();
}
}

View File

@@ -10,8 +10,7 @@
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/src/scope.dart';
abstract class Factory<T> {
T createInstance(Scope scope);

105
lib/src/helper.dart Normal file
View File

@@ -0,0 +1,105 @@
///
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// http://www.apache.org/licenses/LICENSE-2.0
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import 'package:cherrypick/src/scope.dart';
import 'package:meta/meta.dart';
Scope? _rootScope;
class CherryPick {
/// RU: Метод открывает главный [Scope].
/// ENG: The method opens the main [Scope].
///
/// return
static Scope openRootScope() {
_rootScope ??= Scope(null);
return _rootScope!;
}
/// RU: Метод закрывает главный [Scope].
/// ENG: The method close the main [Scope].
///
///
static void closeRootScope() {
if (_rootScope != null) {
_rootScope = null;
}
}
/// RU: Метод открывает дочерний [Scope].
/// ENG: The method open the child [Scope].
///
/// Дочерний [Scope] открывается с [scopeName]
/// Child [Scope] open with [scopeName]
///
/// Example:
/// ```
/// final String scopeName = 'firstScope.secondScope';
/// final subScope = CherryPick.openScope(scopeName);
/// ```
///
///
@experimental
static Scope openScope({String scopeName = '', String separator = '.'}) {
if (scopeName.isEmpty) {
return openRootScope();
}
final nameParts = scopeName.split(separator);
if (nameParts.isEmpty) {
throw Exception('Can not open sub scope because scopeName can not split');
}
return nameParts.fold(
openRootScope(),
(Scope previousValue, String element) =>
previousValue.openSubScope(element));
}
/// RU: Метод открывает дочерний [Scope].
/// ENG: The method open the child [Scope].
///
/// Дочерний [Scope] открывается с [scopeName]
/// Child [Scope] open with [scopeName]
///
/// Example:
/// ```
/// final String scopeName = 'firstScope.secondScope';
/// final subScope = CherryPick.closeScope(scopeName);
/// ```
///
///
@experimental
static void closeScope({String scopeName = '', String separator = '.'}) {
if (scopeName.isEmpty) {
closeRootScope();
}
final nameParts = scopeName.split(separator);
if (nameParts.isEmpty) {
throw Exception(
'Can not close sub scope because scopeName can not split');
}
if (nameParts.length > 1) {
final lastPart = nameParts.removeLast();
final scope = nameParts.fold(
openRootScope(),
(Scope previousValue, String element) =>
previousValue.openSubScope(element));
scope.closeSubScope(lastPart);
} else {
openRootScope().closeSubScope(nameParts[0]);
}
}
}

View File

@@ -10,11 +10,10 @@
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import 'dart:collection';
import 'package:cherrypick/binding.dart';
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/src/binding.dart';
import 'package:cherrypick/src/scope.dart';
/// RU: Класс Module является основой для пользовательских модулей.
/// Этот класс нужен для инициализации [Scope].

View File

@@ -10,11 +10,10 @@
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import 'dart:collection';
import 'package:cherrypick/binding.dart';
import 'package:cherrypick/module.dart';
import 'package:cherrypick/src/binding.dart';
import 'package:cherrypick/src/module.dart';
Scope openRootScope() => Scope(null);
@@ -112,9 +111,7 @@ class Scope {
case Mode.INSTANCE:
return binding.instance;
case Mode.PROVIDER_INSTANCE:
return binding.isSingeltone
? binding.instance
: binding.provider;
return binding.provider;
default:
return null;
}

View File

@@ -1,6 +1,6 @@
name: cherrypick
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
version: 0.1.2
version: 1.0.0
homepage: https://github.com/pese-git/cherrypick
documentation: https://github.com/pese-git/cherrypick/wiki
repository: https://github.com/pese-git/cherrypick

View File

@@ -1,4 +1,4 @@
import 'package:cherrypick/binding.dart';
import 'package:cherrypick/src/binding.dart';
import 'package:test/test.dart';
void main() {

View File

@@ -1,5 +1,5 @@
import 'package:cherrypick/module.dart';
import 'package:cherrypick/scope.dart';
import 'package:cherrypick/src/module.dart';
import 'package:cherrypick/src/scope.dart';
import 'package:test/test.dart';
void main() {