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

Spar

State, events that interleave, and server data

Three questions come up in every app with async work: where does a piece of state live, what happens when two things arrive at once, and where does server data go. Spar gives one answer to each. This page says what the answers are and sets them beside Foldkit's and Treflex's.

UI state or store state

Ask who would miss it.

  • UI state is what only the screen would miss: a dropdown is open, which tab is showing, what has been typed and not yet sent, where a divider was dragged to. It is made with UiState.make(name, Schema, initial) in the component's Effect, it belongs to that one component, and it is gone when the component unmounts. It never goes in a store. A store that knows a dropdown is open is a store that has to be told every time a dropdown opens.
  • Store state is what the app would miss: the people, the selection that an export will use, whether a reset is in progress, what the server last said. It lives in a store, it changes only through named commands, and every command is in the log.

A test for the edge cases: if another part of the app, a keyboard shortcut, or an agent would ever need to read it or change it, it is store state. The selection in the people table is store state because "Export CSV" reads it. The text in a search box is UI state until something else needs it.

What takes time is an Effect

A store is a Schema, pure actions and selectors over its part of the one state. It accepts every command at any time; there are no guards, phases or refusals. Work that takes time is a plain Effect on a service, and Effect's own tools decide what happens when events interleave.

A reset that takes two seconds is three steps: start, wait, finish. Written as one Effect and run by a FiberHandle with onlyIfMissing, a second press while it runs does nothing, from a button, a key or an agent alike:

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)

The same tools cover the other cases:

What should happen to a repeatHow it is written
Ignored while the first runsFiberHandle.run(handle, effect, { onlyIfMissing: true })
Replaces the firstFiberHandle.run(handle, effect), which interrupts the earlier run
Waits its turnA Semaphore with one permit around the effect
One per keyA FiberMap keyed by what the work is about
Survives a reload and retriesAn Outbox.channel, which keeps it on the device until it is sent

Every command is in the log and the trace with what it carried, so the order things happened in is never a guess. The counter in the showcase, apps/sidebar/src/ResetCounter.tsx, keeps its buttons enabled and its + key working; its reset is the Effect above.

Server data lives in a store

React Query plus props is two systems: a cache that components subscribe to, and the app's own state, with server data carried between them through props and callbacks. In Spar there is one. A query is a store, and declaring that a component needs one is a line in that component.

The natural way works

A parent reads a value from a store and passes it to a child as a prop. The child declares a query that uses the prop. That is all anybody writes:

export class OrdersQuery extends Query.Tag<OrdersQuery>()("shop/Orders", {
  input: Schema.Int,
  success: Schema.Array(Order),
  failure: ServerUnreachable,
  load: (userId: number) => Effect.flatMap(Effect.service(Server), (found) => found.orders(userId)),
  staleAfter: "30 seconds",
}) {}

(packages/spar/test/serverData.test.tsx)

export const Orders = compose(
  withProps({ userId: Props.Reactive(Schema.Int) }),
  withQuery("orders", OrdersQuery, (props) => props.userId),
)((props) => (
  <p>
    {props.orders.status}: {Reactive.map(props.orders.value, (all) => all?.length ?? 0)} orders
  </p>
))

(packages/spar/test/serverData.test.tsx)

export const Parent = compose(
  withStore(PeopleStore, (people) => ({ selected: people.select.selectedId })),
)((props) => <Orders userId={props.selected} />)

(packages/spar/test/serverData.test.tsx)

That file mounts Parent and checks it shows loaded: 3 orders.

There is no loader in another file, no fragment, no compile step and no list of dependencies. What makes that enough:

  • ask is a declaration. It returns at once with a handle of reactive values (status, value, error, loading) and refetch. It does not wait for data, and no enhancer does, so every query in the tree being mounted is declared in the same pass and they all start together. Nesting components does not make a waterfall.
  • The query runs in its store, not in the component. The store is provided at the root (OrdersQuery.live), so the request and its result outlive the component that asked, and two components that ask for the same input share one request.
  • The input is a value that changes. ask takes a plain value, a reactive value, or a reactive Option. When it changes, the query for the new input starts; going back to an input that is still fresh is a cache hit.
  • A real dependency is an input that is not there yet. Give ask a reactive Option made from another query's value and it is waiting until that value arrives, then starts at once. That is the only waterfall there is, and it is visible as a state.
const askedTogether = Effect.gen(function* () {
  const users = yield* Users.ask()
  const stock = yield* Stock.ask()
  const firstUser = Reactive.map(users.value, (loaded) => Option.fromNullishOr(loaded?.[0]?.id))
  const orders = yield* Orders.ask(firstUser)

  return { users, stock, orders }
})

