@@ -11,19 +11,27 @@ import ../core/thin-live.bend as TL
1111import ../web/app.bend as A
1212import ../web/mail-actions.bend as MA
1313import ../mobile/hubs.bend as H
14+ import ./thin-splice.bend as S
15+ import ../core/i18n.bend as I
1416
1517# Private, connection-owned UI state. It never enters a client snapshot or
1618# persisted event. Keep it behind a List in the connection context so the
1719# native ABI does not flatten a second full Ui into every Client argument.
20+ # memo: the last full render's reply and what it was made of (at most
21+ # one; thin-splice.bend writes only the live row into it until anything
22+ # else changes or a second passes: Memo.splice)
23+ type Memo is Data:
24+ Memo{key: String, clock: Nat, partial: Bool, result: J.Json}
25+
1826type Actor is Data:
19- Actor{ui: U.Ui, view: String, target: J.Json, pending: Map<&2, String>, reads: List<&2, J.Json>, dirty: Bool}
27+ Actor{ui: U.Ui, view: String, target: J.Json, pending: Map<&2, String>, reads: List<&2, J.Json>, dirty: Bool, memo: List<&2, Memo> }
2028
2129type Rendered is Data:
2230 Rendered{actor: Actor, result: J.Json}
2331
2432def Actor.ui( +a: Actor) -> U.Ui:
2533 match a:
26- case Actor{u, _, _, _, _, _}: u
34+ case Actor{u, _, _, _, _, _, _ }: u
2735
2836def Actor.scope(+target: J.Json) -> String:
2937 J.Json.show(V.Target.json(J.Json.get_str(target, "kind", ""), J.Json.get_str(target, "id", "")))
@@ -114,28 +122,28 @@ def Actor.merge( +fresh: U.Ui, +old: U.Ui, +same: Bool, +thread: String, +presen
114122 M.Time.max(rid, rid2), want, error, note, Actor.prefs(Map.to_list(&2, String, nf2), nf), outbox, stores}
115123
116124def Actor.make(+u: U.Ui, +view: String, +target: J.Json) -> Actor:
117- Actor{u, view, target, Map.new(&2, String), Nil{}, False{}}
125+ Actor{u, view, target, Map.new(&2, String), Nil{}, False{}, Nil{} }
118126
119127def Actor.refresh( +xs: List<&2, Actor>, +web: Bool, +st: M.State, +info: J.Json, +live: J.Json, +now: Nat, +p: J.Json, +view: String) -> Actor:
120128 match xs:
121129 case Nil{}: Actor.make(V.Session.current(web, st, info, live, now, Actor.safe_input(st, p)), view, J.Json.get_obj(p, "target"))
122- case Actor{old, oldView, +target, pending, reads, dirty} <> _:
130+ case Actor{old, oldView, +target, pending, reads, dirty, _ } <> _:
123131 +input = Actor.safe_input(st, p)
124132 +thread = V.Target.thread(st, J.Json.get_obj(p, "target"))
125133 +fresh = V.Session.current(web, st, info, live, now, input)
126134 +same = Bool.and(String.eq(oldView, view), W.Target.same(target, J.Json.get_obj(p, "target")))
127135 Actor{V.Session.renderer(web, U.Ui.info_merge(Actor.merge(fresh, old, same, thread, Maybe.is_some(&2, J.Json, J.Json.get(J.Json.get_obj(J.Json.get_obj(input, "session"), "drafts"), thread))), info), p),
128- view, J.Json.get_obj(p, "target"), Bool.pick(Map<&2, String>, same, pending, Map.new(&2, String)), Bool.pick(List<&2, J.Json>, same, reads, Nil{}), dirty}
136+ view, J.Json.get_obj(p, "target"), Bool.pick(Map<&2, String>, same, pending, Map.new(&2, String)), Bool.pick(List<&2, J.Json>, same, reads, Nil{}), dirty, Nil{} }
129137
130138# The hub's canonical projection is shared; each actor keeps its own pages,
131139# drafts, request slots and exact scope. Compare only the selected body's
132140# newest identity across authoritative states, never its text or full log.
133141def Actor.mail_cache(+a: Actor, previous: List<&2, Actor>, cache: TM.Cache) -> Actor:
134142 match a previous:
135- case Actor{u, view, target, pending, reads, dirty} Nil{}:
136- Actor{TM.Cache.install(cache, u), view, target, pending, reads, dirty}
137- case Actor{u, view, target, pending, reads, dirty} old <> _:
138- Actor{TM.Cache.refresh(cache, u, Actor.ui(old)), view, target, pending, reads, dirty}
143+ case Actor{u, view, target, pending, reads, dirty, memo } Nil{}:
144+ Actor{TM.Cache.install(cache, u), view, target, pending, reads, dirty, memo }
145+ case Actor{u, view, target, pending, reads, dirty, memo } old <> _:
146+ Actor{TM.Cache.refresh(cache, u, Actor.ui(old)), view, target, pending, reads, dirty, memo }
139147
140148def Actor.rpc(+command: J.Json) -> J.Json:
141149 Maybe.default(&2, J.Json, J.Json.parse(J.Json.get_str(command, "text", "")), J.Null{})
@@ -166,16 +174,16 @@ def Actor.reads( +cs: List<&2, J.Json>, +acc: List<&2, J.Json>) -> List<&2, J.Js
166174
167175def Actor.accept( +a: Actor, +r: V.ActResult, +queue: Bool) -> Actor:
168176 match a r:
169- case Actor{_, view, +target, pending, reads, dirty} V.ActResult{u, +cs}:
177+ case Actor{_, view, +target, pending, reads, dirty, memo } V.ActResult{u, +cs}:
170178 Actor{u, view, target, Actor.note(cs, pending, Actor.scope(target)),
171- List.append(&2, J.Json, reads, Bool.pick(List<&2, J.Json>, queue, Actor.reads(cs, Nil{}), Nil{})), dirty}
179+ List.append(&2, J.Json, reads, Bool.pick(List<&2, J.Json>, queue, Actor.reads(cs, Nil{}), Nil{})), dirty, memo }
172180
173181def Actor.mail_ready( +a: Actor) -> Actor:
174182 Actor.accept(a, V.ActResult.web(MA.MA.after(U.Mailv.sync(U.Act{Actor.ui(a), Nil{}}))), True{})
175183
176184def Actor.origin( +a: Actor, +origin: String) -> Actor:
177185 match a:
178- case Actor{u, view, target, pending, reads, dirty}: Actor{U.Ui.info_set(u, "origin", origin), view, target, pending, reads, dirty}
186+ case Actor{u, view, target, pending, reads, dirty, memo }: Actor{U.Ui.info_set(u, "origin", origin), view, target, pending, reads, dirty, memo }
179187
180188def Actor.mail_context_if( +shown: Bool, +u: U.Ui) -> J.Json:
181189 match shown:
@@ -202,7 +210,7 @@ def Actor.receive( +web: Bool, +u: U.Ui, +key: String, +now: Nat, +o: J.Json) ->
202210
203211def Actor.mark_dirty( +a: Actor) -> Actor:
204212 match a:
205- case Actor{u, view, target, pending, reads, _}: Actor{u, view, target, pending, reads, True{}}
213+ case Actor{u, view, target, pending, reads, _, _ }: Actor{u, view, target, pending, reads, True{}, Nil {}}
206214
207215def Actor.now( +u: U.Ui) -> Nat:
208216 match u:
@@ -215,26 +223,26 @@ def Actor.reply_if( +owned: Bool, +a: Actor, +web: Bool, +key: String, +o: J.Jso
215223
216224def Actor.reply(+a: Actor, +web: Bool, +key: String, +o: J.Json) -> Actor:
217225 match a:
218- case Actor{u, view, +target, +pending, reads, dirty}:
226+ case Actor{u, view, +target, +pending, reads, dirty, memo }:
219227 +id = J.Json.as_raw(J.Json.get(o, "id"), "")
220228 Actor.reply_if(Bool.and(String.eq(J.Json.get_str(o, "t", ""), "reply"), String.eq(Actor.pending_scope(pending, id), Actor.scope(target))),
221- Actor{u, view, target, Map.del(&2, String, pending, id), reads, dirty}, web, key, o)
229+ Actor{u, view, target, Map.del(&2, String, pending, id), reads, dirty, memo }, web, key, o)
222230
223231def Actor.clean( +a: Actor) -> Actor:
224232 match a:
225- case Actor{u, view, target, pending, _, _}: Actor{u, view, target, pending, Nil{}, False{}}
233+ case Actor{u, view, target, pending, _, _, memo }: Actor{u, view, target, pending, Nil{}, False{}, memo }
226234
227235def Actor.queued( +a: Actor) -> List<&2, J.Json>:
228236 match a:
229- case Actor{_, _, _, _, reads, _}: reads
237+ case Actor{_, _, _, _, reads, _, _ }: reads
230238
231239def Actor.dirty( +a: Actor) -> Bool:
232240 match a:
233- case Actor{_, _, _, _, _, dirty}: dirty
241+ case Actor{_, _, _, _, _, dirty, _ }: dirty
234242
235243def Actor.capacity( +a: Actor) -> Bool:
236244 match a:
237- case Actor{_, _, _, pending, reads, _}: Bool.and(Nat.is_le(List.length(&2, String, Map.keys(&2, String, pending)), 128n), Nat.is_le(List.length(&2, J.Json, reads), 16n))
245+ case Actor{_, _, _, pending, reads, _, _ }: Bool.and(Nat.is_le(List.length(&2, String, Map.keys(&2, String, pending)), 128n), Nat.is_le(List.length(&2, J.Json, reads), 16n))
238246
239247# A queued read is useful only to the exact surviving connection incarnation
240248# and view that produced it. Equal RPC ids on another socket grant nothing.
@@ -244,3 +252,52 @@ def Job.valid(+actualClient: U32, +jobClient: U32, +actualGen: U32, +jobGen: U32
244252
245253def Control.fresh(+id: Nat, +last: Nat) -> Bool:
246254 Bool.and(Nat.is_gt(id, Nat.mul(65536n, 2147483648n)), Bool.and(Nat.is_le(id, P.Session.nat.max()), Nat.is_gt(id, last)))
255+
256+ # The render memo (thin-splice.bend)
257+ # ----------------------------------
258+
259+ def Actor.memos(xs: List<&2, Actor>) -> List<&2, Memo>:
260+ match xs:
261+ case Actor{_, _, _, _, _, _, memo} <> _: memo
262+ case Nil{}: Nil{}
263+
264+ def Actor.loc(xs: List<&2, Actor>) -> I.Loc:
265+ match xs:
266+ case Actor{u, _, _, _, _, _, _} <> _: U.Lang.loc(u)
267+ case Nil{}: I.En{}
268+
269+ def Actor.remember(+a: Actor, +m: Memo) -> Actor:
270+ match a:
271+ case Actor{u, view, target, pending, reads, dirty, _}: Actor{u, view, target, pending, reads, dirty, [m]}
272+
273+ # what a render is made of but its live row: the read model's sequence, the
274+ # hub's info, the request but its revisions, the page anchor, the view, the
275+ # renderer and the thread (a reply arriving clears the memo: Actor.reply_if)
276+ def Memo.request(+p: J.Json) -> J.Json:
277+ match p:
278+ case J.Obj{+fs}: J.Obj{V.Obj.drop(V.Obj.drop(fs, "requestRevision"), "viewRevision")}
279+ case _: p
280+
281+ def Memo.key(+seq: Nat, +info: String, +p: J.Json, +anchor: Nat, +view: String, +web: Bool, +thread: String) -> String:
282+ J.Json.show(J.Json.obj([J.KV{"seq", J.Json.nat(seq)}, J.KV{"info", J.Str{info}}, J.KV{"p", Memo.request(p)}, J.KV{"anchor", J.Json.nat(anchor)},
283+ J.KV{"view", J.Str{view}}, J.KV{"web", J.Flag{web}}, J.KV{"thread", J.Str{thread}}]))
284+
285+ def Memo.reply.k(+ok: Bool, +res: J.Json, +web: Bool, +stream: TL.Stream, +thread: String, +epoch: String, +target: J.Json, +lc: I.Loc) -> Maybe<&2, J.Json>:
286+ match ok:
287+ case True{}: S.Splice.reply(web, res, stream, thread, epoch, target, lc)
288+ case False{}: None{}
289+
290+ # the memo's reply with only the live row and window new, when nothing else
291+ # can have changed: the same key, at most a second old (clock text), a live
292+ # row now with the same chunk marker as then; None: render in full
293+ def Memo.reply(ms: List<&2, Memo>, +update: Bool, +key: String, +clock: Nat, +web: Bool, +stream: TL.Stream, +thread: String, +epoch: String, +target: J.Json, +lc: I.Loc) -> Maybe<&2, J.Json>:
294+ match ms:
295+ case Nil{}: None{}
296+ case Memo{+k, +at, +partial, +res} <> _:
297+ Memo.reply.k(Bool.and(update, Bool.and(String.eq(k, key), Bool.and(Nat.is_ge(clock, at), Bool.and(Nat.is_lt(Nat.sub(clock, at), 1000n),
298+ Bool.and(Nat.is_gt(TL.Stream.size(stream), 0n), Bool.or(Bool.and(partial, S.Splice.partial(stream)), Bool.and(Bool.not(partial), Bool.not(S.Splice.partial(stream))))))))), res, web, stream, thread, epoch, target, lc)
299+
300+ # a full render remembered for the next ones (clock: ms)
301+ def Rendered.remember(r: Rendered, +key: String, +clock: Nat, +stream: TL.Stream) -> Rendered:
302+ match r:
303+ case Rendered{+a, +res}: Rendered{Actor.remember(a, Memo{key, clock, S.Splice.partial(stream), res}), res}
0 commit comments