capture static method
Reads every storage and Backpack value from the running app.
only keeps just the keys matching one of its patterns, and except
drops the keys matching one of its patterns; * matches anything, so
cache_* covers every cache key. Pass backpack false to leave
Backpack out.
Nylo's own seeder record, expired values and Backpack values that can't be written as JSON are left out and listed in skipped.
Implementation
static Future<StorageSnapshot> capture({
List<String> only = const [],
List<String> except = const [],
bool backpack = true,
}) async {
bool wanted(String key) =>
(only.isEmpty || _matchesAny(key, only)) && !_matchesAny(key, except);
final Map<String, Object?> storage = {};
final List<Map<String, String>> skipped = [];
final int now = NyTime.now().millisecondsSinceEpoch;
final Map<String, String> values = await NyStorage.manager().readAll();
final List<String> keys = values.keys.toList()..sort();
for (final String key in keys) {
if (_storageInternal.contains(key) || !wanted(key)) continue;
final String raw = values[key]!;
final _Envelope? envelope = _Envelope.parse(raw);
if (envelope == null) {
storage[key] = {'type': 'raw', 'value': raw};
continue;
}
int? ttl;
final int? expiresAt = envelope.expiresAt;
if (expiresAt != null) {
if (expiresAt <= now) {
skipped.add({'store': 'storage', 'key': key, 'reason': 'expired'});
continue;
}
ttl = ((expiresAt - now) / 1000).ceil();
}
storage[key] = _literal(envelope, raw, ttl);
}
final Map<String, Object?> values2 = {};
if (backpack) {
final Backpack bag = Backpack.instance;
final List<String> bagKeys = bag.keys.toList()..sort();
for (final String key in bagKeys) {
if (_backpackInternal.contains(key) || !wanted(key)) continue;
final dynamic value = bag.read<dynamic>(key);
final Object? written = _backpackLiteral(value);
if (identical(written, _notJson)) {
skipped.add({
'store': 'backpack',
'key': key,
'reason': '${value.runtimeType} can\'t be written as JSON',
});
continue;
}
values2[key] = written;
}
}
return StorageSnapshot(
storage: storage,
backpack: values2,
skipped: skipped,
);
}