added reload dependency, reloadAll, InstanceInfo and fix ui fail when navigate to same route
Showing
4 changed files
with
111 additions
and
15 deletions
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | import 'dart:collection'; | 2 | import 'dart:collection'; |
3 | 3 | ||
4 | +import 'package:flutter/foundation.dart'; | ||
5 | + | ||
4 | import '../../get_core/get_core.dart'; | 6 | import '../../get_core/get_core.dart'; |
5 | 7 | ||
6 | import 'lifecycle.dart'; | 8 | import 'lifecycle.dart'; |
7 | 9 | ||
10 | +class InstanceInfo { | ||
11 | + final bool isPermanent; | ||
12 | + final bool isSingleton; | ||
13 | + bool get isCreate => !isSingleton; | ||
14 | + final bool isRegistered; | ||
15 | + final bool isPrepared; | ||
16 | + final bool isInit; | ||
17 | + const InstanceInfo({ | ||
18 | + @required this.isPermanent, | ||
19 | + @required this.isSingleton, | ||
20 | + @required this.isRegistered, | ||
21 | + @required this.isPrepared, | ||
22 | + @required this.isInit, | ||
23 | + }); | ||
24 | +} | ||
25 | + | ||
8 | class GetInstance { | 26 | class GetInstance { |
9 | factory GetInstance() => _getInstance ??= GetInstance._(); | 27 | factory GetInstance() => _getInstance ??= GetInstance._(); |
10 | 28 | ||
@@ -156,8 +174,13 @@ class GetInstance { | @@ -156,8 +174,13 @@ class GetInstance { | ||
156 | final key = _getKey(S, name); | 174 | final key = _getKey(S, name); |
157 | _singl.putIfAbsent( | 175 | _singl.putIfAbsent( |
158 | key, | 176 | key, |
159 | - () => | ||
160 | - _InstanceBuilderFactory<S>(isSingleton, builder, permanent, false)); | 177 | + () => _InstanceBuilderFactory<S>( |
178 | + isSingleton, | ||
179 | + builder, | ||
180 | + permanent, | ||
181 | + false, | ||
182 | + ), | ||
183 | + ); | ||
161 | } | 184 | } |
162 | 185 | ||
163 | /// Clears from memory registered Instances associated with [routeName] when | 186 | /// Clears from memory registered Instances associated with [routeName] when |
@@ -226,6 +249,29 @@ class GetInstance { | @@ -226,6 +249,29 @@ class GetInstance { | ||
226 | _routesKey.putIfAbsent(_getKey(S, tag), () => Get.reference); | 249 | _routesKey.putIfAbsent(_getKey(S, tag), () => Get.reference); |
227 | } | 250 | } |
228 | 251 | ||
252 | + InstanceInfo getInstanceInfo<S>({String tag}) { | ||
253 | + final build = _getDependency<S>(tag: tag); | ||
254 | + | ||
255 | + return InstanceInfo( | ||
256 | + isPermanent: build?.permanent, | ||
257 | + isSingleton: build?.isSingleton, | ||
258 | + isRegistered: isRegistered<S>(tag: tag), | ||
259 | + isPrepared: !(build?.isInit ?? true), | ||
260 | + isInit: build?.isInit, | ||
261 | + ); | ||
262 | + } | ||
263 | + | ||
264 | + _InstanceBuilderFactory _getDependency<S>({String tag, String key}) { | ||
265 | + final newKey = key ?? _getKey(S, tag); | ||
266 | + | ||
267 | + if (!_singl.containsKey(newKey)) { | ||
268 | + Get.log('Instance "$newKey" is not registered.', isError: true); | ||
269 | + return null; | ||
270 | + } else { | ||
271 | + return _singl[newKey]; | ||
272 | + } | ||
273 | + } | ||
274 | + | ||
229 | /// Initializes the controller | 275 | /// Initializes the controller |
230 | S _startController<S>({String tag}) { | 276 | S _startController<S>({String tag}) { |
231 | final key = _getKey(S, tag); | 277 | final key = _getKey(S, tag); |
@@ -316,12 +362,6 @@ class GetInstance { | @@ -316,12 +362,6 @@ class GetInstance { | ||
316 | /// the Instance. **don't use** it unless you know what you are doing. | 362 | /// the Instance. **don't use** it unless you know what you are doing. |
317 | /// - [force] Will delete an Instance even if marked as [permanent]. | 363 | /// - [force] Will delete an Instance even if marked as [permanent]. |
318 | bool delete<S>({String tag, String key, bool force = false}) { | 364 | bool delete<S>({String tag, String key, bool force = false}) { |
319 | - // return _queue.secure<bool>(() { | ||
320 | - return _delete<S>(tag: tag, key: key, force: force); | ||
321 | - // }); | ||
322 | - } | ||
323 | - | ||
324 | - bool _delete<S>({String tag, String key, bool force = false}) { | ||
325 | final newKey = key ?? _getKey(S, tag); | 365 | final newKey = key ?? _getKey(S, tag); |
326 | 366 | ||
327 | if (!_singl.containsKey(newKey)) { | 367 | if (!_singl.containsKey(newKey)) { |
@@ -360,6 +400,37 @@ class GetInstance { | @@ -360,6 +400,37 @@ class GetInstance { | ||
360 | return true; | 400 | return true; |
361 | } | 401 | } |
362 | 402 | ||
403 | + void reloadAll({bool force = false}) { | ||
404 | + _singl.forEach((key, value) { | ||
405 | + if (value.permanent && !force) { | ||
406 | + Get.log('Instance "$key" is permanent. Skipping reload'); | ||
407 | + } else { | ||
408 | + value.dependency = null; | ||
409 | + value.isInit = false; | ||
410 | + Get.log('Instance "$key" was reloaded.'); | ||
411 | + } | ||
412 | + }); | ||
413 | + } | ||
414 | + | ||
415 | + void reload<S>({String tag, String key, bool force = false}) { | ||
416 | + final newKey = key ?? _getKey(S, tag); | ||
417 | + | ||
418 | + final builder = _getDependency<S>(tag: tag, key: newKey); | ||
419 | + if (builder == null) return; | ||
420 | + | ||
421 | + if (builder.permanent && !force) { | ||
422 | + Get.log( | ||
423 | + '''Instance "$newKey" is permanent. Use [force = true] to force the restart.''', | ||
424 | + isError: true, | ||
425 | + ); | ||
426 | + return; | ||
427 | + } | ||
428 | + | ||
429 | + builder.dependency = null; | ||
430 | + builder.isInit = false; | ||
431 | + Get.log('Instance "$newKey" was restarted.'); | ||
432 | + } | ||
433 | + | ||
363 | /// Check if a Class Instance<[S]> (or [tag]) is registered in memory. | 434 | /// Check if a Class Instance<[S]> (or [tag]) is registered in memory. |
364 | /// - [tag] is optional, if you used a [tag] to register the Instance. | 435 | /// - [tag] is optional, if you used a [tag] to register the Instance. |
365 | bool isRegistered<S>({String tag}) => _singl.containsKey(_getKey(S, tag)); | 436 | bool isRegistered<S>({String tag}) => _singl.containsKey(_getKey(S, tag)); |
@@ -370,12 +441,11 @@ class GetInstance { | @@ -370,12 +441,11 @@ class GetInstance { | ||
370 | bool isPrepared<S>({String tag}) { | 441 | bool isPrepared<S>({String tag}) { |
371 | final newKey = _getKey(S, tag); | 442 | final newKey = _getKey(S, tag); |
372 | 443 | ||
373 | - if (!_singl.containsKey(newKey)) { | ||
374 | - Get.log('Instance "$newKey" not found.', isError: true); | 444 | + final builder = _getDependency<S>(tag: tag, key: newKey); |
445 | + if (builder == null) { | ||
375 | return false; | 446 | return false; |
376 | } | 447 | } |
377 | 448 | ||
378 | - final builder = _singl[newKey]; | ||
379 | if (!builder.isInit) { | 449 | if (!builder.isInit) { |
380 | return true; | 450 | return true; |
381 | } | 451 | } |
@@ -386,8 +386,11 @@ class GetPageRoute<T> extends PageRoute<T> { | @@ -386,8 +386,11 @@ class GetPageRoute<T> extends PageRoute<T> { | ||
386 | void dispose() { | 386 | void dispose() { |
387 | super.dispose(); | 387 | super.dispose(); |
388 | if (Get.smartManagement != SmartManagement.onlyBuilder) { | 388 | if (Get.smartManagement != SmartManagement.onlyBuilder) { |
389 | - WidgetsBinding.instance.addPostFrameCallback( | ||
390 | - (_) => GetInstance().removeDependencyByRoute("$reference")); | 389 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
390 | + if (Get.reference != reference) { | ||
391 | + GetInstance().removeDependencyByRoute("$reference"); | ||
392 | + } | ||
393 | + }); | ||
391 | } | 394 | } |
392 | 395 | ||
393 | // if (Get.smartManagement != SmartManagement.onlyBuilder) { | 396 | // if (Get.smartManagement != SmartManagement.onlyBuilder) { |
@@ -69,10 +69,14 @@ abstract class GetWidget<S extends GetLifeCycleBase> extends GetWidgetCache { | @@ -69,10 +69,14 @@ abstract class GetWidget<S extends GetLifeCycleBase> extends GetWidgetCache { | ||
69 | class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> { | 69 | class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> { |
70 | S _controller; | 70 | S _controller; |
71 | bool _isCreator = false; | 71 | bool _isCreator = false; |
72 | + InstanceInfo info; | ||
72 | @override | 73 | @override |
73 | void onInit() { | 74 | void onInit() { |
74 | - _isCreator = Get.isPrepared<S>(tag: widget.tag); | ||
75 | - if (Get.isPrepared<S>()) { | 75 | + info = GetInstance().getInstanceInfo<S>(tag: widget.tag); |
76 | + | ||
77 | + _isCreator = info.isPrepared && info.isCreate; | ||
78 | + | ||
79 | + if (info.isRegistered) { | ||
76 | _controller = Get.find<S>(tag: widget.tag); | 80 | _controller = Get.find<S>(tag: widget.tag); |
77 | } | 81 | } |
78 | 82 | ||
@@ -90,6 +94,7 @@ class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> { | @@ -90,6 +94,7 @@ class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> { | ||
90 | GetWidget._cache[widget] = null; | 94 | GetWidget._cache[widget] = null; |
91 | }); | 95 | }); |
92 | } | 96 | } |
97 | + info = null; | ||
93 | super.onClose(); | 98 | super.onClose(); |
94 | } | 99 | } |
95 | 100 |
@@ -81,6 +81,19 @@ void main() { | @@ -81,6 +81,19 @@ void main() { | ||
81 | Get.reset(); | 81 | Get.reset(); |
82 | }); | 82 | }); |
83 | 83 | ||
84 | + test('Get.reloadInstance test', () async { | ||
85 | + Get.lazyPut<Controller>(() => Controller()); | ||
86 | + var ct1 = Get.find<Controller>(); | ||
87 | + ct1.increment(); | ||
88 | + expect(ct1.count, 1); | ||
89 | + ct1 = Get.find<Controller>(); | ||
90 | + expect(ct1.count, 1); | ||
91 | + GetInstance().reload<Controller>(); | ||
92 | + ct1 = Get.find<Controller>(); | ||
93 | + expect(ct1.count, 0); | ||
94 | + Get.reset(); | ||
95 | + }); | ||
96 | + | ||
84 | test('Get.lazyPut with abstract class test', () async { | 97 | test('Get.lazyPut with abstract class test', () async { |
85 | final api = Api(); | 98 | final api = Api(); |
86 | Get.lazyPut<Service>(() => api); | 99 | Get.lazyPut<Service>(() => api); |
@@ -127,6 +140,7 @@ void main() { | @@ -127,6 +140,7 @@ void main() { | ||
127 | class Controller extends DisposableController { | 140 | class Controller extends DisposableController { |
128 | int init = 0; | 141 | int init = 0; |
129 | int close = 0; | 142 | int close = 0; |
143 | + int count = 0; | ||
130 | @override | 144 | @override |
131 | void onInit() { | 145 | void onInit() { |
132 | init++; | 146 | init++; |
@@ -138,4 +152,8 @@ class Controller extends DisposableController { | @@ -138,4 +152,8 @@ class Controller extends DisposableController { | ||
138 | close++; | 152 | close++; |
139 | super.onClose(); | 153 | super.onClose(); |
140 | } | 154 | } |
155 | + | ||
156 | + void increment() { | ||
157 | + count++; | ||
158 | + } | ||
141 | } | 159 | } |
-
Please register or login to post a comment