initialize method
Initializes the intercom.
This is the keypoint of all plugin system, where Isolate for plugin is spawned.
There's an option to pass args (refer to Isolate.spawnUri).
After isolate has been spawned, it waits for plugin to send its SendPort to support
two-sided communication. If not-null timeout was given, initialization fails
with TimeoutException if plugin didn't send its SendPort before the timeout.
Implementation
@override
Future<void> initialize(
{List<String> args = const [],
Duration timeout = const Duration(seconds: 2)}) async {
var comp = Completer<void>();
_inPort = ReceivePort();
isolate = await Isolate.spawnUri(uri, args, _inPort.sendPort);
var lsid = listener.prependListener((message) {
if (!_initialized) {
if (message is SendPort) {
_initialized = true;
_outPort = message;
comp.complete();
return false;
}
}
return true;
});
_inPort.listen(_onEvent);
return comp.future.timeout(timeout).then((value) {
listener.removeListener(lsid);
return value;
}).onError((error, stackTrace) {
listener.removeListener(lsid);
_inPort.close();
if (error != null) throw error;
});
}