# @ferret/dataflow: API

The game language: a scene's logic as streams, kinds of entities, motion programs and TypeScript externs; its check, interpreter, scene forms, live change and the game.* commands.

48 exports. Generated from the source (`packages/dataflow/src`), so it matches the code.

## Classes

### FlowError

*class* · `packages/dataflow/src/check.ts:12`

```ts
class FlowError extends Error
```

- `constructor(message: string, readonly pos?: Pos, readonly form?: string)`

### Machine

*class* · `packages/dataflow/src/interp.ts:96`

```ts
class Machine
```

- `time: number`
- `dt: number`
- `stepNo: number`
- `readonly rt: MotionRuntime`
- `readonly mod: Module`
- `readonly state: GraphState`
- `readonly entities: Map<number, Entity>`
- `readonly removed: Set<string>` — the origins of level entities that the game removed while running: a changed file does not bring them back
- `previous: Machine | undefined`
- `readonly stats: Map<string, GraphStats>` — statistics by graph: `game` and `kind:<name>`
- `readonly externStats: Map<string, GraphStats>` — extern calls by name
- `onStep: Array<(m: Machine) => void>` — called after every step (the trace of `game.trace` reads the tokens here)
- `constructor(readonly game: Game, readonly host: FlowHost = {}, opts: MachineOptions = {})`
- `dispose()`
- `byOrigin(origin: string): Entity | undefined` — the live entity a level made under this name
- `carry(old: Entity, declared: Record<string, unknown> = old.declared, origin: string | undefined = old.origin): Entity` — take an entity over from the replaced machine: the same id; a field keeps its running value unless what the file says it starts as has changed
- `commit(): { carried: number; made: number; dropped: number }` — the new file is accepted: the entities made while running come over, the replaced machine's other programs stop and it is done with
- `abort()` — the new file is refused: what this machine started is stopped, the replaced machine goes on untouched
- `despawn(id: number): boolean` — remove an entity: its programs stop; one a level made stays removed when the file changes
- `deliver(event: string, payload: unknown = true)` — a world event reaches the game: the streams `(event "name")` have it at the next step (the last payload of the step if several came)
- `stats_(name: string): GraphStats`
- `spawn(kind: string, fields: Record<string, unknown> = {}, from?: { origin: string }): Entity` — make an entity of a kind with its fields (the defaults of the kind for the rest)
- `alive(kind?: string): Entity[]`
- `step(dt: number): void`
- `kind(name: string): KindDef | undefined`
- `tokens(entity?: number): unknown[]` — the tokens of the last step: `game`, or the entity's graph

## Functions

### bindExterns

*function* · `packages/dataflow/src/externs.ts:29`

```ts
function bindExterns(rt: MotionRuntime, resolve: (name: string) => ExternFn | undefined): void
```

how the words of a runtime find their functions (the game sets it; the lookup is made at each call, so a function may come later)

### compile

*function* · `packages/dataflow/src/check.ts:598`

```ts
function compile(source: string | Form[], opts: CompileOptions = {}): Game
```

Read and check a file of the game language: the text (or the forms of the JSON form) -> the game (graph, kinds, externs, scene forms). Throws with `file:line:col`.

### dataToForm

*function* · `packages/dataflow/src/scene.ts:39`

```ts
function dataToForm(v: unknown): Form
```

### declareExternWord

*function* · `packages/dataflow/src/externs.ts:32`

```ts
function declareExternWord(rt: MotionRuntime, d: Pick<ExternDecl, 'name' | 'kind' | 'from'>): void
```

make the declared function a word of the motion runtime (again, when its kind has changed); does nothing for a name that is already another word

### emptyGraph

*function* · `packages/dataflow/src/graph.ts:66`

```ts
function emptyGraph: () => Graph
```

### externFiles

*function* · `packages/dataflow/src/extern-files.ts:11`

```ts
function externFiles(text: string): string[]
```

