get_instance.dart
9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import 'package:flutter/cupertino.dart';
import 'package:get/src/core/log.dart';
import 'package:get/src/navigation/root/smart_management.dart';
import 'package:get/src/state_manager/rx/rx_interface.dart';
import 'package:get/src/utils/queue/get_queue.dart';
class GetConfig {
static SmartManagement smartManagement = SmartManagement.full;
static bool isLogEnable = true;
static LogWriterCallback log = defaultLogWriterCallback;
static String currentRoute;
}
class Lazy {
Lazy(this.builder, this.fenix);
bool fenix;
FcBuilderFunc builder;
}
class GetInstance {
factory GetInstance() {
if (_getInstance == null) _getInstance = GetInstance._();
return _getInstance;
}
const GetInstance._();
static GetInstance _getInstance;
static Map<dynamic, dynamic> _singl = {};
static Map<dynamic, Lazy> _factory = {};
static Map<String, String> _routesKey = {};
void lazyPut<S>(FcBuilderFunc builder, {String tag, bool fenix = false}) {
String key = _getKey(S, tag);
_factory.putIfAbsent(key, () => Lazy(builder, fenix));
}
Future<S> putAsync<S>(FcBuilderFuncAsync<S> builder,
{String tag, bool permanent = false}) async {
return put<S>(await builder(), tag: tag, permanent: permanent);
}
/// Injects a Instance [S] in [GetInstance].
///
/// No need to define the generic type <[S]> as it's inferred from the [dependency]
///
/// - [dependency] The Instance to be injected.
/// - [tag] optionally, use a [tag] as an "id" to create multiple records of the same Type<[S]>
/// - [permanent] keeps the Instance in memory, not following [GetConfig.smartManagement]
/// rules.
///
S put<S>(
S dependency, {
String tag,
bool permanent = false,
FcBuilderFunc<S> builder,
}) {
_insert(
isSingleton: true,
name: tag,
permanent: permanent,
builder: builder ?? (() => dependency));
return find<S>(tag: tag);
}
/// 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].
///
/// Example:
///
/// ```create(() => Repl());
/// Repl a = find();
/// Repl b = find();
/// print(a==b); (false)```
///
void create<S>(
FcBuilderFunc<S> builder, {
String name,
bool permanent = true,
}) {
_insert(
isSingleton: false, name: name, builder: builder, permanent: permanent);
}
/// Injects the Instance [S] builder into the [_singleton] HashMap.
///
void _insert<S>({
bool isSingleton,
String name,
bool permanent = false,
FcBuilderFunc<S> builder,
}) {
assert(builder != null);
final key = _getKey(S, name);
_singl.putIfAbsent(
key, () => FcBuilder<S>(isSingleton, builder, permanent, false));
}
/// Clears from memory registered Instances associated with [routeName] when
/// using [GetConfig.smartManagement] as [SmartManagement.full] or [SmartManagement.keepFactory]
/// Meant for internal usage of [GetPageRoute] and [GetDialogRoute]
///
Future<void> removeDependencyByRoute(String routeName) async {
final keysToRemove = <String>[];
_routesKey.forEach((key, value) {
if (value == routeName) {
keysToRemove.add(key);
}
});
keysToRemove.forEach((element) async {
await delete(key: element);
});
keysToRemove.forEach((element) {
_routesKey?.remove(element);
});
keysToRemove.clear();
}
/// Initializes the dependencies for a Class Instance [S] (or tag),
/// If its a Controller, it starts the lifecycle process.
/// Optionally associating the current Route to the lifetime of the instance,
/// if [GetConfig.smartManagement] is marked as [SmartManagement.full] or
/// [GetConfig.keepFactory]
///
bool _initDependencies<S>({String name}) {
final key = _getKey(S, name);
bool isInit = _singl[key].isInit;
if (!isInit) {
_startController<S>(tag: name);
_singl[key].isInit = true;
if (GetConfig.smartManagement != SmartManagement.onlyBuilder) {
_registerRouteInstance<S>(tag: name);
}
}
return true;
}
/// Links a Class instance [S] (or [tag]) to the current route.
/// Requires usage of [GetMaterialApp].
///
void _registerRouteInstance<S>({String tag}) {
_routesKey.putIfAbsent(_getKey(S, tag), () => GetConfig.currentRoute);
}
/// Finds and returns a Class instance [S] (or tag) without further processing.
S findByType<S>(Type type, {String tag}) {
String key = _getKey(type, tag);
return _singl[key].getDependency() as S;
}
void _startController<S>({String tag}) {
final key = _getKey(S, tag);
final i = _singl[key].getDependency();
if (i is DisposableInterface) {
i.onStart();
GetConfig.log('$key has been initialized');
}
}
// S putOrFind<S>(S Function() dep, {String tag}) {
// final key = _getKey(S, tag);
// if (_singl.containsKey(key)) {
// return _singl[key].getDependency() as S;
// } else {
// if (_factory.containsKey(key)) {
// S _value = put<S>((_factory[key].builder() as S), tag: tag);
// if (GetConfig.smartManagement != SmartManagement.keepFactory) {
// if (!_factory[key].fenix) {
// _factory.remove(key);
// }
// }
// return _value;
// }
// return GetInstance().put(dep(), tag: tag);
// }
// }
/// Finds a instance of the required Class<[S]> (or [tag])
/// In the case of using Get.[create], it will create an instance
/// each time you call [find]
///
S find<S>({String tag}) {
String key = _getKey(S, tag);
if (isRegistered<S>(tag: tag)) {
FcBuilder builder = _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";
}
}
_initDependencies<S>(name: tag);
return _singl[key].getDependency() as S;
} else {
if (!_factory.containsKey(key))
throw "$S not found. You need call put<$S>($S()) before";
GetConfig.log('$S instance was created at that time');
S _value = put<S>(_factory[key].builder() as S);
_initDependencies<S>(name: tag);
if (GetConfig.smartManagement != SmartManagement.keepFactory &&
!_factory[key].fenix) {
_factory.remove(key);
}
return _value;
}
}
String _getKey(Type type, String name) {
return name == null ? type.toString() : type.toString() + name;
}
/// Clears all registered instances (and/or tags).
/// Even the persistent ones.
///
/// [clearFactory] clears the callbacks registered by [lazyPut]
/// [clearRouteBindings] clears Instances associated with routes.
///
bool reset({bool clearFactory = true, bool clearRouteBindings = true}) {
if (clearFactory) _factory.clear();
if (clearRouteBindings) _routesKey.clear();
_singl.clear();
return true;
}
static GetQueue queue = GetQueue();
// Future<bool> delete<S>({String tag, String key, bool force = false}) async {
// final s = await queue
// .add<bool>(() async => dele<S>(tag: tag, key: key, force: force));
// return s;
// }
/// Delete registered Class Instance [S] (or [tag]) and, closes any open
/// controllers [DisposableInterface], cleans up the memory
Future<bool> delete<S>({String tag, String key, bool force = false}) async {
final newKey = key ?? _getKey(S, tag);
return queue.add<bool>(() async {
if (!_singl.containsKey(newKey)) {
GetConfig.log('Instance $newKey already been removed.', isError: true);
return false;
}
FcBuilder builder = _singl[newKey] as FcBuilder;
if (builder.permanent && !force) {
GetConfig.log(
'[$newKey] has been marked as permanent, SmartManagement is not authorized to delete it.',
isError: true);
return false;
}
final i = builder.dependency;
if (i is GetxService && !force) {
return false;
}
if (i is DisposableInterface) {
await i.onClose();
GetConfig.log('onClose of $newKey called');
}
_singl.removeWhere((oldKey, value) => (oldKey == newKey));
if (_singl.containsKey(newKey)) {
GetConfig.log('error on remove object $newKey', isError: true);
} else {
GetConfig.log('$newKey deleted from memory');
}
// _routesKey?.remove(key);
return true;
});
}
/// Check if a Class instance [S] (or [tag]) is registered.
bool isRegistered<S>({String tag}) => _singl.containsKey(_getKey(S, tag));
/// Check if Class instance [S] (or [tag]) is prepared to be used.
bool isPrepared<S>({String tag}) => _factory.containsKey(_getKey(S, tag));
}
typedef FcBuilderFunc<S> = S Function();
typedef FcBuilderFuncAsync<S> = Future<S> Function();
class FcBuilder<S> {
bool isSingleton;
FcBuilderFunc builderFunc;
S dependency;
bool permanent = false;
bool isInit = false;
FcBuilder(this.isSingleton, this.builderFunc, this.permanent, this.isInit);
S getDependency() {
if (isSingleton) {
if (dependency == null) {
dependency = builderFunc() as S;
}
return dependency;
} else {
return builderFunc() as S;
}
}
}