downloadIsolateEntry function
Entry point function for the download isolate. Sets up communication with the main isolate and listens for incoming messages to control download tasks (start, pause, cancel, etc.).
Implementation
void downloadIsolateEntry(SendPort mainSendPort) {
final receivePort = ReceivePort();
// Send the SendPort of this isolate back to the main isolate for communication.
mainSendPort.send(DownloadIsolateMsg(
IsolateMsgType.sendPort,
receivePort.sendPort,
));
DownloadIsolate? downloadIsolate;
// Listen for messages from the main isolate.
receivePort.listen((message) {
if (message is DownloadIsolateMsg) {
switch (message.type) {
case IsolateMsgType.logPrint:
// Update log print configuration if needed.
if (message.data != null && message.data is bool) {
Config.logPrint = message.data as bool;
}
break;
case IsolateMsgType.httpClient:
// Update HTTP client builder if needed.
httpClientBuilder = message.data as HttpClientBuilder;
break;
case IsolateMsgType.task:
// Handle download task messages (start, pause, cancel).
if (message.data == null) break;
final task = message.data as DownloadTask;
downloadIsolate ??= DownloadIsolate();
if (task.status == DownloadStatus.PAUSED) {
downloadIsolate?.pause(task);
} else if (task.status == DownloadStatus.CANCELLED) {
downloadIsolate?.cancel(task);
} else {
downloadIsolate?.reset(task);
downloadIsolate?.start(task, mainSendPort);
}
break;
default:
break;
}
}
});
}