describe("the natural way: a value from one place feeds a query declared in another", () => {
  it.effect(
    "what waits for nothing starts at once, and a dependent query waits only for its input",
    () =>
      Effect.gen(function* () {
        const { built, noted, whats, answer } = yield* opened
        const declared = yield* Effect.provide(askedTogether, built)

        yield* Effect.yieldNow
        expect(yield* whats).toEqual(["users", "stock"])
        expect(yield* readings(declared.orders)).toEqual(["waiting", null, null, false])

        yield* answer("stock", 40)
        expect(yield* declared.stock.value.get).toBe(40)
        expect(yield* whats).toEqual(["users", "stock"])
        expect(yield* declared.orders.status.get).toBe("waiting")

        yield* answer("users", [ada, grace])
        yield* Effect.yieldNow
        expect(yield* whats).toEqual(["users", "stock", "orders 1"])
        expect(yield* readings(declared.orders)).toEqual(["loading", null, null, true])

        yield* answer("orders 1", ["tea", "ink"])
        expect(yield* readings(declared.orders)).toEqual(["loaded", ["tea", "ink"], null, false])
        expect(
          (yield* Ref.get(noted))
            .filter((seen) => seen.kind === "command")
            .map((seen) => seen.name),
        ).toEqual([
          "test/Users/requested",
          "test/Stock/requested",
          "test/Stock/loaded",
          "test/Users/loaded",
          "test/Orders/requested",
          "test/Orders/loaded",
        ])
      }),
  )

  it.effect("a patch changes what a query holds at once, before the server is asked", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened
      const stock = yield* Effect.provide(Stock.ask(), built)
      const restocked = Effect.provide(
        Stock.patch(undefined, () => 9),
        built,
      )

      yield* Effect.yieldNow
      yield* restocked
      yield* Effect.yieldNow
      expect(yield* readings(stock)).toEqual(["loading", null, null, true])

      yield* answer("stock", 3)
      yield* restocked
      yield* Effect.yieldNow
      expect(yield* readings(stock)).toEqual(["loaded", 9, null, false])
      expect(yield* whats).toEqual(["stock"])
    }),
  )

  it.effect("while another input loads, the last value that was loaded can still be shown", () =>
    Effect.gen(function* () {
      const { built, answer } = yield* opened
      const page = yield* SubscriptionRef.make(1)
      const orders = yield* Effect.provide(Orders.ask(Reactive.fromSubscriptionRef(page)), built)

      yield* Effect.yieldNow
      expect(yield* orders.latest.get).toBeNull()
      yield* answer("orders 1", ["tea"])
      yield* SubscriptionRef.set(page, 2)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect([yield* orders.value.get, yield* orders.latest.get]).toEqual([null, ["tea"]])

      yield* answer("orders 2", ["ink"])
      expect([yield* orders.value.get, yield* orders.latest.get]).toEqual([["ink"], ["ink"]])
    }),
  )

  it.effect("a new input is a new entry in the cache, and going back to one is a cache hit", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened
      const selected = yield* SubscriptionRef.make(1)
      const orders = yield* Effect.provide(
        Orders.ask(Reactive.fromSubscriptionRef(selected)),
        built,
      )

      yield* Effect.yieldNow
      yield* answer("orders 1", ["tea"])
      yield* SubscriptionRef.set(selected, 2)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["orders 1", "orders 2"])
      expect(yield* readings(orders)).toEqual(["loading", null, null, true])

      yield* answer("orders 2", ["ink"])
      expect(yield* orders.value.get).toEqual(["ink"])

      yield* SubscriptionRef.set(selected, 1)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["orders 1", "orders 2"])
      expect(yield* readings(orders)).toEqual(["loaded", ["tea"], null, false])
      expect(yield* Effect.provide(Orders.read(2), built)).toEqual(["ink"])
      expect(yield* Effect.provide(Orders.read(99), built)).toBeNull()
    }),
  )

  it.effect("a plain value is an input too, and two declarations of it share one request", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened
      const first = yield* Effect.provide(Orders.ask(7), built)
      const second = yield* Effect.provide(Orders.ask(7), built)

      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["orders 7"])
      expect(yield* first.status.get).toBe("loading")
      yield* answer("orders 7", ["pen"])
      expect([yield* first.value.get, yield* second.value.get]).toEqual([["pen"], ["pen"]])
    }),
  )

  it.live("a list read a page at a time: the pages so far, whether there is more, and more", () =>
    Effect.gen(function* () {
      const Feed = compose(
        Effect.fnUntraced(function* () {
          const feed = yield* Orders.pages({
            first: 1,
            next: (_page, input) => (input < 3 ? Option.some(input + 1) : Option.none()),
          })
          const state = yield* Reactive.zip(feed.loading, feed.failed)

          return {
            items: Reactive.map(feed.pages, (all) => all.flat().join(",")),
            state: Reactive.map(state, ([loading, failed]) => `${loading} ${failed}`),
            more: Reactive.map(feed.more, String),
            onMore: feed.loadMore,
          }
        }),
      )(FeedView)
      const found = yield* directory
      const answered = (count: number, exit: (waiting: Asked) => Effect.Effect<boolean>) =>
        Effect.flatMap(
          Effect.repeat(Ref.get(found.asked), {
            until: (all) => all.length === count,
            schedule: Schedule.spaced("5 millis"),
          }),
          (all) => Effect.forEach(all.slice(-1), exit),
        )
      yield* Testing.mount(Feed)
      yield* shows("", "true false", "false")
      yield* Testing.click("button", "More")
      yield* answered(1, (waiting) => Deferred.succeed(waiting.answer, ["a", "b"]))
      yield* shows("a,b", "false false", "true")

      yield* Testing.click("button", "More")
      yield* shows("a,b", "true false", "false")
      yield* answered(2, (waiting) => Deferred.succeed(waiting.answer, ["c"]))
      yield* shows("a,b,c", "false false", "true")

      yield* Testing.click("button", "More")
      yield* answered(3, (waiting) => Deferred.fail(waiting.answer, "down"))
      yield* shows("a,b,c", "false true", "false")
      yield* Testing.click("button", "More")
      expect((yield* Ref.get(found.asked)).map((each) => each.what)).toEqual([
        "orders 1",
        "orders 2",
        "orders 3",
      ])
    }).pipe(
      Effect.provide(
        Orders.live.pipe(
          Layer.provideMerge(Directory.layer),
          Layer.provide(SecureRandom.layerWebCrypto),
        ),
      ),
    ),
  )

  it.live("a list whose next page is one it already has says there is no more", () =>
    Effect.gen(function* () {
      const Feed = compose(
        Effect.fnUntraced(function* () {
          const feed = yield* Orders.pages({ first: 1, next: () => Option.some(1) })

          return {
            items: Reactive.map(feed.pages, (all) => all.flat().join(",")),
            state: Reactive.map(feed.loading, String),
            more: Reactive.map(feed.more, String),
            onMore: feed.loadMore,
          }
        }),
      )(FeedView)
      const found = yield* directory

      yield* Testing.mount(Feed)
      yield* shows("", "true", "false")

      const asked = yield* Effect.repeat(Ref.get(found.asked), {
        until: (all) => all.length === 1,
        schedule: Schedule.spaced("5 millis"),
      })

      yield* Effect.forEach(asked, (waiting) => Deferred.succeed(waiting.answer, ["a"]))
      yield* shows("a", "false", "false")
      yield* Testing.click("button", "More")
      yield* Effect.sleep("20 millis")
      yield* shows("a", "false", "false")
      expect((yield* Ref.get(found.asked)).map((each) => each.what)).toEqual(["orders 1"])
    }).pipe(
      Effect.provide(
        Orders.live.pipe(
          Layer.provideMerge(Directory.layer),
          Layer.provide(SecureRandom.layerWebCrypto),
        ),
      ),
    ),
  )

  it.live("in components: the parent passes a prop, the child declares its query with it", () =>
    Effect.gen(function* () {
      const OrdersOf = compose(
        withProps({ userId: Props.Reactive(Schema.Int) }),
        Effect.fnUntraced(function* ({ userId }) {
          const orders = yield* Orders.ask(userId)

          return {
            status: orders.status,
            count: Reactive.map(orders.value, (loaded) => loaded?.length ?? 0),
          }
        }),
      )(OrdersView)
      const selected = yield* SubscriptionRef.make(1)
      const Shelf = compose(
        Effect.fnUntraced(function* () {
          return {
            orders: yield* View.child(OrdersOf, {
              userId: Reactive.fromSubscriptionRef(selected),
            }),
          }
        }),
      )(ShelfView)

      yield* Testing.mount(Shelf)
      yield* Testing.eventually(() =>
        expect(document.querySelector('[aria-label="Orders status"]')?.textContent).toBe("loading"),
      )

      const found = yield* directory
      const asked = yield* Effect.repeat(Ref.get(found.asked), {
        until: (all) => all.length === 1,
        schedule: Schedule.spaced("5 millis"),
      })

      expect(asked.map((each) => each.what)).toEqual(["orders 1"])
      yield* Effect.forEach(asked, (waiting) => Deferred.succeed(waiting.answer, ["tea", "ink"]))
      yield* Testing.eventually(() =>
        expect(document.querySelector('[aria-label="Orders count"]')?.textContent).toBe("2"),
      )
    }).pipe(
      Effect.provide(
        Orders.live.pipe(
          Layer.provideMerge(Directory.layer),
          Layer.provide(SecureRandom.layerWebCrypto),
        ),
      ),
    ),
  )
})

