run static method
Runs a world script's body and serves whoever opened it, until they
close the world.
void main(List<String> args) => World.run(args, (w) async {
var server = await startServer(port: await w.freePort());
w.onClose(server.close);
w.person('Ana', app: Launch('Shop', knobs: {'server': '${server.url}'}));
});
Opened by flutterware, the script speaks to its owner and the people get
their apps. Run on its own — dart run worlds/pickup.dart — it prints
what it declares instead, which is how to debug the setup half without
launching anything; name=value arguments are its knob values, and
Ctrl-C closes it.
Implementation
static Future<void> run(
List<String> args,
FutureOr<void> Function(World w) body,
) async {
var socketPath = Platform.environment[worldSocketVariable];
if (socketPath == null) return _runAlone(args, body);
// Each action is a step, as a tap is: every request it sends says so.
HttpOverrides.global = StepStamping(StepStamping.zoneStep);
var socket = await Socket.connect(
InternetAddress(socketPath, type: InternetAddressType.unix),
0,
);
var lines = socket
.cast<List<int>>()
.transform(utf8.decoder)
.transform(const LineSplitter());
await serve(lines, (line) => socket.writeln(line), body);
await socket.flush();
await socket.close();
// Whatever the script left running — a server's idle keep-alive, a
// timer — is not a reason for the world to outlive its owner.
exit(0);
}