Skip to content

Commit

Permalink
Properly handle platform worker thread parameter + getter in generate…
Browse files Browse the repository at this point in the history
…d worker/worker pool when `with_finalizers` is enabled (fix for #5)
  • Loading branch information
d-markey committed Jun 8, 2023
1 parent 7092f2f commit a20b0a9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1

- Properly handle platform worker thread parameter + getter in generated worker/worker pool when `with_finalizers` is enabled (fix for https://github.com/d-markey/squadron_builder/issues/5).

## 2.2.0

- Automatically discover Squadron capabilities (`WorkerRequest`/`WorkerResponse` serialization type + availability of `EntryPoint` and `PlatformWorkerHook`).
Expand Down
25 changes: 12 additions & 13 deletions example/generated/my_service.worker.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,15 @@ void main() async {

Future<PerfCounters> runService(
MyServiceConfig<bool> trace, MyServiceConfig<int> workloadDelay) async {
var counters = await testWith(MyService(trace, workloadDelay: workloadDelay));
var counters = await testWith(MyService(trace, workloadDelay));
await Future.delayed(Duration.zero);
counters += await testWith(MyService(trace, workloadDelay: workloadDelay));
counters += await testWith(MyService(trace, workloadDelay));
return counters / 2;
}

Future<PerfCounters> runWorker(
MyServiceConfig<bool> trace, MyServiceConfig<int> workloadDelay) async {
final worker = MyServiceWorker(trace,
workloadDelay: workloadDelay,
final worker = MyServiceWorker(trace, workloadDelay,
platformWorkerHook: (w) => Squadron.info(
'Standalone worker ready (platform worker is a ${w.runtimeType})'));

Expand All @@ -116,8 +115,7 @@ Future<PerfCounters> runWorker(

Future runPool(
MyServiceConfig<bool> trace, MyServiceConfig<int> workloadDelay) async {
final pool = MyServiceWorkerPool(trace,
workloadDelay: workloadDelay,
final pool = MyServiceWorkerPool(trace, workloadDelay,
concurrencySettings: ConcurrencySettings(
minWorkers: 5,
maxWorkers: 5,
Expand Down
2 changes: 1 addition & 1 deletion example/my_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ part 'generated/my_service.worker.g.dart';
@SquadronService()
@UseLogger(ParentSquadronLogger)
class MyService extends WorkerService with $MyServiceOperations {
MyService(this._trace, {MyServiceConfig<int>? workloadDelay})
MyService(this._trace, MyServiceConfig<int>? workloadDelay)
: _delay = Duration(microseconds: workloadDelay?.value ?? 50);

final MyServiceConfig<bool> _trace;
Expand Down
1 change: 1 addition & 0 deletions lib/src/_overrides.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const workerOverrides = {
'Duration get upTime': 'upTime',
'String get workerId': 'workerId',
'int get workload': 'workload',
'PlatformWorkerHook? get platformWorkerHook': 'platformWorkerHook',
// worker control
'Future<Channel> start()': 'start()',
'void stop()': 'stop()',
Expand Down
48 changes: 31 additions & 17 deletions lib/src/worker_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,18 @@ class WorkerAssets {

yield _generateServiceInitializer();

final withPlatformWorkerHook = _squadron?.hasPlatformWorkerHook ?? false;

final generateWorker =
withFinalizers ? _generateFinalizableWorker : _generateWorker;

var activationsArgs = service.serializedArguments.isEmpty
? serviceActivator
: '$serviceActivator, args: [${service.serializedArguments}]';

final platformWorkerHook = _squadron?.hasPlatformWorkerHook ?? false;

var workerParameters = service.parameters;
if (platformWorkerHook) {

if (withPlatformWorkerHook) {
// worker constructors also have an optional PlatformWorkerHook parameter
activationsArgs += ', platformWorkerHook: platformWorkerHook';
if (workerParameters.isEmpty) {
Expand All @@ -200,8 +201,8 @@ class WorkerAssets {
}
}

yield generateWorker(
workerParameters, activationsArgs, commands, unimplemented);
yield generateWorker(workerParameters, activationsArgs,
withPlatformWorkerHook, commands, unimplemented);

if (service.pool) {
final generatePool =
Expand All @@ -210,27 +211,27 @@ class WorkerAssets {
var poolParameters = service.parameters;
// worker pool constructors also have an optional ConcurrencySettings parameter
if (poolParameters.isEmpty) {
poolParameters = platformWorkerHook
poolParameters = withPlatformWorkerHook
? '{ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook}'
: '{ConcurrencySettings? concurrencySettings}';
} else if (poolParameters.endsWith('}')) {
poolParameters = poolParameters.substring(0, poolParameters.length - 1);
poolParameters += platformWorkerHook
poolParameters += withPlatformWorkerHook
? ', ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook}'
: ', ConcurrencySettings? concurrencySettings}';
} else if (poolParameters.endsWith(']')) {
poolParameters = poolParameters.substring(0, poolParameters.length - 1);
poolParameters += platformWorkerHook
poolParameters += withPlatformWorkerHook
? ', ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook]'
: ', ConcurrencySettings? concurrencySettings]';
} else {
poolParameters += platformWorkerHook
poolParameters += withPlatformWorkerHook
? ', {ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook}'
: ', {ConcurrencySettings? concurrencySettings}';
}

yield generatePool(
poolParameters, platformWorkerHook, commands, unimplemented);
poolParameters, withPlatformWorkerHook, commands, unimplemented);
}
}

Expand All @@ -257,6 +258,7 @@ class WorkerAssets {
String _generateWorker(
String workerParameters,
String activationsArgs,
bool platformWorkerHook,
List<SquadronMethodAnnotation> commands,
List<SquadronMethodAnnotation> unimplemented) =>
'''
Expand All @@ -281,11 +283,22 @@ class WorkerAssets {
''';

String _generateFinalizableWorker(
String workerParameters,
String activationsArgs,
List<SquadronMethodAnnotation> commands,
List<SquadronMethodAnnotation> unimplemented) =>
'''
String workerParameters,
String activationsArgs,
bool withPlatformWorkerHook,
List<SquadronMethodAnnotation> commands,
List<SquadronMethodAnnotation> unimplemented) {
var nonFormalArguments = service.nonFormalArguments;
if (withPlatformWorkerHook) {
if (nonFormalArguments.isEmpty) {
nonFormalArguments = 'platformWorkerHook: platformWorkerHook';
} else if (service.parameters.endsWith(']')) {
nonFormalArguments += ', platformWorkerHook';
} else {
nonFormalArguments += ', platformWorkerHook: platformWorkerHook';
}
}
return '''
// Worker for ${service.name}
class _$workerClassName
extends Worker with $operationsMixinName
Expand All @@ -310,7 +323,7 @@ class WorkerAssets {
// Finalizable worker wrapper for ${service.name}
class $workerClassName implements _$workerClassName {
$workerClassName(${service.nonFormalParameters}) : _worker = _$workerClassName(${service.nonFormalArguments}) {
$workerClassName($workerParameters) : _worker = _$workerClassName($nonFormalArguments) {
_finalizer.attach(this, _worker, detach: _worker._detachToken);
}
Expand Down Expand Up @@ -339,6 +352,7 @@ class WorkerAssets {
${workerOverrides.entries.map((e) => _forwardOverride(e.key, '_worker', e.value)).join('\n\n')}
}
''';
}

String _generateWorkerPool(
String poolParameters,
Expand Down Expand Up @@ -439,7 +453,7 @@ class WorkerAssets {
implements ${service.name} {
_$workerPoolClassName($poolParameters) : super(
() => $workerClassName($serviceArguments}),
() => $workerClassName($serviceArguments),
concurrencySettings: concurrencySettings);
${service.fields.values.map(_generateField).join('\n\n')}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: squadron_builder
description: Dart code generator for Squadron workers. Implement your worker service and let squadron_builder bridge the gap with Web Workers and Isolates!
version: 2.2.0
version: 2.2.1
homepage: https://github.com/d-markey/squadron_builder
repository: https://github.com/d-markey/squadron_builder

Expand Down

0 comments on commit a20b0a9

Please sign in to comment.