Guide

Effects

An effect is an await in a handler. The handler awaits an imported or module-level async function. The compiler splits the handler at the await. The part before it is one message, and the call runs as a task in the browser. The part after it is a second message, the continuation, which gets the result.

idlip has no Http or Storage commands and no Result type. Call fetch, localStorage or a library from your own async function and await it.

#A request with a loading state

The async function lives in a plain module:

ts
// api.ts
export interface User {
  id: string;
  name: string;
}

export async function loadUser(id: string): Promise<User> {
  const response = await fetch(`/api/users/${id}`);
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }
  return (await response.json()) as User;
}

The component keeps the lifecycle in one union and sets it in the handler:

import type { JSX } from 'idlip/jsx-runtime';
import { loadUser } from './api.ts';
import type { User } from './api.ts';

type UserState =
  | { state: 'idle' }
  | { state: 'loading'; id: string }
  | { state: 'failed'; error: string }
  | { state: 'loaded'; user: User };

export default function Profile(): JSX.Element {
  let user = { state: 'idle' } satisfies UserState as UserState;

  async function open(id: string) {
    user = { state: 'loading', id };
    try {
      user = { state: 'loaded', user: await loadUser(id) };
    } catch (error) {
      user = { state: 'failed', error: String(error) };
    }
  }

  return (
    <main>
      <button on:click={() => open('u1')}>Open</button>
      <p>{user.state}</p>
    </main>
  );
}

A click on Open runs these steps:

  1. The message open with ['u1'] sets user to the loading state. The page patches at once, so the loading state shows.
  2. The loader loads the task=open$1 chunk and calls loadUser('u1') in it. That chunk holds only the import.
  3. When the call settles, the loader queues the continuation open$1 with the outcome: { ok: true, value } or { ok: false, error }.
  4. open$1 runs the rest of the try block, or the catch block on a failure, and the page patches again.

tsc checks the call and its result as ordinary TypeScript. The test packages/idlip/test/client/tasks/tasks.test.ts covers these steps, and packages/idlip/test/fixtures/effects/ has the compiled chunks of a larger example.

#Built-in values

Random and Time from idlip give the values a handler must not compute itself, because a handler is pure. Await them like any task:

import { Random, Time } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

interface Note {
  id: string;
  at: number;
}

export default function Notes(): JSX.Element {
  let notes = [] satisfies Note[] as Note[];

  async function add() {
    const id = await Random.id();
    const at = await Time.now();
    notes = [...notes, { id, at }];
  }

  return (
    <main>
      <button on:click={add}>Add</button>
      <p>{notes.length}</p>
    </main>
  );
}

Call Value
Random.id() crypto.randomUUID()
Random.number() Math.random()
Time.now() Date.now()
Time.sleep(ms) Resolves after ms milliseconds.
Dom.measure(ref) The box of an element, see Refs.

These calls are built in: their code sits in the handler chunk, and no task chunk loads. Their continuation runs in the same batch as the message. A handler that awaits only built-ins patches the page once, at the end. A handler that awaits Time.sleep(500) shows nothing new for 500 ms. To wait in the background, use a subscription instead.

A call is built in only when its root name is imported from idlip.

#State after an await

#Errors

A rejection inside a try runs the catch block. A rejection outside a try is thrown at the start of the continuation. The loader reports it in the console, and the model stays as the part before the await left it.

A task chunk that fails to load is reported the same way, and no continuation runs.

#Server, resume and unmount

#Common errors

Rule id Cause
await-callee The awaited value is not a call to an import or a module-level function, such as await fetch(url).
await-placement An await inside an if or a loop.
await-nested An await inside a nested try, or inside a catch or finally block.
await-multiple Two awaits in one statement. Put each in a statement of its own.
await-finally An await in a try that has a finally block.
await-handler A handler calls another handler that awaits, or a handler that awaits calls itself.

Move the await to the top level of the handler or of a try block. To branch, set a local before the await and test it after.