Line data Source code
1 : import 'package:combine/src/combine_worker/combine_worker_manager.dart';
2 : import 'package:combine/src/combine_worker/effective_worker_factory.dart';
3 : import 'package:combine/src/combine_worker/tasks.dart';
4 : import 'package:combine/src/combine_worker_singleton.dart';
5 :
6 : class CombineWorkerImpl implements CombineWorker {
7 : CombineWorkerManager? _workerManager;
8 2 : CombineWorkerManager get _effectiveWorkerManager {
9 4 : return _workerManager ??= _createCombineWorkerManager();
10 : }
11 :
12 : var _isInitialized = false;
13 :
14 : /// {@macro combine_worker_initialize}
15 2 : @override
16 : Future<void> initialize({
17 : int? isolatesCount,
18 : int tasksPerIsolate = defaultTasksPerIsolate,
19 : WorkerInitializer? initializer,
20 : String isolatesPrefix = defaultIsolatePrefix,
21 : }) async {
22 : assert(
23 4 : isolatesCount == null || isolatesCount > 0,
24 : "`isolatesCount` must be greater than zero if specified.",
25 : );
26 : assert(
27 4 : !_isInitialized,
28 : "`CombineWorker` is already initialized.\n"
29 : "This may happen if you call some `execute` function before `initialize`.\n",
30 : );
31 : assert(
32 4 : tasksPerIsolate > 0,
33 : "`tasksPerIsolate` parameter must be greater that zero",
34 : );
35 :
36 2 : _isInitialized = true;
37 6 : _workerManager = effectiveWorkerFactory.create(
38 : tasksPerIsolate: tasksPerIsolate,
39 : isolatesCount: isolatesCount,
40 : );
41 4 : await _effectiveWorkerManager.initialize(
42 : initializer: initializer,
43 : isolatesPrefix: isolatesPrefix,
44 : );
45 : }
46 :
47 : /// {@macro combine_worker_execute}
48 2 : @override
49 : Future<T> execute<T>(Task<T> action) {
50 6 : return _effectiveWorkerManager.execute(NoArgsTask(action));
51 : }
52 :
53 : /// {@macro combine_worker_execute_with_arg}
54 2 : @override
55 : Future<T> executeWithArg<T, Q>(Task1<T, Q> action, Q argument) {
56 6 : return _effectiveWorkerManager.execute(TaskWithArg(action, argument));
57 : }
58 :
59 : /// {@macro combine_worker_execute_with_2_args}
60 2 : @override
61 : Future<T> executeWith2Args<T, Q, C>(
62 : Task2<T, Q, C> action,
63 : Q argument,
64 : C argument2,
65 : ) {
66 4 : return _effectiveWorkerManager.execute(
67 2 : TaskWith2Args(action, argument, argument2),
68 : );
69 : }
70 :
71 : /// {@macro combine_worker_close}
72 1 : @override
73 : Future<void> close({bool waitForRemainingTasks = false}) async {
74 2 : final closeResult = _workerManager?.close(
75 : waitForRemainingTasks: waitForRemainingTasks,
76 : );
77 :
78 1 : _workerManager = null;
79 1 : _isInitialized = false;
80 :
81 : return closeResult;
82 : }
83 :
84 1 : @override
85 : Future<T> executeWith3Args<T, Q, C, A>(
86 : Task3<T, Q, C, A> task,
87 : Q argument,
88 : C argument2,
89 : A argument3,
90 : ) {
91 2 : return _effectiveWorkerManager.execute(
92 1 : TaskWith3Args(task, argument, argument2, argument3),
93 : );
94 : }
95 :
96 1 : @override
97 : Future<T> executeWith4Args<T, Q, C, A, B>(
98 : Task4<T, Q, C, A, B> task,
99 : Q argument,
100 : C argument2,
101 : A argument3,
102 : B argument4,
103 : ) {
104 2 : return _effectiveWorkerManager.execute(
105 1 : TaskWith4Args(task, argument, argument2, argument3, argument4),
106 : );
107 : }
108 :
109 1 : @override
110 : Future<T> executeWith5Args<T, Q, C, A, B, D>(
111 : Task5<T, Q, C, A, B, D> task,
112 : Q argument,
113 : C argument2,
114 : A argument3,
115 : B argument4,
116 : D arguments5,
117 : ) {
118 2 : return _effectiveWorkerManager.execute(
119 1 : TaskWith5Args(
120 : task,
121 : argument,
122 : argument2,
123 : argument3,
124 : argument4,
125 : arguments5,
126 : ),
127 : );
128 : }
129 :
130 1 : @override
131 : Future<T> executeWithApply<T>(
132 : TaskApply task,
133 : List? positionalArguments, [
134 : Map<Symbol, dynamic>? namedArguments,
135 : ]) {
136 2 : return _effectiveWorkerManager.execute(
137 1 : TaskWithApplyArgs(task, positionalArguments, namedArguments),
138 : );
139 : }
140 :
141 2 : CombineWorkerManager _createCombineWorkerManager() {
142 2 : _isInitialized = true;
143 4 : return effectiveWorkerFactory.create()
144 2 : ..initialize(isolatesPrefix: defaultIsolatePrefix);
145 : }
146 : }
|