close method
Implementation
List<Json> close() {
final rest = _buffer.toString();
_buffer.clear();
if (rest.isNotEmpty) _parser._readLine(rest);
_parser.finish();
// A text with no line of a node (a print that says its document on the
// `document {…}` line): the screen is sent now, as its first line would.
if (!_parser._canvas && _parser._widget == null && !_parser._noCanvas) {
_parser._canvas = true;
_parser.ops
..add({'op': 'roots', 'roots': _parser._rootIds})
..add({'op': 'set', 'id': _parser.options.screen, 'node': _parser._canvasNode..remove('slots')});
}
// What the end of the text does (design 22, 4.5): each line's tail merged
// again over what later lines added, the nodes a tail took away, the
// canvas lines' words; sent as patches and removals.
final removed = <String>[];
final patched = <String, Json>{};
_result = _parser._end(changed: (p, id, before) {
final now = p.nodes[id];
if (now == null) {
removed.add(id);
return;
}
final d = diffPatch(before ?? const {}, now);
if (d == null) return;
for (final k in const ['children', 'slots', 'type', 'id']) {
d.remove(k);
}
if (d.isNotEmpty) patched[id] = d;
});
for (final p in [_parser, ..._parser._overlays]) {
final c = p._canvasPatch;
if (c == null) continue;
final d = {...c}..remove('slots')..remove('type');
if (d.isNotEmpty) patched[p.options.screen] = mergePatch(patched[p.options.screen] ?? const {}, d, keepNulls: true);
}
// A `document {…}` line's nodes: patched, taken away, or new (a print
// that says a whole document on that line: its nodes set, then placed
// under their parents as its children and slots say).
final known = {
for (final p in [_parser, ..._parser._overlays]) ...p.nodes.keys,
};
final added = <String, Json>{};
final placed = <Json>[];
for (final e in (readMap(_parser._documentPatch?['nodes']) ?? const {}).entries) {
if (e.value == null) {
removed.add(e.key);
} else if (readMap(e.value) case final Json d) {
final fresh = !known.contains(e.key) && d['type'] is String;
if (fresh) added[e.key] = {...d}..removeWhere((k, _) => k == 'children' || k == 'slots');
if (d['children'] case final List c) {
for (var i = 0; i < c.length; i++) {
if (c[i] is String) placed.add({'op': 'move', 'id': c[i], 'parent': e.key, 'index': i});
}
}
for (final s in (readMap(d['slots']) ?? const {}).entries) {
if (s.value is String) placed.add({'op': 'move', 'id': s.value, 'parent': e.key, 'slot': s.key});
}
if (fresh) continue;
final p = {...d}..removeWhere((k, _) => const {'children', 'slots', 'type', 'id'}.contains(k));
if (p.isNotEmpty) patched[e.key] = mergePatch(patched[e.key] ?? const {}, p, keepNulls: true);
}
}
for (final e in added.entries) {
_parser.ops.add({'op': 'set', 'id': e.key, 'node': e.value});
}
for (final e in patched.entries) {
_parser.ops.add({'op': 'patch', 'id': e.key, 'patch': e.value});
}
_parser.ops.addAll(placed);
if (readList(_parser._documentPatch?['roots']) case final List r when r.every((x) => x is String)) {
_parser.ops.add({'op': 'roots', 'roots': r});
}
// Its variables, declared in their scopes.
for (final e in (readMap(_parser._documentPatch?['variables']) ?? const {}).entries) {
final set = <String, Object?>{
for (final v in readList(e.value) ?? const [])
if (readMap(v) case final Json m when m['name'] is String) m['name']! as String: {...m}..remove('name'),
};
if (set.isNotEmpty) _parser.ops.add({'op': 'vars', 'scope': e.key, 'set': set});
}
if (removed.isNotEmpty) _parser.ops.add({'op': 'remove', 'ids': removed});
return _take();
}