import 'dart:isolate';
import 'package:function_types/function_types.dart';
import 'package:synchronized/synchronized.dart';
// This doesn't really work properly for all functions
// Waiting for this to merge - https://github.com/dart-lang/sdk/issues/36097
class WorkerQueue {
Isolate? _isolate;
SendPort? _sendPort;
var _receivePort = ReceivePort();
var _loadingLock = Lock();
Func1 func;
WorkerQueue(this.func);
Future _initIsolate() async {
if (_isolate != null && _sendPort != null) return;
return await _loadingLock.synchronized(() async {
if (_isolate != null && _sendPort != null) return;
if (_isolate != null) {
_isolate!.kill(priority: Isolate.immediate);
_isolate = null;
}
_isolate = await Isolate.spawn(_isolateMain, _receivePort.sendPort);
var data = await _receivePort.first;
assert(data is SendPort);
_sendPort = data as SendPort;
});
}
Future