Implementation
Future<void> updateSession() async {
// add completion
sdk.eventManager.notifySessionExpired();
// If websocket exists, ws request to update session key
if (sdk.webSocket?.isConnected() == true) {
try {
await sdk.cmdManager
.sendCommand(Command.buildLOGIUpdateSessionKey(_accessToken));
} catch (e) {
logger.e('WS LOGI extend session Error: ' + e.toString());
throw WebSocketError(
message: 'LOGI extend session error: ' + e.toString());
} finally {
return;
}
}
final completer = Completer();
sessionUpdateCompleters.add(completer);
if (isRefreshingKey) {
return completer.future;
}
final appId = sdk.state.appId;
final accessToken = _accessToken;
if (appId == null || accessToken == null) throw UnauthorizeError();
final hasSessionHandler = sdk.eventManager.getSessionHandler() != null;
isRefreshingKey = true;
logger.i('Updating session with $_accessToken');
try {
final res = await sdk.api.send(
AppSessionKeyUpdateRequest(
appId: appId,
accessToken: accessToken,
expiringSession: hasSessionHandler,
),
);
isRefreshingKey = false;
logger.i('Updated session $res');
_applyRefreshedSessionKey(res);
return completer.future;
} on SBError catch (err) {
logger.w('Failed to update session sb $err');
isRefreshingKey = false;
if (err.code == ErrorCode.accessTokenNotValid) {
sdk.eventManager.notifySessionTokenRequired();
} else {
flushResultCompleters(SessionKeyRefreshFailedError());
sdk.eventManager.notifySessionError(SessionKeyRefreshFailedError());
}
return completer.future;
} catch (err) {
logger.w('Failed to update session $err');
isRefreshingKey = false;
flushResultCompleters(SessionKeyRefreshFailedError());
sdk.eventManager.notifySessionError(SessionKeyRefreshFailedError());
return completer.future;
}
}