Showing
3 changed files
with
59 additions
and
34 deletions
| @@ -4,6 +4,7 @@ import 'dart:collection'; | @@ -4,6 +4,7 @@ import 'dart:collection'; | ||
| 4 | import 'package:get_core/get_core.dart'; | 4 | import 'package:get_core/get_core.dart'; | 
| 5 | 5 | ||
| 6 | import 'lifecycle.dart'; | 6 | import 'lifecycle.dart'; | 
| 7 | +import 'utils/secure_operations.dart'; | ||
| 7 | 8 | ||
| 8 | class GetInstance { | 9 | class GetInstance { | 
| 9 | factory GetInstance() => _getInstance ??= GetInstance._(); | 10 | factory GetInstance() => _getInstance ??= GetInstance._(); | 
| @@ -12,8 +13,6 @@ class GetInstance { | @@ -12,8 +13,6 @@ class GetInstance { | ||
| 12 | 13 | ||
| 13 | static GetInstance _getInstance; | 14 | static GetInstance _getInstance; | 
| 14 | 15 | ||
| 15 | - // static final config = Get(); | ||
| 16 | - | ||
| 17 | /// Holds references to every registered Instance when using | 16 | /// Holds references to every registered Instance when using | 
| 18 | /// [Get.put()] | 17 | /// [Get.put()] | 
| 19 | static final Map<String, _InstanceBuilderFactory> _singl = {}; | 18 | static final Map<String, _InstanceBuilderFactory> _singl = {}; | 
| @@ -227,26 +226,26 @@ class GetInstance { | @@ -227,26 +226,26 @@ class GetInstance { | ||
| 227 | return i; | 226 | return i; | 
| 228 | } | 227 | } | 
| 229 | 228 | ||
| 230 | - // S putOrFind<S>(S Function() dep, {String tag}) { | ||
| 231 | - // final key = _getKey(S, tag); | 229 | + S putOrFind<S>(InstanceBuilderCallback<S> dep, {String tag}) { | 
| 230 | + final key = _getKey(S, tag); | ||
| 232 | 231 | ||
| 233 | - // if (_singl.containsKey(key)) { | ||
| 234 | - // return _singl[key].getDependency() as S; | ||
| 235 | - // } else { | ||
| 236 | - // if (_factory.containsKey(key)) { | ||
| 237 | - // S _value = put<S>((_factory[key].builder() as S), tag: tag); | 232 | + if (_singl.containsKey(key)) { | 
| 233 | + return _singl[key].getDependency() as S; | ||
| 234 | + } else { | ||
| 235 | + if (_factory.containsKey(key)) { | ||
| 236 | + final _value = put<S>((_factory[key].builder() as S), tag: tag); | ||
| 238 | 237 | ||
| 239 | - // if (Get.smartManagement != SmartManagement.keepFactory) { | ||
| 240 | - // if (!_factory[key].fenix) { | ||
| 241 | - // _factory.remove(key); | ||
| 242 | - // } | ||
| 243 | - // } | ||
| 244 | - // return _value; | ||
| 245 | - // } | 238 | + if (Get.smartManagement != SmartManagement.keepFactory) { | 
| 239 | + if (!_factory[key].fenix) { | ||
| 240 | + _factory.remove(key); | ||
| 241 | + } | ||
| 242 | + } | ||
| 243 | + return _value; | ||
| 244 | + } | ||
| 246 | 245 | ||
| 247 | - // return GetInstance().put(dep(), tag: tag); | ||
| 248 | - // } | ||
| 249 | - // } | 246 | + return GetInstance().put(dep(), tag: tag); | 
| 247 | + } | ||
| 248 | + } | ||
| 250 | 249 | ||
| 251 | /// Finds the registered type <[S]> (or [tag]) | 250 | /// Finds the registered type <[S]> (or [tag]) | 
| 252 | /// In case of using Get.[create] to register a type <[S]> or [tag], | 251 | /// In case of using Get.[create] to register a type <[S]> or [tag], | 
| @@ -307,15 +306,7 @@ class GetInstance { | @@ -307,15 +306,7 @@ class GetInstance { | ||
| 307 | return true; | 306 | return true; | 
| 308 | } | 307 | } | 
| 309 | 308 | ||
| 310 | -// Future<bool> delete<S>({ | ||
| 311 | -// String tag, | ||
| 312 | -// String key, | ||
| 313 | -// bool force = false, | ||
| 314 | -// }) async { | ||
| 315 | -// final s = await queue | ||
| 316 | -// .add<bool>(() async => dele<S>(tag: tag, key: key, force: force)); | ||
| 317 | -// return s; | ||
| 318 | -// } | 309 | + static final GetQueue _queue = GetQueue(); | 
| 319 | 310 | ||
| 320 | /// Delete registered Class Instance [S] (or [tag]) and, closes any open | 311 | /// Delete registered Class Instance [S] (or [tag]) and, closes any open | 
| 321 | /// controllers [DisposableInterface], cleans up the memory | 312 | /// controllers [DisposableInterface], cleans up the memory | 
| @@ -334,6 +325,12 @@ class GetInstance { | @@ -334,6 +325,12 @@ class GetInstance { | ||
| 334 | /// the Instance. **don't use** it unless you know what you are doing. | 325 | /// the Instance. **don't use** it unless you know what you are doing. | 
| 335 | /// - [force] Will delete an Instance even if marked as [permanent]. | 326 | /// - [force] Will delete an Instance even if marked as [permanent]. | 
| 336 | Future<bool> delete<S>({String tag, String key, bool force = false}) async { | 327 | Future<bool> delete<S>({String tag, String key, bool force = false}) async { | 
| 328 | + return _queue.secure<bool>(() { | ||
| 329 | + return _delete<S>(tag: tag, key: key, force: force); | ||
| 330 | + }); | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + Future<bool> _delete<S>({String tag, String key, bool force = false}) async { | ||
| 337 | final newKey = key ?? _getKey(S, tag); | 334 | final newKey = key ?? _getKey(S, tag); | 
| 338 | 335 | ||
| 339 | if (!_singl.containsKey(newKey)) { | 336 | if (!_singl.containsKey(newKey)) { | 
| 1 | +import 'dart:async'; | ||
| 2 | + | ||
| 3 | +class GetQueue { | ||
| 4 | + final List<_Item> _queue = []; | ||
| 5 | + bool _active = false; | ||
| 6 | + | ||
| 7 | + void _check() async { | ||
| 8 | + if (!_active && _queue.isNotEmpty) { | ||
| 9 | + _active = true; | ||
| 10 | + var item = _queue.removeAt(0); | ||
| 11 | + try { | ||
| 12 | + item.completer.complete(await item.job()); | ||
| 13 | + } on Exception catch (e) { | ||
| 14 | + item.completer.completeError(e); | ||
| 15 | + } | ||
| 16 | + _active = false; | ||
| 17 | + _check(); | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + Future<T> secure<T>(Function job) { | ||
| 22 | + var completer = Completer<T>(); | ||
| 23 | + _queue.add(_Item<T>(completer, job)); | ||
| 24 | + _check(); | ||
| 25 | + return completer.future; | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +class _Item<T> { | ||
| 30 | + final Completer<T> completer; | ||
| 31 | + final Function job; | ||
| 32 | + | ||
| 33 | + _Item(this.completer, this.job); | ||
| 34 | +} | 
- 
Please register or login to post a comment