Skip to content

Commit

Permalink
Generailze reusuable worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Oct 29, 2024
1 parent aa0acb9 commit 966335e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/src/embedded/isolate_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:stream_channel/stream_channel.dart';

import 'concurrency.dart' if (dart.library.js) 'js/concurrency.dart';
import 'embedded_sass.pb.dart';
import 'isolate_main.dart';
import 'isolate_main.dart' if (dart.library.js) 'js/isolate_main.dart';
import 'reusable_isolate.dart' if (dart.library.js) 'js/reusable_isolate.dart';
import 'util/proto_extensions.dart';
import 'utils.dart';
Expand Down
14 changes: 14 additions & 0 deletions lib/src/embedded/js/isolate_main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:js_interop';

import 'js.dart';

@JS('process.argv')
external JSArray<JSAny?> get _argv;

(String, JSArray<JSString>) isolateMain() {
return (_argv[1]! as String, _argv.slice(2) as JSArray<JSString>);
}
14 changes: 14 additions & 0 deletions lib/src/embedded/js/js.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:js_interop';

extension JSTypedArrayExtension on JSTypedArray {
external JSArrayBuffer get buffer;
}

extension JSArrayExtension<T extends JSAny?> on JSArray<T> {
external T operator [](int index);
external JSArray<T> slice([int start, int end]);
}
17 changes: 10 additions & 7 deletions lib/src/embedded/js/reusable_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import 'dart:js_interop';
import 'dart:typed_data';

import '../../io.dart';
import 'js.dart';
import 'sync_message_port.dart';
import 'worker_threads.dart';

@JS('process.argv')
external JSArray<JSString> get _argv;
/// The entrypoint for a [ReusableIsolate].
///
/// This must return a Record of filename and argv for creating the Worker.
typedef ReusableIsolateEntryPoint = (String, JSArray<JSAny?>) Function();

class ReusableIsolate {
/// The worker.
Expand All @@ -33,17 +36,17 @@ class ReusableIsolate {
this._worker, this._sendPort, this._receivePort, this._subscription);

/// Spawns a [ReusableIsolate] that runs the the entrypoint script.
static Future<ReusableIsolate> spawn(Function unused,
static Future<ReusableIsolate> spawn(ReusableIsolateEntryPoint entryPoint,
{Function? onError}) async {
var (filename, argv) = entryPoint();
var channel = SyncMessagePort.createChannel();
var argv = _argv.toDart;
var worker = Worker(
argv[1].toDart,
filename,
WorkerOptions(
workerData: channel.port2,
transferList: [channel.port2].toJS,
argv: argv.sublist(2).toJS));
worker.on(
argv: argv));
worker.once(
'exit',
(int code) {
// Worker exit code 1 means it is killed by worker.terminate()
Expand Down
1 change: 1 addition & 0 deletions lib/src/embedded/js/sync_receive_port.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:typed_data';

import '../sync_receive_port.dart';
import 'isolate.dart';
import 'js.dart';
import 'sync_message_port.dart';
import 'worker_threads.dart';

Expand Down
10 changes: 3 additions & 7 deletions lib/src/embedded/js/worker_threads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

import 'dart:js_interop';

extension JSUint8ArrayExt on JSUint8Array {
external JSObject get buffer;
}

@JS('worker_threads.isMainThread')
external bool get isMainThread;

Expand All @@ -17,14 +13,14 @@ external JSAny? get workerData;
@JS('worker_threads.Worker')
extension type Worker._(JSObject _) implements JSObject {
external Worker(String filename, WorkerOptions options);
external void on(String type, JSFunction listener);
external void once(String type, JSFunction listener);
external void terminate();
}

@JS()
extension type WorkerOptions._(JSObject _) implements JSObject {
external WorkerOptions(
{JSArray<JSAny> argv,
{JSArray<JSAny?> argv,
JSObject env,
bool eval,
JSArray<JSString> execArgv,
Expand All @@ -35,7 +31,7 @@ extension type WorkerOptions._(JSObject _) implements JSObject {
bool trackUnmanagedFds,
JSArray<JSAny> transferList,
ResourceLimits resourceLimits});
external JSArray<JSAny> get argv;
external JSArray<JSAny?> get argv;
external JSObject get env;
external bool get eval;
external JSArray<JSString> get execArgv;
Expand Down

0 comments on commit 966335e

Please sign in to comment.