waitForCallsSdk method
Waits for the Calls SDK to be ready AND the user to be logged in. If already ready, returns immediately. Other components (e.g. CallLogsBloc, VoipCallHandler) should call this instead of initializing the SDK themselves.
If init hasn't been called yet (e.g. because _initiateAfterLogin
fired it without await), this method polls briefly, then falls back
to calling init directly so the caller never proceeds with an
uninitialized Calls SDK.
Implementation
Future<void> waitForCallsSdk() async {
if (_callsSdkReady && _callsSdkLoginReady) return;
// If init() hasn't been called yet, poll briefly until it starts.
if (!_initialized && _callsSdkCompleter == null) {
for (int i = 0; i < 30; i++) {
await Future.delayed(const Duration(milliseconds: 200));
if (_callsSdkCompleter != null || _callsSdkReady) break;
}
}
// Fallback: if init() still hasn't started after polling, trigger it
// directly. This covers the race where _initiateAfterLogin() fired
// init() without await and the future hasn't been scheduled yet, or
// where init() was never called at all.
if (!_initialized && _callsSdkCompleter == null) {
developer.log(
'CallEventService: waitForCallsSdk — init() never started, '
'triggering it now',
);
final settings = CometChatUIKit.authenticationSettings;
if (settings != null &&
settings.appId != null &&
settings.region != null) {
await init(
configuration: settings.callingConfiguration,
);
}
// After init completes, check again
if (_callsSdkReady && _callsSdkLoginReady) return;
}
// Wait for SDK init
if (!_callsSdkReady && _callsSdkCompleter != null) {
await _callsSdkCompleter!.future.timeout(
const Duration(seconds: 15),
onTimeout: () {
developer.log('CallEventService: waitForCallsSdk (init) timed out');
},
);
}
// Wait for SDK login — the completer may not exist yet if init() is
// still running (it creates _callsSdkLoginCompleter after _initCallsSdk
// finishes). Poll briefly for it.
if (!_callsSdkLoginReady && _callsSdkLoginCompleter == null) {
for (int i = 0; i < 25; i++) {
await Future.delayed(const Duration(milliseconds: 200));
if (_callsSdkLoginCompleter != null || _callsSdkLoginReady) break;
}
}
if (!_callsSdkLoginReady && _callsSdkLoginCompleter != null) {
await _callsSdkLoginCompleter!.future.timeout(
const Duration(seconds: 15),
onTimeout: () {
developer.log('CallEventService: waitForCallsSdk (login) timed out');
},
);
}
if (!_callsSdkReady || !_callsSdkLoginReady) {
developer.log(
'CallEventService: waitForCallsSdk finished but SDK not fully ready '
'(init=$_callsSdkReady, login=$_callsSdkLoginReady)',
);
}
}