encodeModelInIsolate function

Future<Uint8List> encodeModelInIsolate(
  1. ModelWriteRequest request
)

Encodes a model on a background isolate — the write-side mirror of decodeModelInIsolate (model_loader.dart), for the same reason: writing a real model costs tens of milliseconds, which is jank on the UI isolate however fast the encoder is. See that function's own doc comment for why the web falls back to running in place instead.

The document travels whole, not by reference. Isolate.run sends request the same way decodeModelInIsolate sends its own request and receives a ModelDocument back — plain data, typed lists included, with nothing here holding a closure or a platform resource that would refuse to cross.

Implementation

Future<Uint8List> encodeModelInIsolate(ModelWriteRequest request) async {
  // The web has no isolates. `dart:isolate` compiles there as a stub, so
  // this fails at run time rather than build time if it were tried —
  // encoding in place is what the name promises not to do and the only
  // thing available.
  if (_isWeb) return encodeModel(request);

  final task = developer.TimelineTask()
    ..start(
      'encodeModelInIsolate',
      arguments: <String, Object?>{'format': request.format.name},
    );
  try {
    return await runInBackground(() => encodeModel(request));
  } finally {
    task.finish();
  }
}