forceNewSession method

Future<String?> forceNewSession([
  1. String? customSessionId
])

Fuerza la creación de una nueva sesión manualmente Forces the creation of a new session manually If customSessionId is provided, that ID will be used; otherwise, one is generated automatically

Implementation

Future<String?> forceNewSession([String? customSessionId]) async {
  if (!_isInitialized || _idManager == null) {
    ObslyLogger.warn('SessionController not initialized');
    return null;
  }

  final oldSessionId = _idManager!.getSessionId();

  // Emit manually closed session event
  _sessionEventController ??= StreamController<SessionEvent>.broadcast();
  _sessionEventController!.add(SessionEvent.sessionEnded(oldSessionId));

  // Cancel previous timer if it exists
  _sessionTimer?.cancel();

  if (customSessionId != null) {
    // Create session with custom ID
    await _idManager!.overrideSessionId(customSessionId);

    // Set current timestamp
    final now = DateTime.now().millisecondsSinceEpoch;
    if (DefaultConfiguration.persistSessionTimestamp) {
      await _idManager!.setSessionTimestamp(now);
    }

    // Schedule timeout for new session
    _scheduleSessionTimeout(Duration(minutes: _sessionMaxLengthMins));

    // Emit new session event
    final finalSessionId = _idManager!.getSessionId();
    _sessionEventController ??= StreamController<SessionEvent>.broadcast();
    _sessionEventController!.add(SessionEvent.sessionCreated(finalSessionId));

    ObslyLogger.debug('New session created with custom ID: $finalSessionId (timeout: ${_sessionMaxLengthMins}min)');

    return finalSessionId;
  } else {
    // Create session with automatically generated ID
    await _createNewSession();
    return _idManager!.getSessionId();
  }
}