Worker constructor

Worker(
  1. Client client,
  2. String? url, {
  3. required void onConsoleApiCalled(
    1. ConsoleAPICalledEventType,
    2. List<JsHandle>,
    3. StackTraceData?
    )?,
  4. required void onExceptionThrown(
    1. ExceptionThrownEvent
    )?,
})

Implementation

Worker(this.client, this.url,
    {required void Function(
            ConsoleAPICalledEventType, List<JsHandle>, StackTraceData?)?
        onConsoleApiCalled,
    required void Function(ExceptionThrownEvent)? onExceptionThrown}) {
  var runtimeApi = RuntimeApi(client);

  late JsHandle Function(RemoteObject) jsHandleFactory;
  runtimeApi.onExecutionContextCreated.listen((event) {
    var executionContext = ExecutionContext(client, event, null);
    jsHandleFactory =
        (remoteObject) => JsHandle(executionContext, remoteObject);
    _executionContextCompleter.complete(executionContext);
  });

  runtimeApi.onConsoleAPICalled.listen((event) {
    if (onConsoleApiCalled != null) {
      onConsoleApiCalled(event.type, event.args.map(jsHandleFactory).toList(),
          event.stackTrace);
    }
  });
  runtimeApi.onExceptionThrown.listen((event) {
    if (onExceptionThrown != null) {
      onExceptionThrown(event);
    }
  });

  runtimeApi.enable().catchError((e) {
    // This might fail if the target is closed before we recieve all execution contexts.
  });
}