Closed kidddoto closed 2 years ago
See async_task: ^1.0.13
:
AsyncTaskChannel
:
id
to help debugging.readMessage
.messageQueueLength
and messageQueueIsEmpty
.AsyncTaskChannelPort
:
id
to help debugging.readSync
and messageQueueLength
.Given that I need to receive multiple messages in main isolate, what would be the best way to listen to them?
This works fine, but it will only wait for a single message.
channel.waitMessage().then((value) {
});
Would I need to call: readMessage
from the loop? Is there any equivalent to listen
method?
For now there's no "listener". But you can instantiate a channel passing a messageHandler:
https://pub.dev/documentation/async_task/latest/async_task/AsyncTaskChannel-class.html
Thanks @gmpassos it works great.
My previous code which was looping and losing final messages (because of the 100ms sleep which was a window of opportunity to net get the message due to closed already channel) turned into much shorter and reliable version.
Currently:
final AsyncTaskChannelMessageHandler messageHandler = (dynamic message, bool fromExecutingContext) {
if (message != null) {
importantMessageProcessingFunction(message);
}
};
Previously:
final updateLoop = (AsyncTaskChannel channel) async {
while (!channel.isClosed) {
final message = channel.readMessage();
if (message != null) {
importantMessageProcessingFunction(message);
} else {
await Future.delayed(const Duration(milliseconds: 100));
}
}
};
// Get progress messages
for (final task in tasks) {
final channel = (await task.channel())!;
updateLoop(channel);
}
await Future.wait(executions);
// Get final message
for (final task in tasks) {
final channel = (await task.channel())!;
channel.waitMessage().then(importantMessageProcessingFunction);
}
Hello! Is there a non-blocking method for receiving a message inside the task?