AsterDocsv1.2.3

Docs / Guides

Composable UI flows

A confirmation dialog needs to pause the action that opened it. The user’s choice determines whether that action should continue.

Use yield* inside the calling generator. The delegated generator yields its own view while the choice is pending. When it returns a value, execution continues on the next line of the caller.


Example 1: Confirmation Dialog Returning boolean

tsx
function* confirmDelete(title: string) {
  let choice: boolean | null = null;
  while (choice === null) {
    yield (
      <div class="confirm-box" role="alertdialog">
        <span>
          Delete <strong>{title}</strong>?
        </span>
        <button onClick={() => (choice = false)}>Cancel</button>
        <button class="danger" onClick={() => (choice = true)}>
          Delete
        </button>
      </div>
    );
  }
  return choice;
}

export function* TaskRow(
  props: { task: Task; onDelete: (id: string) => void },
  ctx: Ctx<typeof props>,
) {
  let asking = false;

  for (props of ctx) {
    if (asking) {
      // The caller resumes here with the user's choice.
      const confirmed = yield* confirmDelete(props.task.title);
      asking = false;
      if (confirmed) {
        props.onDelete(props.task.id);
        return;
      }
    }

    yield (
      <li>
        <span>{props.task.title}</span>
        <button onClick={() => (asking = true)}>Delete</button>
      </li>
    );
  }
}

A confirmation that returns a decision

Try keeping the preview key, then revoking it. Each choice returns a boolean to the caller.

Example 2: Passing a result to the next checkout step

tsx
export function* CheckoutFlow() {
  const shipping = yield* ShippingStep();
  const payment = yield* PaymentStep(shipping);
  const confirmed = yield* ReviewStep(shipping, payment);

  if (!confirmed) {
    yield <p>Order cancelled.</p>;
    return;
  }

  yield <p>Order placed for {shipping.email}!</p>;
}

Automatic Teardown Propagation

If CheckoutFlow unmounts while the user is halfway through PaymentStep, Aster calls gen.return() on the outer generator. JavaScript automatically propagates that termination into PaymentStep's finally block so timers, listeners, or locks are cleaned up cleanly.

Choosing who advances a workflow

A synchronous component advances once per scheduled update. While yield* is active, the delegated generator receives the value passed to .next(). A prop update can advance the current step. Arguments captured when the step began retain their values. Key the workflow by record ID when switching records should discard an outstanding confirmation.

An async generator advances again as soon as its previous yield resolves. Use it for finite sequences such as loading, awaiting a request, and displaying a result. Keep click-driven loops in synchronous generators. In an async generator a loop that repeatedly yields while waiting for a click can prevent the browser from dispatching that click.

Exceptions thrown inside a delegated step propagate to the caller's try/catch. Unmount calls .return() on the outer generator and runs active nested finally blocks from the inside out. Use ctx.signal for work that supports cancellation and ctx.refresh() to request a render after other asynchronous work finishes.

In the confirmation example, choose each outcome and watch which message the parent renders. Open Code + CSS and follow the boolean from the delegated step back into the parent.

Moving focus after a yield

Pass the component context into a delegated step to open a dialog once its controls and listeners are ready.

tsx
function* confirm(ctx: Ctx) {
  let accepted: boolean | undefined;
  while (accepted === undefined) {
    yield (
      <dialog
        aria-label="Confirm change"
        ref={(dialog) => ctx.afterRender(() => dialog.showModal())}
        on:cancel={(event) => {
          event.preventDefault();
          accepted = false;
        }}
      >
        <button
          onClick={() => {
            accepted = false;
          }}
        >
          Cancel
        </button>
        <button
          onClick={() => {
            accepted = true;
          }}
        >
          Confirm
        </button>
      </dialog>
    );
  }
  return accepted;
}

ctx.afterRender(callback) runs once, in registration order, immediately after this component's next successful DOM patch. This includes mounting, updates, async yields, and claiming server HTML. Refs point to their elements and synchronous children have mounted by then. The container can be detached, and browser paint and pending async children have their own timing.

Registration alone does not schedule a render. A callback registered inside another callback waits for the next patch. Unmount, replacement of the generator execution, or a failed render discards pending work. Server rendering skips these callbacks. A thrown error or a returned promise rejection goes to the component's error boundary while the execution is active.