Skip to content

Commit b27cbbb

Browse files
nex3ntkme
authored andcommitted
Make the two ReusableWorker interfaces match
1 parent a8e2fa0 commit b27cbbb

File tree

6 files changed

+17
-62
lines changed

6 files changed

+17
-62
lines changed

lib/src/embedded/js/executable.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import 'package:stream_channel/stream_channel.dart';
66

7+
import '../compilation_dispatcher.dart';
78
import '../options.dart';
89
import '../util/length_delimited_transformer.dart';
910
import '../worker_dispatcher.dart';
10-
import '../worker_entrypoint.dart';
1111
import 'io.dart';
1212
import 'sync_receive_port.dart';
1313
import 'worker_threads.dart';
@@ -21,7 +21,7 @@ void main(List<String> args) {
2121
.listen();
2222
} else {
2323
var port = workerData! as MessagePort;
24-
workerEntryPoint(JSSyncReceivePort(port), JSSendPort(port));
24+
CompilationDispatcher(JSSyncReceivePort(port), JSSendPort(port)).listen();
2525
}
2626
}
2727
}

lib/src/embedded/js/reusable_worker.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import 'dart:async';
66
import 'dart:js_interop';
77
import 'dart:typed_data';
88

9+
import 'package:node_interop/node_interop.dart';
10+
911
import 'js.dart';
1012
import 'sync_message_port.dart';
1113
import 'worker_threads.dart';
1214

13-
/// The entrypoint for a [ReusableWorker].
14-
///
15-
/// This must return a Record of filename and argv for creating the Worker.
16-
typedef ReusableWorkerEntryPoint = (String, JSArray<JSAny?>) Function();
17-
1815
class ReusableWorker {
1916
/// The worker.
2017
final Worker _worker;
@@ -34,17 +31,17 @@ class ReusableWorker {
3431
ReusableWorker._(
3532
this._worker, this._sendPort, this._receivePort, this._subscription);
3633

37-
/// Spawns a [ReusableWorker] that runs the the entrypoint script.
38-
static Future<ReusableWorker> spawn(ReusableWorkerEntryPoint entryPoint,
39-
{Function? onError}) async {
40-
var (filename, argv) = entryPoint();
34+
/// Spawns a [ReusableWorker].
35+
static Future<ReusableWorker> spawn({Function? onError}) async {
36+
var filename = process.argv[1] as String;
37+
var argv = [for (var arg in process.argv.skip(2)) (arg as String).toJS];
4138
var channel = SyncMessagePort.createChannel();
4239
var worker = Worker(
4340
filename,
4441
WorkerOptions(
4542
workerData: channel.port2,
4643
transferList: [channel.port2].toJS,
47-
argv: argv));
44+
argv: argv.toJS));
4845
var controller = StreamController<dynamic>(sync: true);
4946
var sendPort = SyncMessagePort(channel.port1);
5047
var receivePort = channel.port1;

lib/src/embedded/js/worker_entrypoint.dart

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/src/embedded/vm/reusable_worker.dart

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@ import 'package:native_synchronization/mailbox.dart';
1010
import 'package:native_synchronization/sendable.dart';
1111

1212
import '../sync_receive_port.dart';
13-
14-
/// The entrypoint for a [ReusableWorker].
15-
///
16-
/// This must be a static global function. It's run when the isolate is spawned,
17-
/// and is passed a [Mailbox] that receives messages from [ReusableWorker.send]
18-
/// and a [SendPort] that sends messages to the [ReceivePort] listened by
19-
/// [ReusableWorker.borrow].
20-
///
21-
/// If the [sendPort] sends a message before [ReusableWorker.borrow] is called,
22-
/// this will throw an unhandled [StateError].
23-
typedef ReusableWorkerEntryPoint = FutureOr<void> Function(
24-
SyncReceivePort receivePort, SendPort sendPort);
13+
import '../compilation_dispatcher.dart';
2514

2615
class ReusableWorker {
2716
/// The wrapped isolate.
@@ -46,15 +35,13 @@ class ReusableWorker {
4635
Function? onError,
4736
}) : _subscription = _receivePort.listen(_defaultOnData, onError: onError);
4837

49-
/// Spawns a [ReusableWorker] that runs the given [entryPoint].
50-
static Future<ReusableWorker> spawn(
51-
ReusableWorkerEntryPoint entryPoint, {
38+
/// Spawns a [ReusableWorker].
39+
static Future<ReusableWorker> spawn({
5240
Function? onError,
5341
}) async {
5442
var mailbox = Mailbox();
5543
var receivePort = ReceivePort();
5644
var isolate = await Isolate.spawn(_isolateMain, (
57-
entryPoint,
5845
mailbox.asSendable,
5946
receivePort.sendPort,
6047
));
@@ -107,9 +94,9 @@ void _defaultOnData(dynamic _) {
10794
throw StateError("Shouldn't receive a message before being borrowed.");
10895
}
10996

110-
void _isolateMain(
111-
(ReusableWorkerEntryPoint, Sendable<Mailbox>, SendPort) message,
112-
) {
113-
var (entryPoint, sendableMailbox, sendPort) = message;
114-
entryPoint(MailboxSyncReceivePort(sendableMailbox.materialize()), sendPort);
97+
void _isolateMain((Sendable<Mailbox>, SendPort) message) {
98+
var (sendableMailbox, sendPort) = message;
99+
CompilationDispatcher(
100+
MailboxSyncReceivePort(sendableMailbox.materialize()), sendPort)
101+
.listen();
115102
}

lib/src/embedded/worker_dispatcher.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import 'util/proto_extensions.dart';
1515
import 'utils.dart';
1616
import 'vm/concurrency.dart' if (dart.library.js) 'js/concurrency.dart';
1717
import 'vm/reusable_worker.dart' if (dart.library.js) 'js/reusable_worker.dart';
18-
import 'worker_entrypoint.dart'
19-
if (dart.library.js) 'js/worker_entrypoint.dart';
2018

2119
/// A class that dispatches messages between the host and various workers that
2220
/// are each running an individual compilation.
@@ -129,7 +127,6 @@ class WorkerDispatcher {
129127
_inactiveWorkers.remove(worker);
130128
} else {
131129
var future = ReusableWorker.spawn(
132-
workerEntryPoint,
133130
onError: (Object error, StackTrace stackTrace) {
134131
_handleError(error, stackTrace);
135132
},

lib/src/embedded/worker_entrypoint.dart

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)