the files the externs of a game language text name (`:from`), in order and without repeats; comments and strings are passed over as the reader does

### externModulePath

*function* · `packages/dataflow/src/extern-files.ts:8`

```ts
function externModulePath: (from: string) => string
```

where the module built from the file `from` (a path from the project's root) is found in a built game, a package or a model

### formatForm

*function* · `packages/dataflow/src/scene.ts:212`

```ts
function formatForm(f: Form, indent = 0): string
```

the text of a form: a form that does not fit in 110 columns puts its :key value pairs and its sub-forms on lines of their own

### formToData

*function* · `packages/dataflow/src/scene.ts:50`

```ts
function formToData(f: Form, at?: Pos): unknown
```

### formToGame

*function* · `packages/dataflow/src/scene.ts:136`

```ts
function formToGame(f: Form): GameData
```

`(game "name" :start "level" :levels (list "a" "b") (level ...) ...)`: the levels named in `:levels` first, then the inline ones

### formToLevel

*function* · `packages/dataflow/src/scene.ts:103`

```ts
function formToLevel(f: Form): Record<string, unknown>
```

### formToManifest

*function* · `packages/dataflow/src/scene.ts:126`

```ts
function formToManifest(f: Form): unknown
```

### gameToForm

*function* · `packages/dataflow/src/scene.ts:153`

```ts
function gameToForm(g: GameData): Form
```

### installGame

*function* · `packages/dataflow/src/commands.ts:88`

```ts
function installGame(world: World, options: GameOptions = {}): GameBinding
```

### isLangError

*function* · `packages/dataflow/src/check.ts:18`

```ts
function isLangError: (e: unknown) => e is FlowError | MotionError
```

### isLevelManifest

*function* · `packages/dataflow/src/scene.ts:80`

```ts
function isLevelManifest: (m: unknown) => m is Record<string, unknown>
```

### levelToForm

*function* · `packages/dataflow/src/scene.ts:82`

```ts
function levelToForm(m: Record<string, unknown>): Form
```

### manifestToForm

*function* · `packages/dataflow/src/scene.ts:122`

```ts
function manifestToForm(m: unknown): Form
```

a manifest as forms: a level's by `level`, a package's by `manifest`

### plain

*function* · `packages/dataflow/src/interp.ts:374`

```ts
function plain: (x: unknown) => unknown
```

a token as plain data (absent is null, a symbol its name) for reports

### registeredExternModule

*function* · `packages/dataflow/src/externs.ts:24`

```ts
function registeredExternModule: (from: string) => Record<string, unknown> | undefined
```

the registered module of the file an extern names, if the code holds it

### registerExternModule

*function* · `packages/dataflow/src/externs.ts:22`

```ts
function registerExternModule(path: string, module: Record<string, unknown>): void
```

the module of an extern file that is part of the code itself: what a builder adds to the game

### sceneOf

*function* · `packages/dataflow/src/scene.ts:164`

```ts
function sceneOf(game: Game): { game: GameData | null; levels: Record<string, unknown>[]; entities: Record<string, unknown>[]; links: Record<string, unknown>[] }
```

the scene forms of a compiled file: its game, its levels, the entities and links written outside a level

### spawnLevelEntities

*function* · `packages/dataflow/src/scene.ts:178`

```ts
function spawnLevelEntities(machine: Machine, level: Record<string, unknown>): number[]
```

make the entities of a level that name a kind of the machine's file: `:kind` the kind, `:fields` its fields, `:position` the field `position`. In a machine that
replaces another, an entity of the same level and id comes over with its running state, and one the game removed stays removed

## Interfaces

### Clock

*interface* · `packages/dataflow/src/graph.ts:13`

```ts
interface Clock
```

a clock: the base (every step), or a stream's subset of steps (`when`), or the steps an event came; interned, so equal clocks are the same object

- `id: number`
- `parent: Clock | null`
- `cond: number | null`
- `neg: boolean`
- `name: string`

### CompileOptions

*interface* · `packages/dataflow/src/check.ts:97`

```ts
interface CompileOptions
```

- `file?: string`
- `motion?: MotionRuntime`
- `externs?: ExternDecl[]` — functions the host gives the files without an `extern` of theirs (the functions of the models of a scene)

### Entity

*interface* · `packages/dataflow/src/interp.ts:30`

```ts
interface Entity
```

- `id: number`
- `kind: string`
- `fields: Record<string, unknown>`
- `state: GraphState`
- `alive: boolean`
- `origin?: string` — for an entity a level made: the level's name and the entity's id, by which it is found again when the file changes
- `declared: Record<string, unknown>` — the field values the level gave it (over the kind's initial ones)

### ExternDecl

*interface* · `packages/dataflow/src/graph.ts:44`

```ts
interface ExternDecl
```

- `name: string`
- `from: string`
- `kind: ExternKind | null`
- `args: T[] | null`
- `returns: T | null`
- `pos?: Pos`

### FieldDef

*interface* · `packages/dataflow/src/graph.ts:41`

```ts
interface FieldDef
```

- `name: string`
- `init: unknown`
- `type: T`

### FlowHost

*interface* · `packages/dataflow/src/interp.ts:15`

```ts
interface FlowHost
```

- `sense?(name: string, args: unknown[]): unknown` — a sensor of the world (position, speed, pressed ...); the machine itself answers time, dt and step
- `effect?(name: string, args: unknown[]): void` — an effector of the world, applied at the end of the step (command, animate, play-sound, move, force, motor, effect)
- `emit?(event: string, payload: unknown): void` — `(emit "name" x)`: a world event
- `externs?: Record<string, (...args: any[]) => unknown>` — the TS/JS functions of `extern`s, by name
- `error?(e: { form: string; kind: string | null; entity: number | null; message: string }): void` — a run-time error: the form, the entity (or null), the message
- `motionEvent?(event: string, payload: unknown, time: number): void` — every event the motion programs emit, with the program clock

### Game

*interface* · `packages/dataflow/src/graph.ts:47`

```ts
interface Game
```

a scene form of the file: `game`, `level`, `entity`, `link` as the tree they were written (scene.ts reads them)

- `file?: string`
- `graph: Graph`
- `kinds: Map<string, KindDef>`
- `externs: Map<string, ExternDecl>`
- `motions: Form[]` — `defmotion` and `defn` forms for the motion runtime (the motion subset of the language)
- `scene: Form[]` — the scene forms (game, level, entity, link)
- `clocks: Clock[]`
- `names: Map<string, number>` — the top-level named streams of the game, by name
- `flows: string[]` — the defflow/defn names (they are inlined at every call)

### GameBinding

*interface* · `packages/dataflow/src/commands.ts:40`

```ts
interface GameBinding
```

- `readonly sources: Map<string, string>`
- `machine(): Machine | null`
- `load(files: Record<string, string>): Game`
- `prepare(files: Record<string, string>): Promise<void>` — load the modules that the files' `extern`s name and nobody has given: awaited before `load` (the commands do it by themselves)
- `provide(functions: Record<string, ExternFn>, declarations?: Array<Pick<ExternDecl, 'name' | 'kind'> & Partial<Pick<ExternDecl, 'args' | 'returns'>>>): () => void` — Give functions to the game from outside its files (a model's code); returns what takes them back. A function that is declared (`declarations`) is a word of the files loaded after this, with no `extern` of theirs; without a declaration it only answers an `extern` that is declared in a file.
- `running(): boolean`
- `dispose(): void`

### GameData

*interface* · `packages/dataflow/src/scene.ts:134`

```ts
interface GameData
```

- `name?: string`
- `start?: string`
- `levels: unknown[]`
- `[k: string]: unknown`

### GameOptions

*interface* · `packages/dataflow/src/commands.ts:28`

```ts
interface GameOptions
```

- `externs?: Record<string, (...args: any[]) => unknown>` — the TS/JS functions of the file's `extern`s
- `effects?: Record<string, (...args: unknown[]) => void>` — effectors of the game by name (over the built-in command and emit)
- `sensors?: Record<string, (...args: unknown[]) => unknown>` — sensors of the game by name (over the built-in ones)
- `motion?: MotionRuntime` — a motion runtime with the host's own words (the forms of models, of the physics): the game's motion programs run on it, and the running game ticks it
- `modules?: (from: string) => Promise<Record<string, unknown>>` — how the module of a file an `extern` names is loaded when the code does not hold it itself (the builders put it in and register it). Default: `externModulePath(from)` beside the page

### Graph

*interface* · `packages/dataflow/src/graph.ts:33`

```ts
interface Graph
```

- `nodes: Node[]`
- `sinks: number[]` — the nodes that act on something (emit, effect, set-field, send, spawn, despawn)
- `events: string[]` — the world events the graph reads, by name

### GraphState

*interface* · `packages/dataflow/src/interp.ts:37`

```ts
interface GraphState
```

- `vals: unknown[]`
- `pre: Map<number, unknown>`
- `ticks: Map<number, number>`
- `programs: Map<number, Program>`
- `dead: Set<number>`

### GraphStats

*interface* · `packages/dataflow/src/interp.ts:94`

```ts
interface GraphStats
```

- `calls: number`
- `ms: number`

### KindDef

*interface* · `packages/dataflow/src/graph.ts:42`

```ts
interface KindDef
```

- `name: string`
- `fields: FieldDef[]`
- `graph: Graph`
- `pos?: Pos`

### MachineOptions

*interface* · `packages/dataflow/src/interp.ts:78`

```ts
interface MachineOptions
```

- `motion?: MotionRuntime`
- `previous?: Machine` — the machine this one replaces (a changed file): its time, its mail and its state are carried over where the names match. Nothing of it is touched until `commit()`; `abort()` leaves it as it was

### Node

*interface* · `packages/dataflow/src/graph.ts:15`

```ts
interface Node
```

- `id: number`
- `op: string` — const | sensor | op | pre | when | merge | tick | event | extern | motion | field | self | collect | query | nearest | of | gref | fwd | sinks: emit | effect | set-field | send | spawn | despawn
- `args: number[]`
- `attr?: any` — the name of the operation or sensor, the constant, the field, the event ...
- `pos?: Pos`
- `type: T`
- `clock: Clock | null` — null: a constant, which takes the clock of what it is combined with
- `text: string` — how the node reads in messages: the form it came from
- `key?: string` — for a node that keeps state (pre, tick, motion): its name by where it stands (the def, statement or flow it is in), so that a changed file keeps its state

## Types

### ExternFn

*type* · `packages/dataflow/src/externs.ts:18`

```ts
type ExternFn = (...args: any[]) => unknown
```

### ExternKind

*type* · `packages/dataflow/src/graph.ts:43`

```ts
type ExternKind = 'pure' | 'world' | 'lasting' | 'action'
```

### T

*type* · `packages/dataflow/src/graph.ts:10`

```ts
type T = 'num' | 'bool' | 'vec3' | 'str' | 'entity' | 'list' | 'any'
```

### Token

*type* · `packages/dataflow/src/graph.ts:64`

```ts
type Token = unknown
```

## Constants

### ABSENT

*const* · `packages/dataflow/src/graph.ts:63`

```ts
const ABSENT: typeof ABSENT
```

### MOTION_HEADS

*const* · `packages/dataflow/src/check.ts:25`

```ts
const MOTION_HEADS: Set<string>
```

the forms the motion language runs itself: a program of these in a kind or at the top is one node of the graph

### PURE_OPS

*const* · `packages/dataflow/src/check.ts:80`

```ts
const PURE_OPS: string[]
```

