Skip to main content
@craft-ts
LearnLearn with EffectAI agentsGuideReference
@craft-ts/core@craft-ts/component@craft-ts/dev-tools@craft-ts/effect@craft-ts/style@craft-ts/style-testing@craft-ts/i18n@craft-ts/i18n-effect@craft-ts/mcp
ExamplesMigrationEffect compatibility & maturityAdopting CraftTS progressivelyPress kitRoadmapBacklog
Beta
  • Learn
  • Learn with Effect
  • AI agents
  • Guide
  • Reference
  • Packages
  • Resources
  • Guide overview
  • Create a project
i.Core concepts
  • The mental model
  • Which primitive should I use?
  • Anatomy of a primitive
  • Generators and yield*
  • Insertions
  • Typed insertion pipes
  • Exceptions as values
ii.Managing state
  • Local state
  • State machines
  • query
  • Mutations
  • queryParams
  • asyncProcess
  • Selecting
  • Reacting to mutations
  • Collections
  • Collection utilities
  • Persistence
  • GlobalPersisterHandler
  • Pagination placeholders
  • Schema validation
iii.Recommended approaches
  • Inject at the point of use
iv.Structuring the app
  • craftService
  • Service scopes
  • Shaping the public API
  • Abstract services
  • App start
  • Lazy services
  • Server functions
  • craftRegisterFor
  • Target wrapper
v.Routing & type-safe DI
  • Setup
  • CLI automation
  • ESLint rules
  • Route providers
  • Route guards
  • Route exception handling
  • Non-blocking navigation
  • Global error component
  • Route load errors
  • Scaling routes
vi.Components & templates
  • Components
  • Activating the style system
  • Typed styles
  • Defining a design system
  • Tokens and variables
  • Global foundation and fonts
  • Pseudo-elements and animations
  • Axes and the matrix
  • Context obligations
  • Testing visual states
  • Text contrast
  • Visual attestation
  • Template obligations
  • Typed CSS variables
  • Fine-grained reactivity
  • Progressive rendering
  • Directives and .pipe(...)
  • settledValue & pendingNode
  • Accessibility
  • Customization
  • Content projection
  • Styling a component
  • Template migrator
vii.Forms
  • Overview
  • Validators
  • Submitting
  • Nested forms
  • Exception handling
  • Complete examples
viii.Testing
  • Testing services
  • Testing components
  • Type-level tests
  • Browser boundaries
  • Page stability
  • Architecture rules
    • Overview
    • Declarative baseline
    • Unique identities
    • HTTP endpoint ownership
    • Computed purity
    • Dependency cycles
    • Mutation reactions
    • Route DI proofs
    • Route component files
    • Path boundaries
    • Exclusive links
    • Persisted identities
    • insertSelect keys
    • craftEffect and network
    • craftEffect and imperative sync
    • Interactive element names
    • Metric thresholds
    • Server-state loaders
    • Primitive loader requirements
  • Extensible architecture graph
  • Graph insights
  • Craft graph vs Nx
ix.Reactivity utilities
  • craftComputed
  • craftEffect
  • craftMethod
  • source$
  • on$
  • fromEventToSource$
  • sourceFromEvent
  • afterRecomputation
x.Internationalisation
  • Type-safe i18n
  • The catalogue
  • Tokens
  • The runtime
  • With Effect
xi.Deployment (experimental)
  • Overview
  • Manifest reference
  • Diagnostics
  • Providers
  • Alchemy provider
xii.Going further
  • SSR and hydration
  • Effect integration
  • Program operators
  • Pattern matching
  • Observability
  • AI agents
  • Temporal runtime
  1. Reactivity utilities
  2. craftComputed

craftComputed#

A yieldable reactive value that can read Craft dependencies and other reactive Craft values with yield*.

Use it when a derived value reads a Craft reader, a service, or another computed — so those dependencies are recorded on the computed itself. Use craftComputed in application code so every reactive dependency can be traced consistently.

Import#

typescript
import { craftComputed } from '@craft-ts/core';

Overview#

craftComputed exposes a yieldable reader. Application code uses the generator form so every reactive dependency is recorded on that computed:

typescript
const counter = yield* state('counter', 1, ({ state }) => ({
  doubled: craftUse(craftComputed('doubled', function* () {
    return (yield* state()) * 2;
  })),
}));

const doubled = yield* counter.doubled();

Two modes exist:

  • generator factory: craftComputed(name, function* () { ...; return value; }) — the default for Craft values. The generator is replayed on every recomputation.

  • plain computation: craftComputed(name, () => value) — for a computation that only reads values already held by the surrounding scope.

Inside an insertion the name may be omitted: Craft uses the insertion key.

Signatures#

typescript
function craftComputed<Name extends string, T>(
  name: Name,
  computation: () => T,
  options?: CreateComputedOptions<T>,
): YieldableReactiveValue<T, Name>;

function craftComputed<Name extends string, Yielded, T>(
  name: Name,
  factory: () => Generator<Yielded, T, unknown>,
  options?: CreateComputedOptions<T>,
): YieldableReactiveValue<T, Name>;

The first argument is the name outside an insertion and must match the property (or variable) the computed is assigned to. Inside an insertion it may be omitted: Craft uses the insertion key automatically. The name tags the injector context, reactive graph and dev-tools snapshots. The craft-ts/craft-computed-name-match ESLint rule enforces the match and offers a quick fix.

Generator Computation#

Use this form whenever the computed reads a Craft reader, a service, or another computed.

typescript
const counter = yield* state('counter', 1, ({ state }) => ({
  doubled: craftUse(craftComputed('doubled', function* () {
    return (yield* state()) * 2;
  })),
}));

const doubled = yield* counter.doubled();

doubled does not own state(), so it yields it. That is how the computed's own dependency graph records the read.

typescript
import { craftComputed, craftService, craftUse, craftExpose } from '@craft-ts/core';

const { Multiplier } = craftService(
  { name: 'Multiplier', providedIn: 'function' },
  function* () {
    yield* craftExpose('factor', 3);
  },
);

const tripled = craftUse(craftComputed('tripled', function* () {
  const multiplier = yield* Multiplier();
  return (yield* counter()) * multiplier.factor;
}));

Caveats#

  • craftComputed(...) must be created inside an injection context.

  • Unknown yielded values are rejected with a craftComputed-specific error.

  • onAppStart(...) is not supported inside craftComputed(...).

Typing#

Both forms return YieldableReactiveValue<T>. The underlying reactive value stays internal to Craft.

When using a generator, yielded dependencies are tracked and can be extracted with ExtractDeps<...>.

See Also#

  • craftMethod

  • craftEffect

  • craftService

  • onAppStart

  • Architecture rules — assertCraftComputedPure forbids methods and source$ writes inside a computed

←Craft graph vs NxcraftEffect→

On this page

  • Import
  • Overview
  • Signatures
  • Generator Computation
  • Caveats
  • Typing
  • See Also

@craft-ts · MIT licensed

GitHubnpm

Search