-
-
Notifications
You must be signed in to change notification settings - Fork 7
Service Setup
Service setup in Squadron allows you to initialize your services and configure the threads they run in. This can be done either on the worker side (background thread) or on the main thread side.
Initialize your services directly via their constructors. This is the most common way to provide configuration, dependencies, or initial state to a service.
When you define a @SquadronService, squadron_builder automatically manages the transfer of constructor arguments from the main thread to the worker thread.
@SquadronService()
class MyService {
// Arguments passed here will be managed by squadron_builder
MyService(this.config, this.apiKey);
final Map config;
final String apiKey;
@SquadronMethod()
Future<void> doWork() async {
// Service is already initialized with config and apiKey
}
}When instantiating the generated Worker or WorkerPool, you simply provide these arguments:
final worker = MyServiceWorker(config, apiKey);Sometimes you need to configure the thread itself (the Isolate or Web Worker) before it starts executing service commands. This is done on the main thread using a PlatformThreadHook.
A PlatformThreadHook is a function that receives the underlying platform thread object (Isolate on Native, Worker on Web) and can perform low-level configurations.
You can provide a hook when starting a worker or pool. This is useful for:
- Setting up custom
Isolateproperties (e.g., debug names, priorities). - Configuring Web Workers (e.g., adding event listeners at the platform level).
The PlatformThreadHook is defined as:
typedef PlatformThreadHook = FutureOr<void> Function(PlatformThread thread);-
On Native:
PlatformThreadis anIsolate. -
On Web:
PlatformThreadis aWorkerfrompackage:web.
| Feature | Worker-Side Setup | Main-Thread Setup |
|---|---|---|
| Target | The Service instance | The Platform Thread (Isolate/Worker) |
| Location | Background Thread | Main Thread |
| Mechanism | Constructor Arguments | PlatformThreadHook |
| Best For | Business logic initialization | Low-level thread configuration |
💖 Support the project! Sponsor d-markey on GitHub.