Files

67 lines
1.8 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/servers/server_scope.dart';
/// Persistence for acknowledgements made while the server could not take
/// them (offline, or a server without `ack_attention`). The value is the
/// opaque string from `AttentionAcks.encode`; both directions may fail, and a
/// failure only means the queue lives in memory for this session.
abstract interface class AttentionAckQueueStore {
Future<String?> read();
Future<bool> write(String value);
}
/// `shared_preferences` storage scoped to the server, like the other device
/// caches. An unavailable plugin returns null/false instead of throwing.
class SharedPreferencesAttentionAckQueue implements AttentionAckQueueStore {
const SharedPreferencesAttentionAckQueue({this.serverId = ''});
static const key = 'pipelines.next.attention.pending_acks.v1';
final String serverId;
String get _key => scopedServerKey(key, serverId);
Future<SharedPreferences?> _preferences() async {
try {
return await SharedPreferences.getInstance();
} catch (_) {
return null;
}
}
@override
Future<String?> read() async {
try {
return (await _preferences())?.getString(_key);
} catch (_) {
return null;
}
}
@override
Future<bool> write(String value) async {
try {
return await (await _preferences())?.setString(_key, value) ?? false;
} catch (_) {
return false;
}
}
}
/// An in-memory queue for tests and isolated fixtures.
class MemoryAttentionAckQueue implements AttentionAckQueueStore {
MemoryAttentionAckQueue({this.stored});
String? stored;
@override
Future<String?> read() async => stored;
@override
Future<bool> write(String value) async {
stored = value;
return true;
}
}