describe("server data in a store", () => {
  it.effect("loads when asked for, once, and is loading, then loaded, as typed states", () =>
    Effect.gen(function* () {
      const { built, noted, users, whats, answer } = yield* opened
      const shown = yield* Effect.provide(Users.ask(), built)

      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      expect(yield* readings(shown)).toEqual(["loading", null, null, true])
      expect(yield* whats).toEqual(["users"])

      yield* TestClock.adjust("5 seconds")
      yield* answer("users", [ada])

      expect(yield* readings(shown)).toEqual(["loaded", [ada], null, false])
      expect(Object.values(yield* users.select.entries.get)).toEqual([
        {
          status: "loaded",
          value: [ada],
          error: null,
          loadedAt: 5000,
          request: 1,
          input: null,
          traceId: "",
          spanId: "",
          unusedSince: null,
        },
      ])
      expect((yield* Ref.get(noted)).map((seen) => [seen.kind, seen.name])).toEqual([
        ["command", "test/Users/requested"],
        ["data", "test/Users/served"],
        ["command", "test/Users/loaded"],
      ])
    }),
  )

  it.effect("asking again while it loads is not sent twice", () =>
    Effect.gen(function* () {
      const { built, noted, whats } = yield* opened
      const shown = yield* Effect.provide(Users.ask(), built)

      yield* shown.refetch
      yield* Effect.yieldNow

      expect(yield* whats).toEqual(["users"])
      expect((yield* Ref.get(noted)).map((seen) => seen.name)).toEqual(["test/Users/requested"])
    }),
  )

  it.effect("stays fresh for its stale time, counted from when it loaded", () =>
    Effect.gen(function* () {
      const { built, users, whats, answer } = yield* opened

      yield* TestClock.adjust("5 seconds")
      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      yield* answer("users", [ada])
      yield* TestClock.adjust("1 minute")
      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users"])

      yield* TestClock.adjust("1 second")

      const later = yield* Effect.provide(Users.ask(), built)

      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users", "users"])
      expect(yield* readings(later)).toEqual(["loading", [ada], null, true])
      expect(Object.values(yield* users.select.entries.get).map((held) => held.loadedAt)).toEqual([
        5000,
      ])
      yield* answer("users", [ada, grace])
      expect(yield* later.value.get).toEqual([ada, grace])
    }),
  )

  it.effect("what was invalidated, or failed after it had loaded, is asked for again", () =>
    Effect.gen(function* () {
      const { built, users, whats, answer, refuse } = yield* opened

      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      yield* answer("users", [ada])
      yield* users.commands.invalidated()
      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users", "users"])

      yield* refuse("users", "gone")
      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users", "users", "users"])
    }),
  )

  it.effect(
    "everything a query does is marked as data in the trace, with where its input came from",
    () =>
      Effect.gen(function* () {
        const { built, answer } = yield* opened
        const spans = yield* Layer.build(Spans.layer())
        const buffer = Context.get(spans, Spans.SpanBuffer)

        yield* Effect.provide(Effect.provide(Users.ask(), built), spans)
        yield* Effect.yieldNow
        yield* answer("users", [ada])

        const recorded = buffer.recent().filter((span) => span.name.startsWith("test/Users/"))

        expect(recorded.map((span) => [span.name, span.attributes["spar.kind"]])).toEqual([
          ["test/Users/requested", "data"],
          ["test/Users/loaded", "data"],
        ])
        expect(recorded[0]?.attributes["spar.query.after"]).toBe("")
      }),
  )

  it.effect(
    "data already there is served with a short span that points at the trace that loaded it",
    () =>
      Effect.gen(function* () {
        const { built, noted, answer } = yield* opened
        const spans = yield* Layer.build(Spans.layer())
        const buffer = Context.get(spans, Spans.SpanBuffer)
        const asking = (during: string) =>
          Effect.provide(Effect.withSpan(Effect.provide(Users.ask(), built), during), spans)

        yield* asking("pointing at the link")
        yield* Effect.yieldNow
        yield* answer("users", [ada])
        yield* asking("the click")

        const named = (name: string) => buffer.recent().filter((span) => span.name === name)
        const [preload] = named("pointing at the link")
        const [click] = named("the click")
        const [served] = named("test/Users/served")

        expect(preload?.traceId).not.toBe(click?.traceId)
        expect(served?.traceId).toBe(click?.traceId)
        expect(served?.attributes).toMatchObject({
          "spar.kind": "data",
          "spar.query.served_from": preload?.traceId,
        })
        expect((yield* Ref.get(noted)).findLast((seen) => seen.kind === "data")?.detail).toEqual({
          key: "null",
          from: preload?.traceId,
        })
      }),
  )

  it.effect(
    "the served span links to the span that loaded the data, and to nothing when no span did",
    () =>
      Effect.gen(function* () {
        const { built, answer } = yield* opened
        const inner = yield* Effect.tracer
        const made = MutableRef.make<ReadonlyArray<Tracer.Span>>([])
        const watching = Tracer.make({
          span: (options) => {
            const span = inner.span(options)

            MutableRef.update(made, (all) => [...all, span])

            return span
          },
        })
        const during = <A, E, R>(name: string, effect: Effect.Effect<A, E, R>) =>
          Effect.withTracer(Effect.withSpan(Effect.provide(effect, built), name), watching)
        const linksOf = (name: string) =>
          MutableRef.get(made)
            .findLast((span) => span.name === name)
            ?.links.map((link) => [link.span.traceId, link.span.spanId, link.span.sampled])

        yield* during("first", Users.ask())
        yield* Effect.yieldNow
        yield* answer("users", [ada])
        yield* during("second", Users.ask())

        const first = MutableRef.get(made).find((span) => span.name === "first")

        expect(linksOf("test/Users/served")).toEqual([[first?.traceId, first?.spanId, true]])

        yield* Effect.provide(Orders.ask(5), built)
        yield* Effect.yieldNow
        yield* answer("orders 5", ["tea"])
        yield* during("third", Orders.ask(5))
        expect(linksOf("test/Orders/served")).toEqual([])
      }),
  )

  it.effect("data nobody is using is released, and dropped after a while", () =>
    Effect.gen(function* () {
      const { built, noted, answer } = yield* opened
      const orders = yield* Effect.provide(Effect.service(Orders), built)
      const keys = Effect.map(orders.state.get, (now) => Object.keys(now.entries))
      const said = Effect.map(Ref.get(noted), (all) =>
        all.filter((seen) => seen.kind !== "data").map((seen) => seen.name.split("/").at(-1)),
      )

      yield* Effect.scoped(
        Effect.gen(function* () {
          yield* Effect.provide(Orders.ask(1), built)
          yield* Effect.provide(Orders.ask(1), built)
          yield* Effect.yieldNow
          yield* answer("orders 1", ["tea"])
        }),
      )
      yield* Effect.yieldNow
      expect(yield* keys).toEqual(["1"])
      expect(yield* said).toEqual(["requested", "loaded", "released"])

      yield* TestClock.adjust("5 minutes")
      expect(yield* keys).toEqual([])
      expect(yield* said).toEqual(["requested", "loaded", "released", "dropped"])
    }),
  )

  it.effect("the wait before a drop does not hold on to the trace it was released in", () =>
    Effect.gen(function* () {
      const { built, answer } = yield* opened
      const orders = yield* Effect.provide(Effect.service(Orders), built)
      const spans = yield* Layer.build(Spans.layer())
      const buffer = Context.get(spans, Spans.SpanBuffer)

      yield* Effect.provide(
        Effect.withSpan(
          Effect.scoped(
            Effect.gen(function* () {
              yield* Effect.provide(Orders.ask(4), built)
              yield* Effect.yieldNow
              yield* answer("orders 4", ["tea"])
            }),
          ),
          "the page that was left",
        ),
        spans,
      )
      yield* Effect.yieldNow
      yield* TestClock.adjust("5 minutes")

      const named = (name: string) => buffer.recent().filter((span) => span.name === name)
      const [page] = named("the page that was left")
      const [released] = named("test/Orders/released")

      expect(released?.traceId).toBe(page?.traceId)
      expect(named("test/Orders/dropped")).toEqual([])
      expect(Object.keys((yield* orders.state.get).entries)).toEqual([])
    }),
  )

  it.effect("data that is asked for again before it is dropped is kept", () =>
    Effect.gen(function* () {
      const { built, noted, answer } = yield* opened
      const orders = yield* Effect.provide(Effect.service(Orders), built)
      const selected = yield* SubscriptionRef.make(1)

      yield* Effect.scoped(Effect.provide(Orders.ask(1), built))
      yield* Effect.yieldNow
      yield* answer("orders 1", ["tea"])
      yield* TestClock.adjust("1 minute")
      yield* Effect.provide(Orders.ask(Reactive.fromSubscriptionRef(selected)), built)
      yield* TestClock.adjust("5 minutes")
      expect(Object.keys((yield* orders.state.get).entries)).toEqual(["1"])
      expect(
        (yield* Ref.get(noted)).filter((seen) => seen.name === "test/Orders/held").length,
      ).toBe(1)

      yield* SubscriptionRef.set(selected, 2)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect(
        Object.values((yield* orders.state.get).entries).map((entry) => entry.unusedSince === null),
      ).toEqual([false, true])
    }),
  )

  it.effect("over its size, a cache drops what has been unused the longest at once", () =>
    Effect.gen(function* () {
      const { built, answer } = yield* opened
      const few = yield* Effect.provide(Effect.service(Few), built)
      const once = (id: number) =>
        Effect.andThen(
          Effect.scoped(
            Effect.andThen(
              Effect.andThen(Effect.provide(Few.ask(id), built), Effect.yieldNow),
              answer(`orders ${id}`, ["x"]),
            ),
          ),
          Effect.andThen(TestClock.adjust("1 second"), Effect.yieldNow),
        )

      yield* once(1)
      yield* once(2)
      expect(Object.keys((yield* few.state.get).entries)).toEqual(["2"])
      yield* once(3)
      expect(Object.keys((yield* few.state.get).entries)).toEqual(["3"])

      const early = yield* Scope.make()

      yield* Effect.provideService(Effect.provide(Few.ask(7), built), Scope.Scope, early)
      yield* Effect.yieldNow
      yield* answer("orders 7", ["x"])
      yield* once(8)
      expect(Object.keys((yield* few.state.get).entries)).toEqual(["7"])
      yield* once(6)
      yield* TestClock.adjust("1 second")
      yield* Scope.close(early, Exit.void)
      yield* Effect.yieldNow
      expect(Object.keys((yield* few.state.get).entries)).toEqual(["7"])

      yield* Effect.provide(Few.ask(9), built)
      yield* Effect.yieldNow
      yield* answer("orders 9", ["x"])
      yield* once(4)
      expect(Object.keys((yield* few.state.get).entries)).toEqual(["9"])
    }),
  )

  it.effect("an input that is set again to what it already was is not looked at again", () =>
    Effect.gen(function* () {
      const { built, noted, answer } = yield* opened
      const selected = yield* SubscriptionRef.make(1)
      const served = Effect.map(
        Ref.get(noted),
        (all) => all.filter((seen) => seen.name === "test/Orders/served").length,
      )

      yield* Effect.provide(Orders.ask(Reactive.fromSubscriptionRef(selected)), built)
      yield* Effect.yieldNow
      yield* answer("orders 1", ["tea"])
      yield* SubscriptionRef.set(selected, 1)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect(yield* served).toBe(0)
    }),
  )

  it.effect("what is asked for again by hand or by invalidation forgets which span loaded it", () =>
    Effect.gen(function* () {
      const { built, users, answer } = yield* opened
      const loadedBy = Effect.map(users.state.get, (now) =>
        Object.values(now.entries).map((entry) => [entry.traceId === "", entry.spanId === ""]),
      )
      const spans = yield* Layer.build(Spans.layer())
      const handle = yield* Effect.provide(
        Effect.withSpan(Effect.provide(Users.ask(), built), "the first ask"),
        spans,
      )

      yield* Effect.yieldNow
      yield* answer("users", [ada])
      expect(yield* loadedBy).toEqual([[false, false]])

      yield* handle.refetch
      yield* Effect.yieldNow
      expect(yield* loadedBy).toEqual([[true, true]])
      yield* answer("users", [ada])

      yield* Effect.provide(
        Effect.withSpan(Effect.provide(Users.ask(), built), "asked again"),
        spans,
      )
      yield* Effect.provide(Users.invalidate, built)
      yield* Effect.yieldNow
      expect(yield* loadedBy).toEqual([[true, true]])
    }),
  )

  it.live(
    "coming back to the tab or back online asks again for what has gone stale, and only that",
    () =>
      Effect.gen(function* () {
        const { built, whats, answer } = yield* opened
        const live = yield* Layer.build(Layer.provide(LiveStock.live, Layer.succeedContext(built)))
        const asked = (count: number) =>
          Effect.repeat(whats, {
            until: (all) => all.length === count,
            schedule: Schedule.spaced("5 millis"),
          })

        yield* Effect.provide(LiveStock.ask(), live)
        yield* Effect.provide(Users.ask(), built)
        yield* asked(2)
        yield* answer("stock", 3)
        yield* answer("users", [ada])
        yield* Effect.sleep("10 millis")

        yield* hidden("hidden")
        yield* Effect.sleep("20 millis")
        expect(yield* whats).toEqual(["stock", "users"])

        yield* hidden("visible")
        expect(yield* asked(3)).toEqual(["stock", "users", "stock"])
        yield* answer("stock", 4)
        yield* Effect.sleep("10 millis")

        yield* connection("offline")
        yield* connection("online")
        expect(yield* asked(4)).toEqual(["stock", "users", "stock", "stock"])
      }),
  )

  it.effect("a query given a schedule tries again by itself before it calls it a failure", () =>
    Effect.gen(function* () {
      const { built, whats, refuse, answer } = yield* opened
      const patient = yield* Layer.build(Layer.provide(Patient.live, Layer.succeedContext(built)))
      const shown = yield* Effect.provide(Patient.ask(), patient)

      yield* Effect.yieldNow
      yield* refuse("stock", "busy")
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["stock", "stock"])
      expect(yield* shown.status.get).toBe("loading")
      yield* answer("stock", 9)
      expect(yield* readings(shown)).toEqual(["loaded", 9, null, false])
    }),
  )

  it.effect("by default an answer stays fresh for 500 ms: asks 100 ms apart share one call", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened

      yield* Effect.provide(Stock.ask(), built)
      yield* Effect.yieldNow
      yield* answer("stock", 3)
      yield* TestClock.adjust("100 millis")
      yield* Effect.provide(Stock.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["stock"])
      yield* TestClock.adjust("500 millis")
      yield* Effect.provide(Stock.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["stock", "stock"])
    }),
  )

  it.effect("with a stale time of 0 it is stale as soon as time has passed", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened
      const live = yield* Layer.build(Layer.provide(LiveStock.live, Layer.succeedContext(built)))

      yield* Effect.provide(LiveStock.ask(), live)
      yield* Effect.yieldNow
      yield* answer("stock", 3)
      yield* Effect.provide(LiveStock.ask(), live)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["stock"])
      yield* TestClock.adjust("1 milli")
      yield* Effect.provide(LiveStock.ask(), live)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["stock", "stock"])
    }),
  )

  it.effect("a failure is a typed state, and the next need tries again", () =>
    Effect.gen(function* () {
      const { built, whats, refuse, answer } = yield* opened
      const shown = yield* Effect.provide(Users.ask(), built)

      yield* Effect.yieldNow
      yield* refuse("users", "The directory is down")
      expect(yield* readings(shown)).toEqual(["failed", null, "The directory is down", false])

      yield* Effect.provide(Users.ask(), built)
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users", "users"])
      expect(yield* readings(shown)).toEqual(["loading", null, "The directory is down", true])
      yield* answer("users", [grace])
      expect(yield* readings(shown)).toEqual(["loaded", [grace], null, false])
    }),
  )

  it.effect("an answer to an earlier request, or to nothing that was asked, is ignored", () =>
    Effect.gen(function* () {
      const { built, users } = yield* opened
      const shown = yield* Effect.provide(Users.ask(), built)
      const [key] = Object.keys((yield* users.state.get).entries)

      yield* users.commands.loaded({ key: key ?? "", request: 0, value: [grace], at: 1 })
      expect(yield* shown.status.get).toBe("loading")
      yield* users.commands.failed({ key: "nothing asked this", request: 1, error: "late" })
      expect(yield* shown.status.get).toBe("loading")
      expect(Object.keys((yield* users.state.get).entries)).toEqual([key])
    }),
  )

  it.effect("invalidating it loads it again at once", () =>
    Effect.gen(function* () {
      const { built, whats, answer } = yield* opened
      const shown = yield* Effect.provide(Users.ask(), built)

      yield* Effect.yieldNow
      yield* answer("users", [ada])
      yield* Effect.provide(Users.invalidate, built)
      yield* Effect.yieldNow
      yield* Effect.yieldNow
      expect(yield* whats).toEqual(["users", "users"])
      expect(yield* readings(shown)).toEqual(["loading", [ada], null, true])

      yield* answer("users", [grace])
      expect(yield* shown.value.get).toEqual([grace])
    }),
  )

  it.live("can sit beside the view that shows it, with no layer of its own at the root", () =>
    Effect.gen(function* () {
      const Shown = Stock.beside(
        compose(
          Effect.fnUntraced(function* () {
            const stock = yield* Stock.ask()

            return {
              status: stock.status,
              count: Reactive.map(stock.value, (loaded) => loaded ?? 0),
            }
          }),
        )(OrdersView),
      )

      yield* Testing.mount(Shown)

      const found = yield* directory
      const asked = yield* Effect.repeat(Ref.get(found.asked), {
        until: (all) => all.length === 1,
        schedule: Schedule.spaced("5 millis"),
      })

      yield* Effect.forEach(asked, (waiting) => Deferred.succeed(waiting.answer, 12))
      yield* Testing.eventually(() =>
        expect(document.querySelector('[aria-label="Orders count"]')?.textContent).toBe("12"),
      )
    }).pipe(Effect.provide(Directory.layer)),
  )

  it("its states are plain data with a Schema", () => {
    expect(
      ["waiting", "loading", "loaded", "failed", "other"].map(Schema.is(Query.Status)),
    ).toEqual([true, true, true, true, false])
  })
})

