weft
ir 2.6.0 · warp 1.8.0
Part three · Step 11 of 19

Live regions and the channel

A region refreshed over a channel, and the smallest honest form is a delta computed once for every client on the same base.

Goal

Open a channel, name the region a write invalidates, and watch one delta serve every client that is holding the same base — rather than one render each.

The intent you wrote refreshes the cart because it declared that it does. What arrives is a question of form, and the form is negotiated: full markup for a client that has nothing, a patch for one that has an older version, a delta for one holding the exact base this update was computed against.

1 · Make the region live

app/routes/index.data.ts2 lines changed
  total: { fragment: 'cart/total', needs: ['items'] },+ total: { fragment: 'cart/total', needs: ['items'],+          live: true },

One flag, and the region is addressable after the page has painted. The client subscribes on adoption; nothing else about the fragment changes, because a live region is the same sealed template reached a second way.

2 · Watch the wire

$ npx weft dev --devtools  channel  open · 3 clients on base 1cc44d9  update   cart/total → delta 61 B · computed once, sent 3×  client 4 joined on no base → html 412 B

Three clients, one computation. The delta is a function of two states, so every client holding the same base can be sent the same bytes — which is why the base is named by content address rather than by a client id.

3 · What a delta is not

It is not a DOM diff. There is no second tree to compare against and nothing to reconcile: the template says where every value sits, so a changed value is a write to a known place. That is the whole reason a thousand clients cost one diff — see live regions and the channel.