Skip to Content
Views

Memory

Run a Views circuit on a MemoryStorage host with ViewBackend.layerMemory, or open a scoped View.live reader.

Use ViewBackend.layerMemory when running a View.circuit over a MemoryStorage host—useful for unit tests, local prototypes, and ephemeral workers. If you want to read derived snapshots in memory without output State streams, use View.live instead.

Use a MemoryStorage owner

Build CheckpointStore.layer, Transaction.layer, and ViewBackend.layerMemory over one MemoryStorage.layer. The circuit's operator state, checkpoints, and outputs share that owner's transaction.

import { Layer } from "effect";
import { MemoryStorage, Streamsy } from "@streamsy/core";
import { CheckpointStore, Transaction } from "@streamsy/projection";
import { ViewBackend } from "@streamsy/views";
import { Orders, ShopViews } from "./streams.ts";

export const host = Layer.mergeAll(
  CheckpointStore.layer,
  Transaction.layer,
  ViewBackend.layerMemory,
).pipe(Layer.provideMerge(MemoryStorage.layer(Streamsy.group(Orders, ShopViews))));

At the start of each outer transaction, MemoryStorage copies the owner's stream state, including stored message bytes, into a transaction draft. A host that owns streams pays that cost even when a unit writes only a small part of its view state. The Views backend records its own changes in an undo journal.

ViewBackend.layerMemory checks that the running fiber holds an active transaction on the same memory owner. If the transaction is missing or belongs to another owner, the unit fails with a non-retryable ViewFault; see ViewBackend.

Compare with View.live

Each View.live reader creates its own private memory host for operator state and checkpoints, requires only the input State services and an Effect scope, and exposes snapshots directly instead of appending to output streams. Open it outside any transaction; the inputs keep their own hosts and lifetimes. See Read live snapshots.

Memory circuits and live readers need no migration. A memory circuit's operator state, checkpoints, and streams live as long as its host. A live reader releases its host when its scope closes and retains its final committed snapshot.

On this page