applyChunk function
A port of wire.ts applyChunk: (1) delete every key in rm and every key in a run, (2) insert each run after its predecessor (null: at the start), (3) move each mv key, (4) apply scroll. A keyframe starts a new picture.
Implementation
ReplayPicture? applyChunk(ReplayPicture? prev, ReplayChunk c) {
final ReplayPicture pic;
final k = c['keyframe'];
if (k is Map) {
pic = ReplayPicture(screen: '${k['screen']}', w: (k['w'] as num).toInt(), h: (k['h'] as num).toInt(), nodes: [for (final n in k['nodes'] as List) (n as List).cast<Object?>()], scroll: {});
} else if (prev != null) {
pic = ReplayPicture(screen: prev.screen, w: prev.w, h: prev.h, nodes: List.of(prev.nodes), scroll: Map.of(prev.scroll));
} else {
return null;
}
final d = c['diff'];
if (d is Map) {
final add = [for (final r in (d['add'] as List? ?? const [])) r as Map];
final gone = <Object?>{
...(d['rm'] as List? ?? const []),
for (final r in add)
for (final n in r['nodes'] as List) (n as List)[0],
};
var nodes = <KeyedNode>[for (final n in pic.nodes) if (!gone.contains(n[0])) n];
for (final r in add) {
final after = r['after'];
final i = after == null ? 0 : nodes.indexWhere((n) => n[0] == after) + 1;
nodes = [...nodes.sublist(0, i), for (final n in r['nodes'] as List) (n as List).cast<Object?>(), ...nodes.sublist(i)];
}
final mv = {for (final m in (d['mv'] as List? ?? const [])) (m as List)[0]: m};
if (mv.isNotEmpty) {
nodes = [for (final n in nodes) if (mv[n[0]] case final List<Object?> m) [n[0], n[1], m[1], m[2], ...n.sublist(4)] else n];
}
pic.nodes = nodes;
}
for (final s in (c['scroll'] as List? ?? const [])) {
final m = s as Map;
pic.scroll['${m['node']}'] = (x: m['x'] as num? ?? 0, y: m['y'] as num? ?? 0);
}
return pic;
}