sendCmd method
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 processdata
: Optional data payloadtransfer
: 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());
}
}