mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 13:47:24 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
643a830d2d | ||
|
|
c44abaaedb | ||
|
|
e2cc712840 | ||
|
|
c49c9012ac | ||
|
|
4cb210d0c2 | ||
|
|
8f2ae95b8e |
1
AUTHORS.md
Normal file
1
AUTHORS.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
0.1.2+1 Fixed initializtaion error
|
||||||
|
|
||||||
|
---
|
||||||
0.1.2 Fixed warnings in code
|
0.1.2 Fixed warnings in code
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -85,8 +85,7 @@ Example:
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
import 'package:cherrypick/module.dart';
|
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -85,8 +85,7 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
import 'package:cherrypick/module.dart';
|
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:cherrypick/cherrypick.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
|
||||||
import 'package:cherrypick/module.dart';
|
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
name: example
|
name: example
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
author: "Sergey Penkovsky <sergey.penkovsky@gmail.com>"
|
|
||||||
homepage: localhost
|
homepage: localhost
|
||||||
publish_to: none
|
publish_to: none
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,9 @@
|
|||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
library dart_di;
|
library cherrypick;
|
||||||
|
|
||||||
export 'package:cherrypick/scope.dart';
|
export 'package:cherrypick/src/binding.dart';
|
||||||
export 'package:cherrypick/module.dart';
|
export 'package:cherrypick/src/helper.dart';
|
||||||
export 'package:cherrypick/binding.dart';
|
export 'package:cherrypick/src/module.dart';
|
||||||
export 'package:cherrypick/di.dart';
|
export 'package:cherrypick/src/scope.dart';
|
||||||
37
lib/di.dart
37
lib/di.dart
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -96,9 +96,6 @@ class Binding<T> {
|
|||||||
///
|
///
|
||||||
/// return [Binding]
|
/// return [Binding]
|
||||||
Binding<T> singeltone() {
|
Binding<T> singeltone() {
|
||||||
if (_mode == Mode.PROVIDER_INSTANCE) {
|
|
||||||
_instance = _provider?.call();
|
|
||||||
}
|
|
||||||
_isSingeltone = true;
|
_isSingeltone = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -113,5 +110,11 @@ class Binding<T> {
|
|||||||
/// ENG: Resolve instance.
|
/// ENG: Resolve instance.
|
||||||
///
|
///
|
||||||
/// return [T]
|
/// return [T]
|
||||||
T? get provider => _provider?.call();
|
T? get provider {
|
||||||
|
if (_isSingeltone) {
|
||||||
|
_instance ??= _provider?.call();
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
return _provider?.call();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,8 +10,7 @@
|
|||||||
/// See the License for the specific language governing permissions and
|
/// See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
import 'package:cherrypick/src/scope.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
|
||||||
|
|
||||||
abstract class Factory<T> {
|
abstract class Factory<T> {
|
||||||
T createInstance(Scope scope);
|
T createInstance(Scope scope);
|
||||||
105
lib/src/helper.dart
Normal file
105
lib/src/helper.dart
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,11 +10,10 @@
|
|||||||
/// See the License for the specific language governing permissions and
|
/// See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:cherrypick/binding.dart';
|
import 'package:cherrypick/src/binding.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
import 'package:cherrypick/src/scope.dart';
|
||||||
|
|
||||||
/// RU: Класс Module является основой для пользовательских модулей.
|
/// RU: Класс Module является основой для пользовательских модулей.
|
||||||
/// Этот класс нужен для инициализации [Scope].
|
/// Этот класс нужен для инициализации [Scope].
|
||||||
@@ -10,11 +10,10 @@
|
|||||||
/// See the License for the specific language governing permissions and
|
/// See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:cherrypick/binding.dart';
|
import 'package:cherrypick/src/binding.dart';
|
||||||
import 'package:cherrypick/module.dart';
|
import 'package:cherrypick/src/module.dart';
|
||||||
|
|
||||||
Scope openRootScope() => Scope(null);
|
Scope openRootScope() => Scope(null);
|
||||||
|
|
||||||
@@ -112,9 +111,7 @@ class Scope {
|
|||||||
case Mode.INSTANCE:
|
case Mode.INSTANCE:
|
||||||
return binding.instance;
|
return binding.instance;
|
||||||
case Mode.PROVIDER_INSTANCE:
|
case Mode.PROVIDER_INSTANCE:
|
||||||
return binding.isSingeltone
|
return binding.provider;
|
||||||
? binding.instance
|
|
||||||
: binding.provider;
|
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: cherrypick
|
name: cherrypick
|
||||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
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
|
homepage: https://github.com/pese-git/cherrypick
|
||||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||||
repository: https://github.com/pese-git/cherrypick
|
repository: https://github.com/pese-git/cherrypick
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import 'package:cherrypick/binding.dart';
|
import 'package:cherrypick/src/binding.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import 'package:cherrypick/module.dart';
|
import 'package:cherrypick/src/module.dart';
|
||||||
import 'package:cherrypick/scope.dart';
|
import 'package:cherrypick/src/scope.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Reference in New Issue
Block a user