2024-05-31 22:37:25 +00:00
|
|
|
part of 'firo_cache.dart';
|
|
|
|
|
|
|
|
enum FCFuncName {
|
|
|
|
_updateSparkAnonSetCoinsWith,
|
|
|
|
_updateSparkUsedTagsWith,
|
|
|
|
}
|
|
|
|
|
|
|
|
class FCTask {
|
|
|
|
final id = const Uuid().v4();
|
|
|
|
final FCFuncName func;
|
|
|
|
final dynamic data;
|
|
|
|
|
|
|
|
FCTask({required this.func, required this.data});
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FiroCacheWorker {
|
|
|
|
final SendPort _commands;
|
|
|
|
final ReceivePort _responses;
|
|
|
|
final Map<String, Completer<Object?>> _activeRequests = {};
|
|
|
|
|
|
|
|
Future<Object?> runTask(FCTask task) async {
|
|
|
|
final completer = Completer<Object?>.sync();
|
|
|
|
_activeRequests[task.id] = completer;
|
|
|
|
_commands.send(task);
|
|
|
|
return await completer.future;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<_FiroCacheWorker> spawn() async {
|
2024-06-05 19:38:20 +00:00
|
|
|
final dir = await StackFileSystem.applicationFiroCacheSQLiteDirectory();
|
|
|
|
final setCacheFilePath = "${dir.path}/${_FiroCache.sparkSetCacheFileName}";
|
|
|
|
final usedTagsCacheFilePath =
|
|
|
|
"${dir.path}/${_FiroCache.sparkUsedTagsCacheFileName}";
|
2024-05-31 22:37:25 +00:00
|
|
|
|
|
|
|
final initPort = RawReceivePort();
|
|
|
|
final connection = Completer<(ReceivePort, SendPort)>.sync();
|
|
|
|
|
|
|
|
initPort.handler = (dynamic initialMessage) {
|
|
|
|
final commandPort = initialMessage as SendPort;
|
|
|
|
connection.complete(
|
|
|
|
(
|
|
|
|
ReceivePort.fromRawReceivePort(initPort),
|
|
|
|
commandPort,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Isolate.spawn(
|
|
|
|
_startWorkerIsolate,
|
2024-06-05 19:38:20 +00:00
|
|
|
(initPort.sendPort, setCacheFilePath, usedTagsCacheFilePath),
|
2024-05-31 22:37:25 +00:00
|
|
|
);
|
|
|
|
} catch (_) {
|
|
|
|
initPort.close();
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
final (receivePort, sendPort) = await connection.future;
|
|
|
|
|
|
|
|
return _FiroCacheWorker._(receivePort, sendPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FiroCacheWorker._(this._responses, this._commands) {
|
|
|
|
_responses.listen(_handleResponsesFromIsolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleResponsesFromIsolate(dynamic message) {
|
|
|
|
final (id, error) = message as (String, Object?);
|
|
|
|
final completer = _activeRequests.remove(id)!;
|
|
|
|
|
|
|
|
if (error != null) {
|
|
|
|
completer.completeError(error);
|
|
|
|
} else {
|
|
|
|
completer.complete(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _handleCommandsToIsolate(
|
|
|
|
ReceivePort receivePort,
|
|
|
|
SendPort sendPort,
|
2024-06-05 19:38:20 +00:00
|
|
|
Database setCacheDb,
|
|
|
|
Database usedTagsCacheDb,
|
2024-05-31 22:37:25 +00:00
|
|
|
Mutex mutex,
|
|
|
|
) {
|
|
|
|
receivePort.listen((message) {
|
|
|
|
final task = message as FCTask;
|
|
|
|
|
|
|
|
mutex.protect(() async {
|
|
|
|
try {
|
|
|
|
final FCResult result;
|
|
|
|
switch (task.func) {
|
|
|
|
case FCFuncName._updateSparkAnonSetCoinsWith:
|
|
|
|
final data = task.data as (int, Map<String, dynamic>);
|
2024-06-05 19:38:20 +00:00
|
|
|
result = _updateSparkAnonSetCoinsWith(
|
|
|
|
setCacheDb,
|
|
|
|
data.$2,
|
|
|
|
data.$1,
|
|
|
|
);
|
2024-05-31 22:37:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FCFuncName._updateSparkUsedTagsWith:
|
2024-06-05 19:38:20 +00:00
|
|
|
result = _updateSparkUsedTagsWith(
|
|
|
|
usedTagsCacheDb,
|
|
|
|
task.data as List<String>,
|
|
|
|
);
|
2024-05-31 22:37:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
sendPort.send((task.id, null));
|
|
|
|
} else {
|
|
|
|
sendPort.send((task.id, result.error!));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
sendPort.send((task.id, e));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-05 19:38:20 +00:00
|
|
|
static void _startWorkerIsolate((SendPort, String, String) args) {
|
2024-05-31 22:37:25 +00:00
|
|
|
final receivePort = ReceivePort();
|
|
|
|
args.$1.send(receivePort.sendPort);
|
|
|
|
final mutex = Mutex();
|
2024-06-05 19:38:20 +00:00
|
|
|
final setCacheDb = sqlite3.open(
|
2024-05-31 22:37:25 +00:00
|
|
|
args.$2,
|
|
|
|
mode: OpenMode.readWrite,
|
|
|
|
);
|
2024-06-05 19:38:20 +00:00
|
|
|
final usedTagsCacheDb = sqlite3.open(
|
|
|
|
args.$3,
|
|
|
|
mode: OpenMode.readWrite,
|
|
|
|
);
|
|
|
|
_handleCommandsToIsolate(
|
|
|
|
receivePort,
|
|
|
|
args.$1,
|
|
|
|
setCacheDb,
|
|
|
|
usedTagsCacheDb,
|
|
|
|
mutex,
|
|
|
|
);
|
2024-05-31 22:37:25 +00:00
|
|
|
}
|
|
|
|
}
|