thread 0.0.1 copy "thread: ^0.0.1" to clipboard
thread: ^0.0.1 copied to clipboard

outdated

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.

Thread #

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.

Getting started #

Install it using pub:

flutter pub add thread

And import the package:

import 'package:thread/thread.dart';

Usage #

Setup a thread along with a function that will be running with it.

final thread = IsolateThread((emitter) {
    ...
});

// Create a thread with no initial function
final thread = IsolateThread.empty();

Inside the function, use the given Event Emitter to communicate.

final thread = IsolateThread((emitter) {
    emitter.on('do this', (String data) {
        emitter.emit('done', '[Computed] $data');
    });
});

Listen for the result outside the thread and send a signal whenever you need it.

thread.on('done', (String data) {
    print(data);
});

thread.emit('do this', 'Hello World');

// [Output]
// [Computed] Hello World

There are also signals to compute tasks individually with no setup needed. These tasks can be asynchronous.

// Send a single task for the thread to run
print(await thread.compute(() => '[Computed] Hello World'));

// Compute a task along some input dat
print(await thread.computeWith('Hello World', (String data) {
    return '[Computed] $data';
}));

// [Output]
// [Computed] Hello World

The thread starts automatically when you create it, the emitted events will be handled by the thread after started. But you can also start it manually.

final thread = IsolateThread((emitter) {
    ...
}, start: false, keepEmitsWhileNotRunning: false);

await thread.start();

thread.emit( ... );

Stop the thread by using thread.stop() or emitter.emit('end', true), you can start another isolated thread with the same object by using thread.start(), but only if the thread is not already running.

GitHub #

The package code is available on Github: Dart - Thread

Example #

final thread = IsolateThread((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
12
likes
0
pub points
83%
popularity

Publisher

verified publisherdrafakiller.com

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.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

async_signal, events_emitter, uuid

More

Packages that depend on thread