2025-08-06 14:44:12 +03:00
|
|
|
import 'package:cherrypick/cherrypick.dart';
|
2025-08-06 16:21:31 +03:00
|
|
|
import 'di_adapter.dart';
|
2025-08-07 14:11:29 +03:00
|
|
|
import '../scenarios/universal_chain_module.dart';
|
2025-08-06 14:44:12 +03:00
|
|
|
|
2025-08-07 13:44:39 +03:00
|
|
|
class CherrypickDIAdapter extends DIAdapter<Scope> {
|
2025-08-06 14:44:12 +03:00
|
|
|
Scope? _scope;
|
2025-08-07 13:44:39 +03:00
|
|
|
final bool _isSubScope;
|
|
|
|
|
|
|
|
|
|
CherrypickDIAdapter([Scope? scope, this._isSubScope = false]) {
|
|
|
|
|
_scope = scope;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 14:44:12 +03:00
|
|
|
@override
|
2025-08-07 13:44:39 +03:00
|
|
|
void setupDependencies(void Function(Scope container) registration) {
|
|
|
|
|
_scope ??= CherryPick.openRootScope();
|
2025-08-07 08:28:23 +03:00
|
|
|
registration(_scope!);
|
2025-08-06 14:44:12 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 14:11:29 +03:00
|
|
|
@override
|
|
|
|
|
Registration<Scope> universalRegistration<S extends Enum>({
|
|
|
|
|
required S scenario,
|
|
|
|
|
required int chainCount,
|
|
|
|
|
required int nestingDepth,
|
|
|
|
|
required UniversalBindingMode bindingMode,
|
|
|
|
|
}) {
|
|
|
|
|
if (scenario is UniversalScenario) {
|
|
|
|
|
return (scope) {
|
|
|
|
|
scope.installModules([
|
|
|
|
|
UniversalChainModule(
|
|
|
|
|
chainCount: chainCount,
|
|
|
|
|
nestingDepth: nestingDepth,
|
|
|
|
|
bindingMode: bindingMode,
|
|
|
|
|
scenario: scenario,
|
|
|
|
|
),
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
throw UnsupportedError('Scenario $scenario not supported by CherrypickDIAdapter');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 14:44:12 +03:00
|
|
|
@override
|
2025-08-07 09:15:26 +03:00
|
|
|
T resolve<T extends Object>({String? named}) =>
|
2025-08-07 08:28:23 +03:00
|
|
|
named == null ? _scope!.resolve<T>() : _scope!.resolve<T>(named: named);
|
2025-08-06 14:44:12 +03:00
|
|
|
|
|
|
|
|
@override
|
2025-08-07 09:15:26 +03:00
|
|
|
Future<T> resolveAsync<T extends Object>({String? named}) async =>
|
2025-08-07 08:28:23 +03:00
|
|
|
named == null ? await _scope!.resolveAsync<T>() : await _scope!.resolveAsync<T>(named: named);
|
2025-08-06 14:44:12 +03:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void teardown() {
|
2025-08-07 13:44:39 +03:00
|
|
|
if (!_isSubScope) {
|
|
|
|
|
CherryPick.closeRootScope();
|
|
|
|
|
_scope = null;
|
|
|
|
|
}
|
|
|
|
|
// SubScope teardown не требуется
|
2025-08-06 14:44:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
CherrypickDIAdapter openSubScope(String name) {
|
2025-08-07 13:44:39 +03:00
|
|
|
return CherrypickDIAdapter(_scope!.openSubScope(name), true);
|
2025-08-06 14:44:12 +03:00
|
|
|
}
|
2025-08-07 09:15:26 +03:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> waitForAsyncReady() async {}
|
2025-08-06 14:44:12 +03:00
|
|
|
}
|