spawnHybridCode function

StreamChannel spawnHybridCode (String dartCode, { Object message, bool stayAlive: false })

Spawns a VM isolate that runs the given dartCode, which is loaded as the contents of a Dart library.

This allows browser tests to spawn servers with which they can communicate to test client/server interactions. It can also be used by VM tests to easily spawn an isolate.

The dartCode must define a top-level hybridMain() function that takes a StreamChannel argument and, optionally, an Object argument to which message will be passed. Note that message must be JSON-encodable. For example:

import "package:stream_channel/stream_channel.dart";

hybridMain(StreamChannel channel, Object message) {
  // ...
}

Returns a StreamChannel that's connected to the channel passed to hybridMain(). Only JSON-encodable objects may be sent through this channel. If the channel is closed, the hybrid isolate is killed. If the isolate is killed, the channel's stream will emit a "done" event.

Any unhandled errors loading or running the hybrid isolate will be emitted as errors over the channel's stream. Any calls to print() in the hybrid isolate will be printed as though they came from the test that created the isolate.

Code in the hybrid isolate is not considered to be running in a test context, so it can't access test functions like expect() and expectAsync().

By default, the hybrid isolate is automatically killed when the test finishes running. If stayAlive is true, it won't be killed until the entire test suite finishes running.

Note: If you use this API, be sure to add a dependency on the **stream_channel package, since you're using its API as well!

Implementation

StreamChannel spawnHybridCode(String dartCode,
    {Object message, bool stayAlive = false}) {
  var uri = Uri.dataFromString(dartCode,
      encoding: utf8, mimeType: 'application/dart');
  return _spawn(uri.toString(), message, stayAlive: stayAlive);
}