1 | | | import 'dart:async'; |
2 | | |
|
3 | | | import 'worker/worker.dart'; |
4 | | | import 'worker/worker_request.dart'; |
5 | | |
|
6 | | | typedef WorkerInitializer = FutureOr<WorkerService> Function( |
7 | | | WorkerRequest startRequest); |
8 | | |
|
9 | | | typedef CommandHandler = FutureOr<dynamic> Function(WorkerRequest req); |
10 | | |
|
11 | | | typedef SquadronCallback = void Function(); |
12 | | | typedef SquadronAsyncCallback = FutureOr<void> Function(); |
13 | | |
|
14 | | | /// Extend this class or implement this interface in your worker service if it needs |
15 | | | /// to take action when the worker thread is started or stopped. |
16 | | | mixin ServiceInstaller { |
17 | | | /// Squadron will call this method as part of the worker thread initialization. |
18 | | | /// It will be called just after the service instance has been constructed. The |
19 | | | /// future returned by [Worker.start] will not complete before this method completes |
20 | | | /// whether synchronously or asynchronously. If this method throws, the future |
21 | | | /// returned by [Worker.start] will complete with an error and the service will not |
22 | | | /// be available. |
23 | | 1 | FutureOr<void> install() {} |
24 | | |
|
25 | | | /// Squadron will call this method as part of the worker thread shutdown process. |
26 | | | /// It will be called just before effectively closing the platform channel. If |
27 | | | /// this method throws, the exception will not bubble up to the parent thread. |
28 | | | /// Also, [Worker.stop] does not wait for this method to complete. |
29 | | 1 | FutureOr<void> uninstall() {} |
30 | | | } |
31 | | |
|
32 | | | /// Base class for a worker service. |
33 | | | abstract interface class WorkerService { |
34 | | | /// Map of command handlers. Upon reception of a [WorkerRequest], the platform |
35 | | | /// worker will dispatch the request to the [CommandHandler] mathing the value |
36 | | | /// of [WorkerRequest.command]. |
37 | | | Map<int, CommandHandler> get operations; |
38 | | |
|
39 | | | /// Empty command handlers map. |
40 | | 2 | static final Map<int, CommandHandler> noOperations = |
41 | | 1 | Map<int, CommandHandler>.unmodifiable(const {}); |
42 | | | } |