Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
fluttertpc_get
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Jonny Borges
2020-09-10 15:26:04 -0300
Browse Files
Options
Browse Files
Download
Plain Diff
Committed by
GitHub
2020-09-10 15:26:04 -0300
Commit
d11200bd69326197f64ebdf23ec23462f5cb7452
d11200bd
2 parents
075d9fcb
451544aa
Merge pull request #589 from roipeker/fix_get_create
Fix for Get.create() lifecycle
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
4 deletions
.gitignore
lib/src/instance/extension_instance.dart
lib/src/instance/get_instance.dart
.gitignore
View file @
d11200b
...
...
@@ -74,3 +74,9 @@ captures/
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
example/macos/Flutter/ephemeral/Flutter-Generated.xcconfig
example/macos/Flutter/ephemeral/
example/macos/Flutter/GeneratedPluginRegistrant.swift
...
...
lib/src/instance/extension_instance.dart
View file @
d11200b
...
...
@@ -38,9 +38,15 @@ extension Inst on GetInterface {
{
String
tag
,
bool
permanent
=
false
})
async
=>
GetInstance
().
putAsync
<
S
>(
builder
,
tag:
tag
,
permanent:
permanent
);
/// Creates a new
Instance<[S]> from the <[S]>[builder] callback
.
/// Creates a new
Class Instance [S] from the builder callback[S]
.
/// Every time [find]<[S]>() is used, it calls the builder method to generate
/// a new Instance [S].
/// It also registers each [instance.onClose()] with the current
/// Route [GetConfig.currentRoute] to keep the lifecycle active.
/// Is important to know that the instances created are only stored per Route.
/// So, if you call `Get.delete<T>()` the "instance factory" used in this
/// method ([Get.create<T>()]) will be removed, but NOT the instances
/// already created by it.
///
/// Example:
///
...
...
lib/src/instance/get_instance.dart
View file @
d11200b
import
'dart:async'
;
import
'dart:collection'
;
import
'../core/log.dart'
;
import
'../navigation/root/smart_management.dart'
;
import
'../state_manager/rx/rx_core/rx_interface.dart'
;
...
...
@@ -30,6 +33,12 @@ class GetInstance {
/// created to manage the memory.
static
final
Map
<
String
,
String
>
_routesKey
=
{};
/// Stores the onClose() references of instances created with [Get.create()]
/// using the [GetConfig.currentRoute].
/// Experimental feature to keep the lifecycle and memory management with
/// non-singleton instances.
static
final
Map
<
String
,
HashSet
<
Function
>>
_routesByCreate
=
{};
static
final
_queue
=
GetQueue
();
/// Creates a new Instance<S> lazily from the [<S>builder()] callback.
...
...
@@ -101,6 +110,12 @@ class GetInstance {
/// Creates a new Class Instance [S] from the builder callback[S].
/// Every time [find]<[S]>() is used, it calls the builder method to generate
/// a new Instance [S].
/// It also registers each [instance.onClose()] with the current
/// Route [GetConfig.currentRoute] to keep the lifecycle active.
/// Is important to know that the instances created are only stored per Route.
/// So, if you call `Get.delete<T>()` the "instance factory" used in this
/// method ([Get.create<T>()]) will be removed, but NOT the instances
/// already created by it.
///
/// Example:
///
...
...
@@ -144,6 +159,19 @@ class GetInstance {
}
});
/// Removes [Get.create()] instances registered in [routeName].
if
(
_routesByCreate
.
containsKey
(
routeName
))
{
for
(
final
onClose
in
_routesByCreate
[
routeName
])
{
// assure the [DisposableInterface] instance holding a reference
// to [onClose()] wasn't disposed.
if
(
onClose
!=
null
)
{
await
onClose
();
}
}
_routesByCreate
[
routeName
].
clear
();
_routesByCreate
.
remove
(
routeName
);
}
for
(
final
element
in
keysToRemove
)
{
await
delete
(
key:
element
);
}
...
...
@@ -159,14 +187,18 @@ class GetInstance {
/// Optionally associating the current Route to the lifetime of the instance,
/// if [GetConfig.smartManagement] is marked as [SmartManagement.full] or
/// [GetConfig.keepFactory]
/// Only flags `isInit` if it's using `Get.create()`
/// (not for Singletons access).
bool
_initDependencies
<
S
>({
String
name
})
{
final
key
=
_getKey
(
S
,
name
);
final
isInit
=
_singl
[
key
].
isInit
;
if
(!
isInit
)
{
_startController
<
S
>(
tag:
name
);
_singl
[
key
].
isInit
=
true
;
if
(
GetConfig
.
smartManagement
!=
SmartManagement
.
onlyBuilder
)
{
_registerRouteInstance
<
S
>(
tag:
name
);
if
(
_singl
[
key
].
isSingleton
)
{
_singl
[
key
].
isInit
=
true
;
if
(
GetConfig
.
smartManagement
!=
SmartManagement
.
onlyBuilder
)
{
_registerRouteInstance
<
S
>(
tag:
name
);
}
}
}
return
true
;
...
...
@@ -193,6 +225,10 @@ class GetInstance {
i
.
onStart
();
GetConfig
.
log
(
'"
$key
" has been initialized'
);
}
if
(!
_singl
[
key
].
isSingleton
&&
i
.
onClose
!=
null
)
{
_routesByCreate
[
GetConfig
.
currentRoute
]
??=
HashSet
<
Function
>();
_routesByCreate
[
GetConfig
.
currentRoute
].
add
(
i
.
onClose
);
}
}
}
...
...
Please
register
or
login
to post a comment