(packages/spar/test/query.test.ts)

Users and stock start together. Orders starts the moment users has answered, whether or not stock has. The test in that file runs exactly this, controls the order of the answers, and checks it.

The one line of the natural version that matters: the prop has to be passed as the reactive value it is (Props.Reactive), not read out into a plain number first. A plain number works as an input, but it cannot change and it does not know where it came from.

What a query gives

Each of these is an entry in the log and a span in the trace.

React QuerySpar
useQuery in a componentyield* TheQuery.ask(input) in the component's Effect
status, data, errorThe handle's status (waiting, loading, loaded, failed), value, error, loading
Query keysThe input, with a Schema. The cache is keyed by its encoded value
staleTimestaleAfter
refetch()The handle's refetch
Deduping identical requestsOne request for each input at a time. A second while loading is not sent
A late answer overwriting a newer oneIgnored: an answer carries the number of the request it answers
invalidateQueries after a mutationTheQuery.invalidate after the save, in the service method that saves
Keeps old data while loadingFor the same input, yes: value stays while it is refetched. For a new input value is null until that input's answer arrives; show latest to keep the last rows on screen
enabled: !!id for a dependent queryPass a reactive Option: the query is waiting while it is none
Optimistic updateTheQuery.patch(input, change) shows the change at once; an Outbox.channel sends it and ends with TheQuery.invalidate; Outbox.givenUp(channel) runs TheQuery.invalidate if the send fails for good
Refetch on focus and reconnectYes, for what has gone stale, while a component is using the query
Keeping the last page while the next loadsThe handle's latest
Pagination and infinite listsTheQuery.pages({ first, next }): the pages loaded so far, whether there is more, and an action that loads the next
Retriesretry, a schedule; the status stays loading until it gives up
Dropping unused dataAn entry no component is using is released, and dropped after dropAfter (5 minutes unless set). Over keepAtMost entries (100 unless set), the longest unused go at once. Both show in the log

