Pre-beta, under heavy active development: the API changes often and nothing here is stable yet.

Spar

Spar

A front-end framework built on Effect, for agents that write front-end code and the people who read it. Each design decision below is there to make one kind of bug impossible to write, or impossible to miss.

Spar in under two minutes (1:48, with sound).

A component is enhancers, then a view

Enhancers make a component's props; the view comes last and only draws. An enhancer is a plain function from the props so far to an Effect. What the enhancers need and how they can fail are in the component's type, all the way up the tree.

export const Counter = compose(
  withStore(CounterStore, (store) => ({
    count: store.select.count,
    doubled: store.select.doubled,
    onIncrement: store.commands.incremented({ by: 1 }),
    onReset: store.commands.reset(),
  })),
)((props) => (
  <section aria-label="Counter">
    <h2>Counter</h2>
    <p>
      Count <output aria-label="Count">{props.count}</output>, doubled{" "}
      <output aria-label="Doubled">{props.doubled}</output>
    </p>
    <button onClick={props.onIncrement}>Add one</button>
    <button onClick={props.onReset}>Reset</button>
  </section>
))

(apps/demo/src/counter/Counter.tsx)

What it removes. A missing provider and an unhandled failure. App.mount only accepts a component with no errors left and a layer that provides everything the tree needs, so both are compile errors and neither can reach a user.

Server data is declared, not fetched

A component says which data it needs by asking a query. It never fetches. Queries asked for in the same render start together, and one request runs for each input however many components ask.

const ReportPage = compose(
  withProps({ params: Props.Reactive(Report) }),
  withQuery("report", ReportQuery, (props) => Reactive.map(props.params, (now) => now.report)),
  withQuery("author", AuthorQuery, (props) =>
    Reactive.map(props.report.value, (found) => Option.fromNullishOr(found?.authorId)),
  ),
)((props) => (
  <section class="segment" aria-label="Report">
    <h1>
      <output aria-label="Report name">{Reactive.map(props.report.value, labelOf)}</output>
    </h1>
    <p>
      <output aria-label="Report status">{props.report.status}</output>
    </p>
    <p>
      Written by{" "}
      <output aria-label="Author name">{Reactive.map(props.author.value, labelOf)}</output>{" "}
      <output aria-label="Author status">{props.author.status}</output>
    </p>
  </section>
))

(apps/sidebar/src/DeepPage.tsx)

The author query waits for the report only because its input is made from the report's value. That dependency is written down, so the trace viewer can draw it and name what waited for what. In the example below, two requests start together and the third waits for the first.

Live example

  • Profileneeds nothing
  • Ordersneeds nothing
  • Suggestionsneeds the profile

Nothing loaded yet

The profile and the orders start together. The suggestions need the profile, so they start when it lands and not before. Nothing in the component says when to fetch. Turn on the devtools in the top bar and press again to see the same thing as a trace.

What it removes. Accidental request waterfalls, the same request made by every row of a list, and an old answer landing over a newer one: an answer carries the number of the request it answers and a late one is ignored.

A store is an Effect over one state

All app state lives in one place, and changes only through named commands. A store is a schema, pure actions and selectors over its part of that state. What takes time is a plain Effect: the reset below is one fiber, so pressing Reset again while it runs does nothing.

const withReset = Effect.fnUntraced(function* () {
  const store = yield* CounterStore
  const running = yield* FiberHandle.make()
  const reset = Effect.gen(function* () {
    yield* store.commands.resetClicked()
    yield* Effect.sleep("2 seconds")
    yield* store.commands.resetCompleted()
  })

  return { onReset: Effect.asVoid(FiberHandle.run(running, reset, { onlyIfMissing: true })) }
})

(apps/site/src/StoreDemo.ts)

Live example

0counting

Press Reset twice: the second press is ignored, because the reset is already running.

What it removes. State that was changed from somewhere nobody can find, and work that runs twice because two events arrived in an order the author did not think of. Every command is in the log and the trace, with what it carried.

Everything that happens is logged and traced

Every command, every write to UI state, every route change and every request is an entry in one log, in one order, with the trace it belongs to. A press of a button is the root of a trace that runs through the store, the request, the backend and the redraw.

What it removes. Guessing. The devtools show the log, let you step back through it and fork from a moment in the past, and draw each trace as a waterfall across the page and the backend. An agent reads the same data from the command line or over MCP.

Mistakes are reported with a code, a place and a fix

Where the types can refuse a mistake, it does not compile. Where a build rule can, the build stops and names the file and line. What can only be seen while the app runs is reported then, in one format: a code, what happened, the component and file it happened in, and what to do instead.

[SPAR_DUPLICATE_CALL] The same request was made again while an identical one was still on its way.
  in: Dashboard > StockRow
  at: src/StockRow.tsx:12:3
  fix: Load it once with a Query and let each component that needs it ask for it.

What it removes. Errors an agent cannot act on. Every code has an entry in the error reference.

Nothing the user did is lost

What must reach the server goes through an outbox: it is saved on the device first and sent with an idempotency key until the server confirms it. A form's values are UI state, so they are in the log and can be stepped back to like anything else.

What it removes. Work that vanished because a request failed, a tab closed or the network dropped at the wrong moment.

One application, front to back

The page and the backend share one typed contract over Effect RPC, one trace and one way to declare data. The same components render on a server and are picked up in the browser without being drawn again. This site is a Spar app: each page is drawn to HTML with Renderer.html when the site is built, the browser takes it over without drawing it again, and its search box is a query.

Read on in Getting started, or see how Spar sits beside React, Foldkit and Solid 2.0.