serverIsolateEntryPoint function

void serverIsolateEntryPoint(
  1. SendPort isolateToMainStream
)

Implementation

void serverIsolateEntryPoint(SendPort isolateToMainStream) {
  ReceivePort mainToIsolateStream = ReceivePort();
  isolateToMainStream.send(mainToIsolateStream.sendPort);
  IsolateInspectorServer? server;
  int? mainIsolateJSContextId;

  mainToIsolateStream.listen((data) {
    if (data is InspectorServerInit) {
      server = IsolateInspectorServer(data.port, data.address, data.bundleURL);
      server!._isolateToMainStream = isolateToMainStream;
      server!.onStarted = () {
        isolateToMainStream.send(InspectorServerStart());
      };
      server!.onFrontendMessage = (Map<String, dynamic>? frontEndMessage) {
        int? id = frontEndMessage!['id'];
        String _method = frontEndMessage['method'];
        Map<String, dynamic>? params = frontEndMessage['params'];

        List<String> moduleMethod = _method.split('.');
        String module = moduleMethod[0];
        String method = moduleMethod[1];

        // Runtime、Log、Debugger methods should handled on inspector isolate.
        if (module == 'Runtime' || module == 'Log' || module == 'Debugger') {
          server!.messageRouter(id, module, method, params);
        } else {
          isolateToMainStream.send(InspectorFrontEndMessage(id, module, method, params));
        }
      };
      server!.start();
      _inspectorServerMap[data.contextId] = server;
      mainIsolateJSContextId = data.contextId;
      initInspectorServerNativeBinding(data.contextId);
      attachInspector(data.contextId);
    } else if (server != null && server!.connected) {
      if (data is InspectorEvent) {
        server!.sendEventToFrontend(data);
      } else if (data is InspectorMethodResult) {
        server!.sendToFrontend(data.id, data.result);
      } else if (data is InspectorPostTaskMessage) {
        DynamicLibrary? nativeDynamicLibrary = getDynamicLibrary();
        if (nativeDynamicLibrary == null) return;
        final DartDispatchInspectorTask _dispatchInspectorTask = nativeDynamicLibrary
            .lookup<NativeFunction<NativeDispatchInspectorTask>>('dispatchInspectorTask')
            .asFunction();
        _dispatchInspectorTask(mainIsolateJSContextId, Pointer.fromAddress(data.context), Pointer.fromAddress(data.callback));
      } else if (data is InspectorReload) {
        attachInspector(data.contextId);
      }
    }
  });
}