A list read a page at a time

A page is an input, so each page is its own entry in the cache. pages keeps the list of pages a component has opened, as that component's UI state, and asks for each:

const withFeed = Effect.fnUntraced(function* () {
  const feed = yield* PostsQuery.pages({
    first: { after: null },
    next: (page) => Option.map(Option.fromNullishOr(page.nextCursor), (after) => ({ after })),
  })

  return {
    posts: Reactive.map(feed.pages, (all) => all.flatMap((page) => page.posts)),
    loading: feed.loading,
    more: feed.more,
    onMore: feed.loadMore,
  }
})

(packages/spar/test/serverData.test.tsx)

export const Feed = compose(withFeed)((props) => (
  <section aria-label="Posts">
    <ul>
      <For each={props.posts} by={(post) => post.id}>
        {(post) => <li>{Reactive.map(post, (now) => now.title)}</li>}
      </For>
    </ul>
    <button type="button" hidden={Reactive.map(props.more, (more) => !more)} onClick={props.onMore}>
      More
    </button>
  </section>
))

(packages/spar/test/serverData.test.tsx)

The same file mounts Feed, presses More, sees the second page, and checks that More goes away at the end.

next is given a loaded page and its input and says what the following input is, or none at the end. loadMore does nothing while the last page is still loading or when there is no more. failed says a page failed. Going back to the list later finds its pages in the cache, by each page's staleAfter. For numbered pages where only one shows at a time, pass the page number as the input of an ordinary ask and show latest while the next one loads.

