thread 0.1.2
thread: ^0.1.2 copied to clipboard
A simple Isolated Thread wrapped with a type-safe Event Emitter for easier asynchronous communication. Setup events for the thread to reply to, or compute tasks individually.
example/lib/main.dart
import 'package:thread/thread.dart';
void main() async {
final thread = Thread((emitter) {
emitter.on('compute', (String data) async {
await Future.delayed(const Duration(seconds: 1));
emitter.emit('result', '[Computed] $data');
});
});
thread.on('result', (String data) => print(data));
thread.emit('compute', 'Hello world!');
thread.emit('compute', 'Wow!');
print(await thread.compute(() => 'Hello world!'));
print(await thread.computeWith(123, (int data) => 'Wow $data'));
// [Output]
// [Computed] Hello world!
// [Computed] Wow!
// Hello world!
// Wow 123
}