Skip to content

Dart code generator for Squadron workers. Implement your worker service and let squadron_builder bridge the gap with Web Workers and Isolates!

License

Notifications You must be signed in to change notification settings

d-markey/squadron_builder

Repository files navigation

Squadron logo

Squadron - Multithreading and worker pools in Dart

Offload CPU-bound and long running tasks and give your mobile and Web apps some air!

squadron_builder

Dart code generator for Squadron workers. Implement your worker service and let squadron_builder bridge the gap with Web Workers and Isolates!

Pub Package Dart Platforms Flutter Platforms

License Null Safety Dart Style Pub Points Likes Popularity Last Commit

Usage

squadron_builder is a companion package to Squadron and should be installed as a development dependency.

Its purpose is to interpret Squadron annotations applied to classes and methods from your source code, and generate the worker and worker pool classes for you as well as the plumbing required to make platform threads communicate with each other.

Service classes must be decorated with SquadronService annotations, and service methods with SquadronMethod.

Assuming your service implementation is found in file my_service.dart, the following references are required in your source code:

  • import the generated activator file import 'my_service.activator.g.dart';
  • reference the generated part file part 'my_service.worker.g.dart';

Examples

Example - Hello, World

The classic Hello, World! example code with a HelloWorld service:

import 'dart:async';

// Squadron is required for annotations.
// It is also required by the generated part file.
import 'package:squadron/squadron.dart';

// This file must be imported into your library file.
// It is used to provide the worker's entry point, which depends on the target platform.
// It is generated by squadron_builder and required by the generated part file.
import 'hello_world.activator.g.dart';

// This file is generated by squadron_builder as a part file.
// It implements `HelloWorldWorker` and `HelloWorldWorkerPool` for multithreading.
part 'hello_world.worker.g.dart';

@SquadronService(web: false)
class HelloWorld {
  @squadronMethod
  Future<String> hello([String? name]) async {
    name = name?.trim() ?? '';
    return name.isEmpty ? 'Hello, World!' : 'Hello, $name!';
  }
}

Generate code for HelloWorldWorker and HelloWorldWorkerPool with dart run build_runner build.

Now you're ready to go:

import 'hello_world.dart';

void main() async {
  final worker = HelloWorldWorker();
  print(await worker.hello());
  print(await worker.hello('Mary'));
  print(await worker.hello('John'));
  worker.stop();
}

Sample output:

Hello, World!
Hello, Mary!
Hello, John!

Example - Fibonacci sequence

The example computes Fibonacci numbers recursively, simply applying the definition of the Fibonacci sequence. It is very inefficient, but illustrates the effect of multithreading.

import 'dart:async';

import 'package:squadron/squadron.dart';

import 'fib_service.activator.g.dart';

part 'fib_service.worker.g.dart';

@SquadronService()
class FibService {
  @squadronMethod
  Future<int> fibonacci(int i) as