compute library

When working with isolates, wiring up the SendPorts, the ReceivePorts correctly is a lot of boilerplate code when all you want to do is spawn an isolate, compute something, and use the computed value.

Flutter's compute function is a very useful abstraction over isolates that can be useful in all kinds of Dart apps.

Unfortunately, Flutter's compute function is not available for a Dart package is it doesn't use Flutter. Examples where this can be the case are command-line and server-side applications.

This package addresses this issue. It extracts the compute function from Flutter and makes it available for all Dart projects, so if you wish to perform some computation on a separate isolate and use its return value in a command line tool, or any kind of non-Flutter Dart project, now you can!

Do not assume that using compute will automatically speed up your code: you should benchmark your Dart applications with and without the compute function, and only switch to using compute if it really speeds up your application.

Keep in mind, that by using compute, you lose some flexibility that working directly with isolates would enable you.

The package is safe to be used on web, but there will be no real isolates spawned

Changes are synced with Flutter's stable branch only (and they are currently synced manually).

Constants

compute → const ComputeImpl
A function that spawns an isolate and runs the provided callback on that isolate, passes it the provided message, and (eventually) returns the value returned by callback.

Typedefs

ComputeCallback<Q, R> = FutureOr<R> Function(Q message)
Signature for the callback passed to compute.
ComputeImpl = Future<R> Function<Q, R>(ComputeCallback<Q, R> callback, Q message, {String? debugLabel})
The signature of compute, which spawns an isolate, runs callback on that isolate, passes it message, and (eventually) returns the value returned by callback.