1 | | | import '_local_worker_stub.dart' |
2 | | | if (dart.library.js) 'browser/_local_worker.dart' |
3 | | | if (dart.library.html) 'browser/_local_worker.dart' |
4 | | | if (dart.library.io) 'native/_local_worker.dart'; |
5 | | |
|
6 | | | import 'channel.dart'; |
7 | | | import 'worker_request.dart'; |
8 | | | import 'worker_service.dart'; |
9 | | |
|
10 | | | /// Base local worker class. |
11 | | | /// |
12 | | | /// Local workers are similar to other Workers except that they run in the context of the current thread. |
13 | | | /// They do not create any platform thread (such as Isolate or Web Worker) but they provide a [channel] that |
14 | | | /// can be shared with other workers to support communication between threads. One interesting use-case is |
15 | | | /// accessing UI components or platform plugins in Flutter, where only code running in the main thread is |
16 | | | /// allowed access to such features. Creating a [LocalWorker] in the main application and sharing its |
17 | | | /// [channel] with other workers enables providing access to Flutter features. |
18 | | | /// |
19 | | | /// Local workers wrap around a [WorkerService]. Messages sent to the local worker are deserialized as |
20 | | | /// [WorkerRequest] and dispatched to a handler defined in the [service]'s [WorkerService.operations] map |
21 | | | /// according to the [WorkerRequest.command]. |
22 | | | abstract class LocalWorker<W extends WorkerService> implements WorkerService { |
23 | | 1 | LocalWorker(this.service); |
24 | | |
|
25 | | 2 | factory LocalWorker.create(W service) => createLocalWorker<W>(service); |
26 | | |
|
27 | | | /// The [WorkerService] associated to this local worker. |
28 | | | final W service; |
29 | | |
|
30 | | | /// The local worker's [Channel]. This channel can be shared with other workers by calling [Channel.share]. |
31 | | | Channel? get channel; |
32 | | |
|
33 | | | /// Stops the local worker. |
34 | | | void stop(); |
35 | | |
|
36 | | | /// Local Workers do not need an [operations] map. |
37 | | | @override |
38 | | 1 | final Map<int, CommandHandler> operations = WorkerService.noOperations; |
39 | | | } |