@@ -27,6 +27,9 @@ inline extern runtime_snapshot_unescape(text : string) : string
2727inline extern runtime_snapshot_unescape_valid (text : string ) : bool
2828 js inline "((s) => { try { decodeURIComponent(s); return true; } catch (_) { return false; } })(#1) "
2929
30+ val runtime_snapshot_marker = "respo/runtime-snapshot "
31+ val runtime_snapshot_version = 1
32+
3033fun encode_snapshot_entry (entry : state_entry ) : string
3134 runtime_snapshot_escape(entry. path) ++ "| " ++
3235 runtime_snapshot_escape(entry. schema) ++ "| " ++
@@ -38,14 +41,17 @@ fun append_snapshot_line(line : string, rest : string) : string
3841
3942// Effect dependency signatures are render metadata. They are intentionally
4043// rebuilt after HMR instead of being persisted with component state.
41- pub fun encode_state_snapshot (tree : list < state_entry > ) : < div > string
44+ fun encode_snapshot_entries (tree : list < state_entry > ) : < div > string
4245 match tree
4346 Nil -> ""
4447 Cons (entry, rest) ->
45- val encoded_rest = encode_state_snapshot (rest)
48+ val encoded_rest = encode_snapshot_entries (rest)
4649 if entry. schema == "respo/effect " then encoded_rest
4750 else append_snapshot_line(encode_snapshot_entry(entry), encoded_rest)
4851
52+ pub fun encode_state_snapshot (tree : list < state_entry > ) : < div > string
53+ append_snapshot_line(runtime_snapshot_marker ++ "| " ++ runtime_snapshot_version. show, encode_snapshot_entries(tree))
54+
4955fun decode_snapshot_entry (line : string ) : maybe < state_entry >
5056 match split(line, "| " )
5157 [encoded_path, encoded_schema, version, encoded_payload] -> {
@@ -68,4 +74,16 @@ fun decode_snapshot_lines(lines : list<string>) : list<state_entry>
6874 Nothing -> decode_snapshot_lines(rest)
6975
7076pub fun decode_state_snapshot (snapshot : string ) : list < state_entry >
71- if snapshot == "" then Nil else decode_snapshot_lines(split(snapshot, "\n " ))
77+ if snapshot == "" then Nil
78+ else match split(snapshot, "\n " )
79+ Nil -> Nil
80+ Cons (header, entries) ->
81+ match split(header, "| " )
82+ Cons (marker, version_parts) ->
83+ if marker != runtime_snapshot_marker then decode_snapshot_lines(Cons (header, entries))
84+ else match version_parts
85+ [version] -> match parse-int(version)
86+ Just (value) -> if value == runtime_snapshot_version then decode_snapshot_lines(entries) else Nil
87+ Nothing -> Nil
88+ _ -> Nil
89+ Nil -> decode_snapshot_lines(Cons (header, entries))
0 commit comments