Files
social-app/apps/lib/app/services/session_scope_manager.dart
T

43 lines
1.1 KiB
Dart

import '../../data/cache/cache_scope.dart';
import '../../data/cache/cache_store.dart';
class SessionScopeManager {
SessionScopeManager({required HybridCacheStore cacheStore})
: _cacheStore = cacheStore;
final HybridCacheStore _cacheStore;
String? _activeUserId;
Future<void> activate(String userId) async {
final normalizedUserId = userId.trim();
if (normalizedUserId.isEmpty) {
throw StateError('User id cannot be empty when activating cache scope');
}
final previousUserId = _activeUserId;
if (previousUserId == normalizedUserId) {
return;
}
_activeUserId = normalizedUserId;
CacheScope.configureProvider(() => 'user:$normalizedUserId');
if (previousUserId != null && previousUserId != normalizedUserId) {
await _clearUserCache(previousUserId);
}
}
Future<void> clearActiveUserScope() async {
final userId = _activeUserId;
_activeUserId = null;
CacheScope.resetProvider();
if (userId != null) {
await _clearUserCache(userId);
}
}
Future<void> _clearUserCache(String userId) {
return _cacheStore.clearByPrefix('cache:user:$userId:');
}
}