map<R> method
The same call with every answer passed through convert — from the
stored shape (raw JSON, say) to the app's models. It runs on every
answer, so anything it derives from the clock is fresh each time.
A saved copy that convert rejects (an app update changed the shape)
is skipped, and the server's answer is awaited instead.
Implementation
CachedCall<R> map<R>(R Function(T value) convert) =>
CachedCall((strategy) async* {
var shownLocal = false;
await for (final answer in fetch(strategy)) {
if (answer.isLocal) {
final R converted;
try {
converted = convert(answer.value);
} catch (e) {
debugPrint('Skipping a saved copy that no longer converts: $e');
continue;
}
shownLocal = true;
yield answer._withValue(converted);
} else {
yield answer._withValue(
convert(answer.value),
sameAsLocal: shownLocal && answer.sameAsLocal,
);
}
}
});