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

Making it change: intents and authority

An intent to write, and a live region to show the result.

Goal

Write something — the one way this framework allows it — and have the regions that went stale refresh themselves because the intent declared which ones they were.

A render cannot write. So a mutation is an intent, in its own directory, declaring the tags it may invalidate:

1 · Declare the intent

sketch — not compiled
// app/intents/cart.ts
import { defineIntent } from '@weftjs/core'

export const add = defineIntent<{ sku: string }>({
  name: 'cart.add',
  writes: ['cart'],
  input: (raw) => ({ sku: String((raw as { sku?: unknown }).sku ?? '') }),
  async run(ctx, { sku }) {
    await db.cart.add(ctx.session, sku)
    await ctx.revalidate('cart')
  },
})

2 · Name it from a control

Wire it to a control by naming it, not by writing a handler:

sketch — not compiled
import { add } from '../intents/cart.ts'

<button onClick={add} data-sku={item.sku}>add</button>

With no JavaScript this is a form post, dispatched in phase A, so a real status and a 303 are both available. With the runtime present it goes over the channel, the client stages its guess in an epoch, and the server replaces the guess with the truth in one paint.

3 · Make the region live

sketch — not compiled
body: { fragment: 'cart', live: true, load: loadCart }

live is what lets a region be refreshed over the channel rather than by reloading the page. What arrives is the smallest form the client can prove it can apply — usually a delta, which is the changed values and nothing else.