refactor: sync
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// ignore_for_file: avoid-unsafe-collection-methods
|
||||
|
||||
class CollectionUtil {
|
||||
const CollectionUtil();
|
||||
|
||||
static int compareToNullable<T extends Comparable>(T? a, T? b) {
|
||||
if (a == null) {
|
||||
return 1;
|
||||
}
|
||||
if (b == null) {
|
||||
return -1;
|
||||
}
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
/// Find the difference between the two sorted lists [first] and [second]
|
||||
/// Results are passed as callbacks back to the caller during the comparison
|
||||
static bool diffSortedLists<T>(
|
||||
List<T> first,
|
||||
List<T> second, {
|
||||
required Comparator<T> compare,
|
||||
required bool Function(T a, T b) both,
|
||||
required void Function(T a) onlyFirst,
|
||||
required void Function(T b) onlySecond,
|
||||
}) {
|
||||
bool diff = false;
|
||||
int i = 0, j = 0;
|
||||
|
||||
for (; i < first.length && j < second.length;) {
|
||||
final int order = compare(first[i], second[j]);
|
||||
if (order == 0) {
|
||||
diff |= both(first[i++], second[j++]);
|
||||
} else if (order < 0) {
|
||||
onlyFirst(first[i++]);
|
||||
diff = true;
|
||||
} else if (order > 0) {
|
||||
onlySecond(second[j++]);
|
||||
diff = true;
|
||||
}
|
||||
}
|
||||
|
||||
diff |= i < first.length || j < second.length;
|
||||
|
||||
for (; i < first.length; i++) {
|
||||
onlyFirst(first[i]);
|
||||
}
|
||||
for (; j < second.length; j++) {
|
||||
onlySecond(second[j]);
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
extension SortIterable<T> on Iterable<T> {
|
||||
Iterable<T> sortedBy(Comparable Function(T k) key) =>
|
||||
toList()..sort((a, b) => key(a).compareTo(key(b)));
|
||||
}
|
||||
@@ -16,13 +16,19 @@ class _ImApiClientData {
|
||||
const _ImApiClientData({required this.endpoint, required this.headersMap});
|
||||
}
|
||||
|
||||
class InvalidIsolateUsageException implements Exception {
|
||||
const InvalidIsolateUsageException();
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
"IsolateHelper should only be used from the root isolate";
|
||||
}
|
||||
|
||||
// !! Should be used only from the root isolate
|
||||
class IsolateHelper {
|
||||
// Cache the ApiClient to reconstruct it later after inside the isolate
|
||||
late final _ImApiClientData? _clientData;
|
||||
|
||||
static RootIsolateToken get _rootToken => RootIsolateToken.instance!;
|
||||
|
||||
IsolateHelper();
|
||||
|
||||
void preIsolateHandling() {
|
||||
@@ -52,12 +58,21 @@ class IsolateHelper {
|
||||
}
|
||||
|
||||
static Future<T> run<T>(FutureOr<T> Function() computation) async {
|
||||
final token = RootIsolateToken.instance;
|
||||
if (token == null) {
|
||||
throw const InvalidIsolateUsageException();
|
||||
}
|
||||
|
||||
final helper = IsolateHelper()..preIsolateHandling();
|
||||
final token = _rootToken;
|
||||
return await Isolate.run(() async {
|
||||
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
|
||||
helper.postIsolateHandling();
|
||||
return await computation();
|
||||
try {
|
||||
return await computation();
|
||||
} finally {
|
||||
// Always close the new database connection on Isolate end
|
||||
await di<DriftDatabaseRepository>().close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user