connect method

  1. @override
void connect(
  1. EventFluxConnectionType type,
  2. String url, {
  3. Map<String, String> header = const {'Accept' : 'text/event-stream', 'Cache-Control' : 'no-store'},
  4. dynamic onConnectionClose()?,
  5. bool autoReconnect = false,
  6. ReconnectConfig? reconnectConfig,
  7. required dynamic onSuccessCallback(
    1. EventFluxResponse?
    ),
  8. dynamic onError(
    1. EventFluxException
    )?,
  9. HttpClientAdapter? httpClient,
  10. Map<String, dynamic>? body,
  11. String? tag,
  12. bool logReceivedData = false,
  13. List<MultipartFile>? files,
  14. bool multipartRequest = false,
  15. WebConfig? webConfig,
  16. List<EventFluxInterceptor>? interceptors,
  17. Future<void>? abortTrigger,
})
override

Implementation

@override
void connect(
  EventFluxConnectionType type,
  String url, {
  Map<String, String> header = const {'Accept': 'text/event-stream', 'Cache-Control': 'no-store'},
  Function()? onConnectionClose,
  bool autoReconnect = false,
  ReconnectConfig? reconnectConfig,
  required Function(EventFluxResponse?) onSuccessCallback,
  Function(EventFluxException)? onError,
  HttpClientAdapter? httpClient,
  Map<String, dynamic>? body,
  String? tag,
  bool logReceivedData = false,
  List<MultipartFile>? files,
  bool multipartRequest = false,
  WebConfig? webConfig,
  List<EventFluxInterceptor>? interceptors,
  Future<void>? abortTrigger,
}) {

  if (kIsWeb && webConfig == null) {
    throw ArgumentError('WebConfig must be provided on web');
  }
  // This check prevents redundant connection requests when a connection is already in progress.
  if (_status == EventFluxStatus.connected ||
      _status == EventFluxStatus.connectionInitiated ||
      _status == EventFluxStatus.reconnecting) {
    eventFluxLog('Already Connection in Progress, Skipping redundant request',
        LogEvent.info, _tag);
    return;
  }
  _status = EventFluxStatus.connectionInitiated;

  /// Set the tag for logging purposes.
  _tag = tag;

  /// If autoReconnect is enabled and reconnectConfig is not provided, log an error and return.
  if (autoReconnect && reconnectConfig == null) {
    eventFluxLog(
      "ReconnectConfig is required when autoReconnect is enabled",
      LogEvent.error,
      tag,
    );
    return;
  }

  eventFluxLog("$_status", LogEvent.info, _tag);

  if (reconnectConfig != null) {
    _reconnectStrategy.initialize(reconnectConfig);
  }

  _isExplicitDisconnect = false;

  final config = ConnectionConfig(
    type: type,
    url: url,
    header: header,
    autoReconnect: autoReconnect,
    reconnectConfig: reconnectConfig,
    onSuccessCallback: onSuccessCallback,
    onError: onError,
    onConnectionClose: onConnectionClose,
    httpClient: httpClient,
    body: body,
    tag: tag,
    logReceivedData: logReceivedData,
    files: files,
    multipartRequest: multipartRequest,
    webConfig: webConfig,
    interceptors: interceptors,
    abortTrigger: abortTrigger,
  );

  _start(config);
}