Place it like any store: TheQuery.live in the app's layers, or TheQuery.beside(component) to ship it with the view that shows it.

Another store's action reads it directly

Because the data is in a store, nothing has to carry it. In the showcase the people table loads, rows are selected, and "Export CSV" is one Effect with no arguments:

export const exportCsv = Effect.fn("People.exportCsv")(function* () {
  const people = yield* PeopleStore
  const selected = yield* people.select.selected.get

  if (selected.length === 0) return yield* people.commands.nothingToExport()

  const loaded = yield* UsersQuery.read()

  yield* people.commands.exported(csvOf(loaded ?? [], selected))
})

(apps/sidebar/src/People.ts)

It takes the selection from the store and the emails from the users query. No prop carries a user, and the view never sees an email it does not draw. The click and the result are one trace (apps/sidebar/src/People.ts, apps/sidebar/test/people.test.ts).

A reducer stays pure, so reading another store happens in an Effect, which then records the result with a command. That is the general shape for all async work: an Effect does the work, a command records what came of it.

Beside Foldkit

Foldkit's side is read from the installed package (0.170.0): foldkit/asyncData and the experimental foldkit/experimental/query, which its "API Cache Query" example is built on. Its plain "API Cache" example does the same by hand with AsyncData fields in the Model.

FoldkitSpar
Where server data livesIn the one Model, as a Query submodel holding an AsyncData valueIn a query store
Declaring oneQuery.define({ name, data, error, execute }), with args for a keyed oneQuery.Tag<Self>()(key, { input, success, failure, load, staleAfter })
StatesAsyncData: Idle, Loading, Refreshing(data), Failure(error), Stale(error, data), Success(data)status of waiting, loading, loaded, failed, with value and error beside it
Wiring it inThe parent adds the submodel to its Model, lifts the query's update into its own, and folds the completion MessageProvide the layer, or use .beside(component). Nothing is folded by hand
Asking for dataloadIfMissing, revalidate, revalidateOrLoad return the next Model and a Command from updateask(input) in the component that needs it; invalidate after a save
A late answerA generation number on each fetchA request number on each answer, ignored when it is not the current one
Stale timeNot in the query's config as shipped: revalidation is asked forstaleAfter
Keyed by argumentsYes (KeyedQuery)Yes: the input
A dependent queryThe update branch that handles the first answer starts the secondThe second query's input is made from the first one's value; it waits by itself
Reading it elsewhereAny update branch has the whole Model, so it reads the query's data directlyAn Effect calls TheQuery.read(input)

Where Foldkit is ahead: AsyncData tells "refreshing with old data" and "failed but still holding old data" apart as states of their own, where Spar leaves you to read status and value together. Its query API is marked experimental. Where Spar is ahead: nothing is lifted or folded by hand, a stale time is declared, a dependent query needs no code, and every request and answer is in the log.