Skip to main content
On this pageSee the Whole Loop

A Simple Counter Example

See the Whole Loop

Application Definitions

This counter puts the core loop from Architecture into one small application. Its Model holds the count. Its Messages record button clicks. Its update function decides the next count, and its view renders the result.

The example uses two files. src/application.ts defines the root through Block.makeDefinition('Application', ...), creates the named graphless ApplicationComposition, authors init, update, and the primary view through that Composition, then assembles ApplicationBlock. Each make* method returns a function or configured value without calling it. src/entry.ts passes the Block and browser container to Program.make, producing a passive runtime input for Runtime.run. Importing the application pieces in a test does not start the browser application. When a larger root extracts one of these responsibilities, split all of its standard pieces into their named files as described in Project Organization.

Counter Application

Runtime Entry

The entry imports ApplicationBlock and passes it to Program.make with the page container. The returned lowercase program is passive configuration. Runtime.run constructs its Layers and starts the application.

Counter entry.ts
import { Program, Runtime } from 'foldkit'

import { ApplicationBlock } from './counter'

const program = Program.make({
  block: ApplicationBlock,
  container: document.getElementById('root'),
})

Runtime.run(program)

Where to Go Next

Read the example once for its shape. The next four pages examine the Model, Messages, update, and view in order. Later pages extend the same counter with a delayed reset, automatic counting, and saved state to introduce side effects and ongoing work.

Start with the Model, the single data structure that describes the application right now.