Skip to content

Timing ​

From trigger to updated page, what happens before what. Once this is clear, questions like "why did the page flash" or "why was the second call refused" have answers.

The whole picture ​

mermaid
sequenceDiagram
    autonumber
    actor U as User
    participant E as Triggering element
    participant Q as Mount point queue
    participant S as Server (Linker)
    participant ST as Client state
    participant P as Pipeline
    participant RB as Render fragments

    U->>E: click / input
    E->>E: debounce, minimum interval, confirm
    E->>Q: enqueue (in order per mount point)
    Q->>E: busy (disabled)
    Q->>S: POST { component, instance, rev, method, args }
    S->>S: lock instance → checks → call → write back and rev+1 only on success → unlock
    S-->>Q: { ok, data, pipeline, instance, rev }
    alt ok
        Q->>ST: 1. write data into state (bindings redraw synchronously)
        Q->>P: 2. await each then / always step in order
        P->>RB: render.fragment
        RB-->>P: insert → scripts → component binders → bindState → auto
    else failure
        Q->>E: 1. shared error handling (field errors / toast)
        Q->>P: 2. run only catch / always
    end
    Q->>E: restore busy
    Q->>Q: next queued call

1. Before triggering: debounce, interval, confirmation ​

OrderMechanismEffect
①data-linker-debounceWaits for the user to stop
②Minimum interaction interval (800ms)A second click that's too fast: buttons drop it, fields keep the last one and send it later
③data-linker-confirmContinues only after the user confirms
④The form's cancelable submit eventGuards such as uploads can stop it

2. Queueing: one at a time per mount point ​

The browser keeps one Promise queue per mount point. Calls on the same mount point are sent in order and their responses applied in order:

  • A second request can never return first and overwrite the first one's result.
  • In Stateful mode, that's what keeps rev consistent.
  • Different mount points don't wait for each other and can run in parallel.
#orders mount point:  search ──▶ search ──▶ delete       (in order)
#stats  mount point:  load ──▶ load                      (in parallel with the above)

3. Server: locks and rev (Stateful) ​

mermaid
flowchart LR
    A[Call received] --> B{"Acquire instance lock<br/>linker:dispatch:{user}:{instance}<br/>wait up to 3 s"}
    B -->|timeout| X1[service: busy]
    B --> C{rev matches?}
    C -->|no| X2[conflict, with the current rev]
    C --> D[deserialize → booted → call]
    D -->|success| E[serialize back, rev + 1]
    D -->|validation failure| F[renew only]
    D -->|other failure| G[change nothing]
    E --> H[unlock]
    F --> H
    G --> H
  • The lock covers read → call → write on one instance. The lease is 60 seconds, so call must finish within that.
  • A request with an old rev (two tabs sharing the same HTML, say) gets conflict.
  • The struct is written back only on success: if call changes self but ends up returning a failure, the next call sees the state from before.
  • Deserialization failure (struct fields changed after a deploy) → expired.

Stateless has no instance lock, so calls to the same URL can run in parallel; each call is a fresh C::default() plus booted. Correctness of writes comes from DB transactions, unique constraints and idempotency. Success and validation failure return a renewed url, which the browser switches to automatically.

4. Response: state first, then the pipeline ​

On success the order is fixed:

  1. Each key of data is written to client state → the data-state-* subscribed to it redraw synchronously.
  2. The pipeline runs in order.

The other way round, dom.scrollTo would scroll to a page that isn't updated yet.

The namespace is decided when the binding is made, not looked up when the response arrives, because an earlier pipeline's render.fragment may already have replaced the triggering element.

5. Pipeline: in order, awaited, failures jump to catch ​

rust
Ok(LinkerResponse::ok()
    .then(Action::confirm("Also send a notification email?"))               // ① waits for the user's answer
    .then(Action::render_fragment("#list", "orders.list", json!({})))       // ② waits for the fragment to be inserted
    .then(Action::scroll_to("#list"))                                        // ③
    .catch(Action::toast("error", "The page couldn't be updated"))
    .always(Action::focus("#keyword")))
RuleDescription
await in orderEach step (including Promises) finishes before the next runs
Starting stateCall succeeded = not failed; call failed = failed
thenRuns only when not failed
catchRuns only when failed
alwaysAlways runs
A step throws or the action isn't registeredThe state becomes failed and every later then is skipped
Limit20 steps

ui.confirm returns Promise<boolean> and the pipeline waits for it; it only waits for the user's answer and does not mark the state as failed when the user cancels. For "stop if cancelled" flows, put the confirmation on the trigger (data-linker-confirm), or split it into two calls.

When the call fails ​

rust
Ok(LinkerResponse::validation([("body", "A body is required.")])
    .catch(Action::state_set("ticketWizard", "step", 2)))
  1. Shared error handling runs first (field errors are drawn).
  2. data is not written to state.
  3. The pipeline starts as "failed" and runs only catch / always.

6. After a fragment is inserted ​

render.fragment is the most common asynchronous step in a pipeline. It counts as done after:

replace innerHTML
  → run inline <script> in the fragment
  → component binders (bindComponents)
  → bindState()        (data-state-init → the other bindings read state back)
  → renderBladeAuto()  (data-render-auto inside the fragment sends its requests)
  → dispatch cg:rendered  (new data-linker-call and seeds are bound now)

So the pipeline's next step (dom.scrollTo, say) sees new content that's already bound.

When the fragment fingerprint matches (unchanged), the HTML isn't replaced and scripts and bindings don't run again, because inline scripts aren't idempotent; cg:rendered is still dispatched, with detail.unchanged set to true. To force a rerun, pass fresh: true (Action::new("render.fragment", json!({ …, "fresh": true }))).

Render tokens are one-time: fragment requests on the same page are queued in the browser, and each response carries the next token. Sending in parallel would make one of them get 419.

7. Nested calls ​

An action can call another method on the same mount point through context.callLinker(). Since it already holds the queue, it runs directly instead of joining the back of the queue and deadlocking. Nested calls across mount points are refused.

Common timing problems ​

SymptomCauseFix
The second call returns conflictTwo pages share one instance, or the mount point is inside a redrawn fragmentPut the mount point on an outer element
service "操作忙碌中" (busy)The previous call on the same instance took more than 3 secondsMove slow work out of call (schedules, queues)
self was changed but the next call doesn't see itThat call returned a failure, so the state wasn't written backChange state only on success; this is by design
Scrolls to the old positionYour action didn't return a Promise, so the pipeline didn't waitReturn a Promise from the action
A field error flashes and disappearsResponses from two triggers on the same element overwrite each otherAlready handled by "the later one cancels the earlier"; check you aren't sending your own requests
Old values reappear after savingRedrawn with fresh while state still had the old valuesAction::state_clear first, then render.fragment(fresh)
A double click submits twice—The runtime already has a minimum interval and busy state; writes should still be idempotent