sendCmd method

void sendCmd(
  1. String command, {
  2. Map<String, Object?>? data,
  3. JSObject? transfer,
})

Send a structured command to the Web Worker.

Convenience method for sending commands with optional data payload and transferable objects. Handles JS interop automatically.

Parameters:

  • command: Command string for the worker to process
  • data: Optional data payload
  • transfer: Optional transferable objects (ArrayBuffers, etc.)

Example:

// Send image processing command with transfer
final imageBuffer = await loadImageBuffer();
workerPort.sendCmd(
  'process_image',
  data: {'filters': ['blur', 'sharpen']},
  transfer: imageBuffer, // Transfer ownership for performance
);

Implementation

void sendCmd(
  String command, {
  Map<String, Object?>? data,
  JSObject? transfer,
}) {
  final payload = <String, Object?>{...?data, 'command': command};
  if (transfer != null) {
    postMessage(payload.jsify(), transfer);
  } else {
    postMessage(payload.jsify());
  }
}