start method
dynamic
start()
Implementation
start() async {
if (_tempPath == null) {
throw "无法获取临时存储目录,请联系官方。";
}
Directory(_tempPath!).createSync(recursive: true);
Isolate.spawn((hostSendPort) async {
Logger.logLevel = LogLevel.info;
final _tempPath = getTempDirectoryPath();
final clientPort = ReceivePort();
hostSendPort.send(clientPort.sendPort);
DevServer.shared.addListener(() {
hostSendPort.send(
{"serverConnected": DevServer.shared._activeClient != null},
);
});
DevServer.shared.start();
DevServer.shared.eventListenner = (p0, p1) {
hostSendPort.send(
json.encode({"msgType": "event", "method": p0, "params": p1}),
);
};
clientPort.asBroadcastStream().listen((hostMessage) async {
if (hostMessage is String) {
final hostMsgObj = json.decode(hostMessage);
if (!(hostMsgObj is Map)) return;
if (hostMsgObj['cmd'] == "invokeMethod") {
final id = hostMsgObj["id"] as String;
final result = await DevServer.shared.invokeMethod(
hostMsgObj['method'],
hostMsgObj['params'],
);
File("$_tempPath/data_$id").writeAsStringSync(json.encode(result));
} else if (hostMsgObj['cmd'] == "stop") {
DevServer.shared.stop();
Isolate.exit();
}
}
});
}, hostPort.sendPort);
final events = StreamQueue<dynamic>(hostPort);
hostSendPort = await events.next;
while (true) {
final isolateMsg = await events.next;
if (isolateMsg is Map && isolateMsg["serverConnected"] is bool) {
serverConnected = isolateMsg["serverConnected"];
notifyListeners();
}
if (isolateMsg is String) {
try {
final msg = json.decode(isolateMsg);
Logger.debug('MPJS: receive event => $msg');
if (msg["msgType"] == "event") {
eventListenner?.call(msg["method"], msg["params"]);
}
} catch (e) {}
}
}
}