From 588e95469bd51aaf771cf671298699ff994c729d Mon Sep 17 00:00:00 2001 From: Jonny Borges <35742643+jonataslaw@users.noreply.github.com> Date: Sat, 27 Jun 2020 21:26:19 -0300 Subject: [PATCH] Delete get_instance.dart --- lib/src/get_instance.dart | 252 -------------------------------------- 1 file changed, 252 deletions(-) delete mode 100644 lib/src/get_instance.dart diff --git a/lib/src/get_instance.dart b/lib/src/get_instance.dart deleted file mode 100644 index 779f10435..000000000 --- a/lib/src/get_instance.dart +++ /dev/null @@ -1,252 +0,0 @@ -import 'root/smart_management.dart'; -import 'rx/rx_interface.dart'; -import 'typedefs/typedefs.dart'; - -class GetConfig { - //////////// INSTANCE MANAGER - static Map _singl = {}; - static Map _factory = {}; - static Map routesKey = {}; - static SmartManagement smartManagement = SmartManagement.full; - static bool isLogEnable = true; - static String currentRoute; -} - -class GetInstance { - factory GetInstance() { - if (_getInstance == null) _getInstance = GetInstance._(); - return _getInstance; - } - GetInstance._(); - static GetInstance _getInstance; - - void lazyPut(FcBuilderFunc builder, {String tag}) { - String key = _getKey(S, tag); - GetConfig._factory.putIfAbsent(key, () => builder); - } - - Future putAsync(FcBuilderFuncAsync builder, - {String tag, bool permanent = false}) async { - return put(await builder(), tag: tag, permanent: permanent); - } - - /// Inject class on Get Instance Manager - S put( - S dependency, { - String tag, - bool permanent = false, - bool overrideAbstract = false, - FcBuilderFunc builder, - }) { - _insert( - isSingleton: true, - replace: overrideAbstract, - //?? (("$S" == "${dependency.runtimeType}") == false), - name: tag, - permanent: permanent, - builder: builder ?? (() => dependency)); - return find(tag: tag); - } - - /// Create a new instance from builder class - /// Example - /// create(() => Repl()); - /// Repl a = find(); - /// Repl b = find(); - /// print(a==b); (false) - void create( - FcBuilderFunc builder, { - String name, - }) { - _insert(isSingleton: false, name: name, builder: builder); - } - - void _insert({ - bool isSingleton, - String name, - bool replace = true, - bool permanent = false, - FcBuilderFunc builder, - }) { - assert(builder != null); - String key = _getKey(S, name); - if (replace) { - GetConfig._singl[key] = FcBuilder(isSingleton, builder, permanent); - } else { - GetConfig._singl.putIfAbsent( - key, () => FcBuilder(isSingleton, builder, permanent)); - } - } - - void removeDependencyByRoute(String routeName) async { - List keysToRemove = []; - GetConfig.routesKey.forEach((key, value) { - // if (value == routeName && value != null) { - if (value == routeName) { - keysToRemove.add(key); - } - }); - keysToRemove.forEach((element) async { - await delete(key: element); - }); - keysToRemove.forEach((element) { - GetConfig.routesKey?.remove(element); - }); - keysToRemove.clear(); - } - - bool isRouteDependecyNull({String name}) { - return (GetConfig.routesKey[_getKey(S, name)] == null); - } - - bool isDependencyInit({String name}) { - String key = _getKey(S, name); - return GetConfig.routesKey.containsKey(key); - } - - void registerRouteInstance({String tag}) { - GetConfig.routesKey - .putIfAbsent(_getKey(S, tag), () => GetConfig.currentRoute); - } - - S findByType(Type type, {String tag}) { - String key = _getKey(type, tag); - return GetConfig._singl[key].getSependency() as S; - } - - void initController({String tag}) { - String key = _getKey(S, tag); - final i = GetConfig._singl[key].getSependency(); - - if (i is DisposableInterface) { - i.onStart(); - if (GetConfig.isLogEnable) print('[GET] $key has been initialized'); - } - } - - /// Find a instance from required class - S find({String tag, FcBuilderFunc instance}) { - String key = _getKey(S, tag); - bool callInit = false; - if (isRegistred(tag: tag)) { - if (!isDependencyInit() && - GetConfig.smartManagement != SmartManagement.onlyBuilder) { - registerRouteInstance(tag: tag); - callInit = true; - } - - FcBuilder builder = GetConfig._singl[key] as FcBuilder; - if (builder == null) { - if (tag == null) { - throw "class ${S.toString()} is not register"; - } else { - throw "class ${S.toString()} with tag '$tag' is not register"; - } - } - if (callInit) { - initController(tag: tag); - } - - return GetConfig._singl[key].getSependency() as S; - } else { - if (!GetConfig._factory.containsKey(key)) - throw " $S not found. You need call put<$S>($S()) before"; - - if (GetConfig.isLogEnable) - print('[GET] $S instance was created at that time'); - S _value = put(GetConfig._factory[key].call() as S); - - if (!isDependencyInit() && - GetConfig.smartManagement != SmartManagement.onlyBuilder) { - registerRouteInstance(tag: tag); - callInit = true; - } - - if (GetConfig.smartManagement != SmartManagement.keepFactory) { - GetConfig._factory.remove(key); - } - - if (callInit) { - initController(tag: tag); - } - return _value; - } - } - - /// Remove dependency of [S] on dependency abstraction. For concrete class use delete - void remove({String tag}) { - String key = _getKey(S, tag); - FcBuilder builder = GetConfig._singl[key] as FcBuilder; - final i = builder.dependency; - - if (i is DisposableInterface) { - i.onClose(); - if (GetConfig.isLogEnable) print('[GET] onClose of $key called'); - } - if (builder != null) builder.dependency = null; - if (GetConfig._singl.containsKey(key)) { - print('error on remove $key'); - } else { - if (GetConfig.isLogEnable) print('[GET] $key removed from memory'); - } - } - - String _getKey(Type type, String name) { - return name == null ? type.toString() : type.toString() + name; - } - - bool reset({bool clearFactory = true, bool clearRouteBindings = true}) { - if (clearFactory) GetConfig._factory.clear(); - if (clearRouteBindings) GetConfig.routesKey.clear(); - GetConfig._singl.clear(); - return true; - } - - /// Delete class instance on [S] and clean memory - Future delete({String tag, String key}) async { - String newKey; - if (key == null) { - newKey = _getKey(S, tag); - } else { - newKey = key; - } - - if (!GetConfig._singl.containsKey(newKey)) { - print('Instance $newKey not found'); - return false; - } - - FcBuilder builder = GetConfig._singl[newKey] as FcBuilder; - if (builder.permanent) { - (key == null) - ? print( - '[GET] [$newKey] has been marked as permanent, SmartManagement is not authorized to delete it.') - : print( - '[GET] [$newKey] has been marked as permanent, SmartManagement is not authorized to delete it.'); - return false; - } - final i = builder.dependency; - - if (i is DisposableInterface) { - await i.onClose(); - if (GetConfig.isLogEnable) print('[GET] onClose of $newKey called'); - } - - GetConfig._singl.removeWhere((oldkey, value) => (oldkey == newKey)); - if (GetConfig._singl.containsKey(newKey)) { - print('[GET] error on remove object $newKey'); - } else { - if (GetConfig.isLogEnable) print('[GET] $newKey deleted from memory'); - } - // GetConfig.routesKey?.remove(key); - return true; - } - - /// check if instance is registred - bool isRegistred({String tag}) => - GetConfig._singl.containsKey(_getKey(S, tag)); - - /// check if instance is prepared - bool isPrepared({String tag}) => - GetConfig._factory.containsKey(_getKey(S, tag)); -}