Batch per-frame persistence so busy meshes don't stall the UI isolate
Every received packet copy re-saved its channel's whole history, every unread change re-saved the channel list, and adverts/path records re-saved full contact and path-history blobs. Under load the SharedPreferences backlog starved the Dart event loop (frames and timers stopped), the radio's offline queue overflowed, and CPU stayed pinned after traffic ended. - Add CoalescedWrites: keeps the latest write per key and runs them once per window (750 ms; 5 s for path history). Writes start synchronously so stores still read the current scoped key. - Flush before the app is backgrounded, on disconnect, before switching radios, before the channel slot map changes, before channel history reloads, before deleteChannel drops a list, and before path history reloads an evicted contact. - Cap in-memory channel history at ChannelMessageStore.maxStoredMessages; storage already kept only that many, but the list grew unbounded during a session and every save grew with it. - Cache Contact.publicKeyHex and build pubKeyToHex from a hex-digit lookup; it was rebuilt on every equality/hash and per-advert contact scan. Load test (fake TCP radio, 350 contacts, 17 channels, 10-150 packets/s, Linux profile build): CPU at 10 packets/s 104% -> 45%, recovers to idle after traffic instead of staying at 160%, and queued messages drained went from 555 to 3237 over the same run.
This commit is contained in:
1 parent
eab1ad2964
commit
603172b3ec
10 files changed
+365
-29
No files matched your search
@@ -67,6 +67,7 @@ import 'meshcore_uuids.dart';
|
||||
import 'meshcore_protocol.dart';
|
||||
import 'pending_command_replies.dart';
|
||||
import 'raw_packet.dart';
|
||||
import '../utils/coalesced_writes.dart';
|
||||
|
||||
class DirectRepeater {
|
||||
static const int maxAgeMinutes = 30; // Max age for direct repeater info
|
||||
@@ -479,6 +480,9 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
BleDebugLogService? _bleDebugLogService;
|
||||
AppDebugLogService? _appDebugLogService;
|
||||
final ChannelMessageStore _channelMessageStore = ChannelMessageStore();
|
||||
// Per-frame persistence (channel history, conversations, contact lists) is
|
||||
// batched here; see [CoalescedWrites].
|
||||
final CoalescedWrites _coalescedWrites = CoalescedWrites();
|
||||
final MessageStore _messageStore = MessageStore();
|
||||
final ChannelOrderStore _channelOrderStore = ChannelOrderStore();
|
||||
final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore();
|
||||
@@ -988,9 +992,40 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
Future<void> _saveChannelMessages(int slot, List<ChannelMessage> messages) {
|
||||
final id = _channelIdForSlot(slot);
|
||||
if (id == null) return Future<void>.value();
|
||||
_coalescedWrites.cancel('channel:$id');
|
||||
return _channelMessageStore.saveChannelMessages(id, messages);
|
||||
}
|
||||
|
||||
/// Queues a save of [slot]'s history. The write looks the channel up again
|
||||
/// when it runs, so it persists whatever the history is by then and skips a
|
||||
/// channel that was removed in the meantime.
|
||||
void _scheduleChannelSave(int slot) {
|
||||
final id = _channelIdForSlot(slot);
|
||||
if (id == null) return;
|
||||
_coalescedWrites.schedule('channel:$id', () {
|
||||
final currentSlot = _slotForChannelId(id);
|
||||
final messages = currentSlot == null
|
||||
? null
|
||||
: _channelMessages[_canonicalSlot(currentSlot)];
|
||||
if (messages == null) return Future<void>.value();
|
||||
return _channelMessageStore.saveChannelMessages(id, messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// Queues a save of the conversation with [contactKeyHex]; skipped when the
|
||||
/// conversation is gone by the time it runs.
|
||||
void _scheduleConversationSave(String contactKeyHex) {
|
||||
_coalescedWrites.schedule('conversation:$contactKeyHex', () {
|
||||
final messages = _conversations[contactKeyHex];
|
||||
if (messages == null) return Future<void>.value();
|
||||
return _messageStore.saveMessages(contactKeyHex, messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// Runs queued persistence now. Called before the app is backgrounded and
|
||||
/// whenever the stores are about to switch to another radio.
|
||||
Future<void> flushPendingWrites() => _coalescedWrites.flush();
|
||||
|
||||
int getUnreadCountForContact(Contact contact) {
|
||||
if (contact.type == advTypeRepeater) return 0;
|
||||
return getUnreadCountForContactKey(contact.publicKeyHex);
|
||||
@@ -1244,6 +1279,17 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Queues a save of the channel list, which carries the unread counts that
|
||||
/// change on every received channel message.
|
||||
void _scheduleChannelListSave() {
|
||||
_coalescedWrites.schedule(
|
||||
'channels',
|
||||
() => _channelStore.saveChannels(
|
||||
_channels.isNotEmpty ? _channels : _cachedChannels,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void setChannelUnreadCount(int channelIndex, int count) {
|
||||
final channel = _findChannelByIndex(channelIndex);
|
||||
if (channel != null) {
|
||||
@@ -1251,11 +1297,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
channel.unreadCount = count;
|
||||
final total = _cachedChannelsUnreadTotal + (count - previousCount);
|
||||
_cachedChannelsUnreadTotal = total < 0 ? 0 : total;
|
||||
unawaited(
|
||||
_channelStore.saveChannels(
|
||||
_channels.isNotEmpty ? _channels : _cachedChannels,
|
||||
),
|
||||
);
|
||||
_scheduleChannelListSave();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -1271,11 +1313,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
'Channel ${channel.name.isNotEmpty ? channel.name : channelIndex} marked as read (was $previousCount unread)',
|
||||
tag: 'Unread',
|
||||
);
|
||||
unawaited(
|
||||
_channelStore.saveChannels(
|
||||
_channels.isNotEmpty ? _channels : _cachedChannels,
|
||||
),
|
||||
);
|
||||
_scheduleChannelListSave();
|
||||
_notificationService.clearChannelNotification(
|
||||
channelIndex,
|
||||
getTotalUnreadCount(),
|
||||
@@ -1535,6 +1573,9 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
|
||||
/// Load persisted channel messages for a specific channel
|
||||
Future<void> _loadChannelMessages(int channelIndex) async {
|
||||
// The load replaces the in-memory history, so queued writes of it must
|
||||
// reach storage (whose cache updates synchronously) first.
|
||||
unawaited(_coalescedWrites.flush());
|
||||
final channelId = _slotChannelIds[channelIndex];
|
||||
final allMessages = channelId == null
|
||||
? <ChannelMessage>[]
|
||||
@@ -5137,7 +5178,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
),
|
||||
);
|
||||
}
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -5146,7 +5187,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_discoveredContacts.removeWhere(
|
||||
(c) => c.publicKeyHex == contact.publicKeyHex,
|
||||
);
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -5272,7 +5313,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
),
|
||||
);
|
||||
notifyListeners();
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5717,6 +5758,9 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
'$unconfirmed',
|
||||
);
|
||||
}
|
||||
// Queued channel writes find their channel by slot; land them before
|
||||
// the map moves, or a channel whose slot changed would never be saved.
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_slotChannelIds
|
||||
..clear()
|
||||
..addAll(next);
|
||||
@@ -5842,6 +5886,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
);
|
||||
await _loadChannelOrder();
|
||||
final saved = await _channelStore.loadSlotChannelIds();
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_slotChannelIds
|
||||
..clear()
|
||||
..addAll(saved);
|
||||
@@ -5914,6 +5959,9 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_findChannelByIndex(index)?.storageId ?? _slotChannelIds[index];
|
||||
// Delete by setting empty name and zero PSK
|
||||
await sendFrame(buildSetChannelFrame(index, '', Uint8List(16)));
|
||||
// A twin slot with the same PSK keeps this history, so queued writes of
|
||||
// it land before the list is dropped; a full delete clears them below.
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_channelMessages.remove(index);
|
||||
_processedChannelReactions.remove(index);
|
||||
try {
|
||||
@@ -5936,6 +5984,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
/// Removes everything persisted for [channelId]: messages, settings,
|
||||
/// region, images and its place in the channel order.
|
||||
Future<void> _clearChannelStorage(String channelId) async {
|
||||
_coalescedWrites.cancel('channel:$channelId');
|
||||
await _channelMessageStore.clearChannelMessages(channelId);
|
||||
await _channelSettingsStore.clearChannel(channelId);
|
||||
await _channelRegionStore.clearRegion(channelId);
|
||||
@@ -6327,6 +6376,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
unawaited(_coalescedWrites.flush());
|
||||
//set all the stores' public key so they can load the correct data
|
||||
_channelMessageStore.setPublicKeyHex = selfPublicKeyHex;
|
||||
_messageStore.setPublicKeyHex = selfPublicKeyHex;
|
||||
@@ -6953,14 +7003,27 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues a save of the contact list; adverts and contact replies arrive
|
||||
/// many times a second on a busy mesh.
|
||||
Future<void> _persistContacts() async {
|
||||
await _contactStore.saveContacts(_contacts);
|
||||
_coalescedWrites.schedule(
|
||||
'contacts',
|
||||
() => _contactStore.saveContacts(_contacts),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _persistDiscoveredContacts() async {
|
||||
_coalescedWrites.cancel('discovered');
|
||||
await _discoveryContactStore.saveContacts(_discoveredContacts);
|
||||
}
|
||||
|
||||
void _scheduleDiscoveredContactsPersist() {
|
||||
_coalescedWrites.schedule(
|
||||
'discovered',
|
||||
() => _discoveryContactStore.saveContacts(_discoveredContacts),
|
||||
);
|
||||
}
|
||||
|
||||
int _latestContactLastmod() {
|
||||
if (_contacts.isEmpty) return 0;
|
||||
var latest = 0;
|
||||
@@ -8033,11 +8096,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
'Channel ${channel.name.isNotEmpty ? channel.name : channelIndex} unread count incremented to ${channel.unreadCount}',
|
||||
tag: 'Unread',
|
||||
);
|
||||
unawaited(
|
||||
_channelStore.saveChannels(
|
||||
_channels.isNotEmpty ? _channels : _cachedChannels,
|
||||
),
|
||||
);
|
||||
_scheduleChannelListSave();
|
||||
} else {
|
||||
_appDebugLogService?.info(
|
||||
'Channel $channelIndex not found in _channels (${_channels.length}) or _cachedChannels (${_cachedChannels.length})',
|
||||
@@ -8138,7 +8197,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_messageStore.archive(pubKeyHex, messages.sublist(0, trimCount));
|
||||
messages.removeRange(0, trimCount);
|
||||
}
|
||||
_messageStore.saveMessages(pubKeyHex, messages);
|
||||
_scheduleConversationSave(pubKeyHex);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -8409,7 +8468,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
if (reactionApplied) {
|
||||
appLogger.info('Adding channel reaction, id: $reactionIdentifier');
|
||||
// Save updated messages
|
||||
_saveChannelMessages(channelIndex, messages);
|
||||
_scheduleChannelSave(channelIndex);
|
||||
|
||||
// Mark as processed
|
||||
_processedChannelReactions[channelIndex]!.add(reactionIdentifier);
|
||||
@@ -8514,10 +8573,14 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
} else {
|
||||
messages.add(processedMessage);
|
||||
// Storage keeps only the newest maxStoredMessages per channel, so older
|
||||
// entries are gone after the next save anyway. Without this cap a long
|
||||
// session on a busy channel grows the list, and every save, unbounded.
|
||||
final overflow = messages.length - ChannelMessageStore.maxStoredMessages;
|
||||
if (overflow > 0) messages.removeRange(0, overflow);
|
||||
}
|
||||
|
||||
// Save to persistent storage
|
||||
_saveChannelMessages(channelIndex, messages);
|
||||
_scheduleChannelSave(channelIndex);
|
||||
return isNew;
|
||||
}
|
||||
|
||||
@@ -8684,6 +8747,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void _handleDisconnection() {
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_resetLinkSessionState();
|
||||
|
||||
_notifySubscription?.cancel();
|
||||
@@ -8755,6 +8819,9 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
/// Drops the conversation and channel history held in memory, so a
|
||||
/// different radio never shows or persists another radio's messages.
|
||||
void _clearSessionMessageState() {
|
||||
// Writes start synchronously, so they still read this radio's history and
|
||||
// store keys before both change below.
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_conversations.clear();
|
||||
_loadedConversationKeys.clear();
|
||||
_channelMessages.clear();
|
||||
@@ -8897,6 +8964,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_notificationReplySubscription?.cancel();
|
||||
_notificationService.releaseReplyPort();
|
||||
_notifyListenersTimer?.cancel();
|
||||
unawaited(_coalescedWrites.flush());
|
||||
_reconnectTimer?.cancel();
|
||||
_batteryPollTimer?.cancel();
|
||||
_gpsLocationPollTimer?.cancel();
|
||||
@@ -9378,7 +9446,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
isActive: addActive,
|
||||
);
|
||||
notifyListeners();
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -9405,7 +9473,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
_discoveredContacts.add(disContact);
|
||||
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
|
||||
// Show notification for new contact (advertisement)
|
||||
if (_appSettingsService != null && !noNotify) {
|
||||
@@ -9435,7 +9503,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
|
||||
void removeAllDiscoveredContacts() {
|
||||
_discoveredContacts.clear();
|
||||
unawaited(_persistDiscoveredContacts());
|
||||
_scheduleDiscoveredContactsPersist();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
@@ -497,8 +497,16 @@ String ackHashToHex(int ackHash) {
|
||||
}
|
||||
|
||||
// Helper to convert public key to hex string
|
||||
const String _hexDigits = '0123456789abcdef';
|
||||
|
||||
String pubKeyToHex(Uint8List pubKey) {
|
||||
return pubKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
||||
final codes = Uint8List(pubKey.length * 2);
|
||||
for (var i = 0; i < pubKey.length; i++) {
|
||||
final b = pubKey[i];
|
||||
codes[i * 2] = _hexDigits.codeUnitAt(b >> 4);
|
||||
codes[i * 2 + 1] = _hexDigits.codeUnitAt(b & 0x0F);
|
||||
}
|
||||
return String.fromCharCodes(codes);
|
||||
}
|
||||
|
||||
// Helper to convert hex string to public key
|
||||
|
||||
@@ -423,6 +423,15 @@ class _MeshCoreAppState extends State<MeshCoreApp> with WidgetsBindingObserver {
|
||||
state == AppLifecycleState.hidden) {
|
||||
unawaited(widget.imageCodecService.handleMemoryPressure());
|
||||
}
|
||||
// Batched message/contact writes must land before the OS can suspend or
|
||||
// kill a backgrounded app.
|
||||
if (state == AppLifecycleState.inactive ||
|
||||
state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.hidden ||
|
||||
state == AppLifecycleState.detached) {
|
||||
unawaited(widget.connector.flushPendingWrites());
|
||||
unawaited(widget.pathHistoryService.flushPendingWrites());
|
||||
}
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
widget.connector.syncAppBadge();
|
||||
}
|
||||
|
||||
@@ -41,7 +41,9 @@ class Contact {
|
||||
this.rawPacket,
|
||||
}) : lastMessageAt = lastMessageAt ?? lastSeen;
|
||||
|
||||
String get publicKeyHex => pubKeyToHex(publicKey);
|
||||
/// Cached: it backs ==, hashCode and the per-frame contact lookups, and
|
||||
/// rebuilding it allocated a 64-char string on every access.
|
||||
late final String publicKeyHex = pubKeyToHex(publicKey);
|
||||
|
||||
/// Uppercase hex of the first [byteWidth] public-key bytes: the hash this
|
||||
/// node appears as in routing paths.
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../models/contact.dart';
|
||||
import '../models/path_history.dart';
|
||||
import '../models/path_selection.dart';
|
||||
import '../utils/coalesced_writes.dart';
|
||||
import 'storage_service.dart';
|
||||
|
||||
class PathHistoryService extends ChangeNotifier {
|
||||
@@ -10,6 +13,10 @@ class PathHistoryService extends ChangeNotifier {
|
||||
final Map<String, int> _autoRotationIndex = {};
|
||||
final Map<String, _FloodStats> _floodStats = {};
|
||||
final Map<String, Future<void>> _pendingLoads = {};
|
||||
// A path record lands for most received messages, so saves are batched.
|
||||
final CoalescedWrites _writes = CoalescedWrites(
|
||||
delay: const Duration(seconds: 5),
|
||||
);
|
||||
|
||||
// LRU cache eviction tracking
|
||||
static const int _maxCachedContacts = 50;
|
||||
@@ -23,6 +30,9 @@ class PathHistoryService extends ChangeNotifier {
|
||||
PathHistoryService(this._storage);
|
||||
|
||||
void setDevicePublicKey(String publicKeyHex) {
|
||||
// Queued saves use the storage key on entry, so they still land under
|
||||
// the previous radio's key.
|
||||
unawaited(_writes.flush());
|
||||
_storage.setPublicKeyHex = publicKeyHex;
|
||||
_cache.clear();
|
||||
_cacheAccessOrder.clear();
|
||||
@@ -361,7 +371,12 @@ class PathHistoryService extends ChangeNotifier {
|
||||
_cache[contactPubKeyHex] = updatedHistory;
|
||||
_trackAccess(contactPubKeyHex);
|
||||
_evictIfNeeded();
|
||||
_storage.savePathHistory(contactPubKeyHex, updatedHistory);
|
||||
// The snapshot is written, not the cache entry: the LRU may evict it
|
||||
// before the batch runs.
|
||||
_writes.schedule(
|
||||
contactPubKeyHex,
|
||||
() => _storage.savePathHistory(contactPubKeyHex, updatedHistory),
|
||||
);
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -404,6 +419,9 @@ class PathHistoryService extends ChangeNotifier {
|
||||
Future<ContactPathHistory?> _loadHistoryFromStorage(
|
||||
String contactPubKeyHex,
|
||||
) async {
|
||||
// An evicted contact may still have a queued save; it reaches the prefs
|
||||
// cache synchronously, so the load below reads it.
|
||||
unawaited(_writes.flush());
|
||||
return await _storage.loadPathHistory(contactPubKeyHex);
|
||||
}
|
||||
|
||||
@@ -445,6 +463,7 @@ class PathHistoryService extends ChangeNotifier {
|
||||
_cacheAccessOrder.remove(contactPubKeyHex);
|
||||
_autoRotationIndex.remove(contactPubKeyHex);
|
||||
_floodStats.remove(contactPubKeyHex);
|
||||
_writes.cancel(contactPubKeyHex);
|
||||
await _storage.clearPathHistory(contactPubKeyHex);
|
||||
_version++;
|
||||
notifyListeners();
|
||||
@@ -465,6 +484,7 @@ class PathHistoryService extends ChangeNotifier {
|
||||
recentPaths: updatedPaths,
|
||||
);
|
||||
|
||||
_writes.cancel(contactPubKeyHex);
|
||||
await _storage.savePathHistory(contactPubKeyHex, _cache[contactPubKeyHex]!);
|
||||
_version++;
|
||||
notifyListeners();
|
||||
@@ -588,11 +608,21 @@ class PathHistoryService extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs queued saves now, e.g. before the app is backgrounded.
|
||||
Future<void> flushPendingWrites() => _writes.flush();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
unawaited(_writes.flush());
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void clearAllHistories() {
|
||||
_cache.clear();
|
||||
_cacheAccessOrder.clear();
|
||||
_autoRotationIndex.clear();
|
||||
_floodStats.clear();
|
||||
_writes.dispose();
|
||||
_storage.clearAllPathHistories();
|
||||
_version = 0;
|
||||
notifyListeners();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'app_logger.dart';
|
||||
|
||||
/// Collapses repeated persistence requests for the same key into one write.
|
||||
///
|
||||
/// Busy meshes deliver many frames a second, and each one used to rewrite a
|
||||
/// whole JSON blob (a channel's history, the contact list). Every write is a
|
||||
/// full re-encode plus a SharedPreferences write: on Linux/Windows that
|
||||
/// rewrites the entire prefs file synchronously, on Android/iOS it ships the
|
||||
/// blob across the platform channel. Scheduling instead keeps only the latest
|
||||
/// writer per key and runs them all once [delay] after the first request.
|
||||
class CoalescedWrites {
|
||||
CoalescedWrites({this.delay = const Duration(milliseconds: 750)});
|
||||
|
||||
final Duration delay;
|
||||
final Map<String, Future<void> Function()> _pending = {};
|
||||
Timer? _timer;
|
||||
|
||||
bool get hasPending => _pending.isNotEmpty;
|
||||
|
||||
/// Queues [write] under [key], replacing any write already queued for it.
|
||||
/// [write] should read the current state when it runs, not a snapshot.
|
||||
void schedule(String key, Future<void> Function() write) {
|
||||
_pending[key] = write;
|
||||
_timer ??= Timer(delay, () => unawaited(flush()));
|
||||
}
|
||||
|
||||
/// Drops the queued write for [key], e.g. because an immediate write of
|
||||
/// newer state or a delete just superseded it.
|
||||
void cancel(String key) {
|
||||
_pending.remove(key);
|
||||
}
|
||||
|
||||
/// Runs every queued write now. All writes are started synchronously before
|
||||
/// the first await, so a store that reads its scoped key on entry still sees
|
||||
/// the key that was current when the write was queued.
|
||||
Future<void> flush() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
if (_pending.isEmpty) return Future<void>.value();
|
||||
final writes = _pending.values.toList();
|
||||
_pending.clear();
|
||||
// Callers fire and forget, so a failed write is logged rather than left
|
||||
// as an unhandled async error, and never stops the other writes.
|
||||
final futures = <Future<void>>[];
|
||||
for (final write in writes) {
|
||||
try {
|
||||
futures.add(write().catchError(_logFailure));
|
||||
} catch (error) {
|
||||
_logFailure(error);
|
||||
}
|
||||
}
|
||||
return Future.wait(futures).then((_) {});
|
||||
}
|
||||
|
||||
static void _logFailure(Object error) {
|
||||
appLogger.warn('Coalesced write failed: $error', tag: 'Storage');
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_pending.clear();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user