onConnectionStateChanged method

  1. @override
Future<void> onConnectionStateChanged(
  1. ConnectionState? previous,
  2. ConnectionState? current,
  3. Connection connection
)
override

Called when connection state is changed

@param previous - old state @param current - new state @param connection - current connection

Implementation

@override
Future<void> onConnectionStateChanged(ConnectionState? previous, ConnectionState? current, Connection connection) async {
  // convert status
  PorterStatus s1 = PorterStatus.getStatus(previous);
  PorterStatus s2 = PorterStatus.getStatus(current);
  //
  //  1. callback when status changed
  //
  if (s1 != s2) {
    bool notFinished = s2 != PorterStatus.error;
    Porter? worker = await dock(connection, notFinished);
    if (worker == null) {
      // connection closed and porter removed
      return;
    }
    // callback for porter status
    await delegate?.onPorterStatusChanged(s1, s2, worker);
  }
  //
  //  2. heartbeat when connection expired
  //
  if (current?.index == ConnectionStateOrder.expired.index) {
    await heartbeat(connection);
  }
}