init method

Future<SendPort> init({
  1. required IsolateDataRecived<T> onDataRecived,
  2. required IsolateDataRecived<T> onIsolateDataRecived,
  3. Object? initialMessage,
  4. bool queueMode = false,
})

Implementation

Future<SendPort> init({
  required IsolateDataRecived<T> onDataRecived,
  required IsolateDataRecived<T> onIsolateDataRecived,
  Object? initialMessage,
  bool queueMode = false,
}) async {
  _mainReceivePort = ReceivePort();
  _isolate = await Isolate.spawn(
    _isolateEntryPoint,
    _BuildIsolateMessageArguments<T>(
      mainSendPort: _mainReceivePort.sendPort,
      onDataRecived: onIsolateDataRecived,
      queueMode: queueMode,
    ),
  );
  _mainListenSubscription = _mainReceivePort.listen((data) async {
    if (data is SendPort) {
      _isolateSendPort = data;
      _completer.complete(_isolateSendPort);
      if (initialMessage != null) _isolateSendPort.send(initialMessage);
    } else if (data is T) {
      final future = onDataRecived(data, _isolateSendPort);
      if (queueMode) await future;
    }
  });
  return _completer.future;
}