@ferret/dataflow
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.
Classes
FlowError class
class FlowError extends Errorconstructor(message: string, readonly pos?: Pos, readonly form?: string) |
packages/dataflow/src/check.ts:12
Machine class
class Machinetime: 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 |
packages/dataflow/src/interp.ts:96
Functions
bindExterns function
function bindExterns(rt: MotionRuntime, resolve: (name: string) => ExternFn | undefined): voidhow 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)
packages/dataflow/src/externs.ts:29
compile function
function compile(source: string | Form[], opts: CompileOptions = {}): GameRead 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.
packages/dataflow/src/check.ts:598
dataToForm function
function dataToForm(v: unknown): Formpackages/dataflow/src/scene.ts:39
declareExternWord function
function declareExternWord(rt: MotionRuntime, d: Pick<ExternDecl, 'name' | 'kind' | 'from'>): voidmake 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
packages/dataflow/src/externs.ts:32
emptyGraph function
function emptyGraph: () => Graphpackages/dataflow/src/graph.ts:66
externFiles function
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
packages/dataflow/src/extern-files.ts:11
externModulePath function
function externModulePath: (from: string) => stringwhere 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
packages/dataflow/src/extern-files.ts:8
formatForm function
function formatForm(f: Form, indent = 0): stringthe 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
packages/dataflow/src/scene.ts:212
formToData function
function formToData(f: Form, at?: Pos): unknownpackages/dataflow/src/scene.ts:50
formToGame function
function formToGame(f: Form): GameData(game "name" :start "level" :levels (list "a" "b") (level ...) ...): the levels named in :levels first, then the inline ones
packages/dataflow/src/scene.ts:136
formToLevel function
function formToLevel(f: Form): Record<string, unknown>packages/dataflow/src/scene.ts:103
formToManifest function
function formToManifest(f: Form): unknownpackages/dataflow/src/scene.ts:126
gameToForm function
function gameToForm(g: GameData): Formpackages/dataflow/src/scene.ts:153
installGame function
function installGame(world: World, options: GameOptions = {}): GameBindingpackages/dataflow/src/commands.ts:88
isLangError function
function isLangError: (e: unknown) => e is FlowError | MotionErrorpackages/dataflow/src/check.ts:18
isLevelManifest function
function isLevelManifest: (m: unknown) => m is Record<string, unknown>packages/dataflow/src/scene.ts:80
levelToForm function
function levelToForm(m: Record<string, unknown>): Formpackages/dataflow/src/scene.ts:82
manifestToForm function
function manifestToForm(m: unknown): Forma manifest as forms: a level's by level, a package's by manifest
packages/dataflow/src/scene.ts:122
plain function
function plain: (x: unknown) => unknowna token as plain data (absent is null, a symbol its name) for reports
packages/dataflow/src/interp.ts:374
registeredExternModule function
function registeredExternModule: (from: string) => Record<string, unknown> | undefinedthe registered module of the file an extern names, if the code holds it
packages/dataflow/src/externs.ts:24
registerExternModule function
function registerExternModule(path: string, module: Record<string, unknown>): voidthe module of an extern file that is part of the code itself: what a builder adds to the game
packages/dataflow/src/externs.ts:22
sceneOf function
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
packages/dataflow/src/scene.ts:164
spawnLevelEntities function
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
packages/dataflow/src/scene.ts:178
Interfaces
Clock interface
interface Clocka 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 |
packages/dataflow/src/graph.ts:13
CompileOptions interface
interface CompileOptionsfile?: string | |
motion?: MotionRuntime | |
externs?: ExternDecl[] | functions the host gives the files without an extern of theirs (the functions of the models of a scene) |
packages/dataflow/src/check.ts:97
Entity interface
interface Entityid: 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) |
packages/dataflow/src/interp.ts:30
ExternDecl interface
interface ExternDeclname: string | |
from: string | |
kind: ExternKind | null | |
args: T[] | null | |
returns: T | null | |
pos?: Pos |
packages/dataflow/src/graph.ts:44
FieldDef interface
interface FieldDefname: string | |
init: unknown | |
type: T |
packages/dataflow/src/graph.ts:41
FlowHost interface
interface FlowHostsense?(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 externs, 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 |
packages/dataflow/src/interp.ts:15
Game interface
interface Gamea 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) |
packages/dataflow/src/graph.ts:47
GameBinding interface
interface GameBindingreadonly 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' externs 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 |
packages/dataflow/src/commands.ts:40
GameData interface
interface GameDataname?: string | |
start?: string | |
levels: unknown[] | |
[k: string]: unknown |
packages/dataflow/src/scene.ts:134
GameOptions interface
interface GameOptionsexterns?: Record<string, (...args: any[]) => unknown> | the TS/JS functions of the file's externs |
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 |
packages/dataflow/src/commands.ts:28
Graph interface
interface Graphnodes: 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 |
packages/dataflow/src/graph.ts:33
GraphState interface
interface GraphStatevals: unknown[] | |
pre: Map<number, unknown> | |
ticks: Map<number, number> | |
programs: Map<number, Program> | |
dead: Set<number> |
packages/dataflow/src/interp.ts:37
GraphStats interface
interface GraphStatscalls: number | |
ms: number |
packages/dataflow/src/interp.ts:94
KindDef interface
interface KindDefpackages/dataflow/src/graph.ts:42
MachineOptions interface
interface MachineOptionsmotion?: 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 |
packages/dataflow/src/interp.ts:78
Node interface
interface Nodeid: 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 |
packages/dataflow/src/graph.ts:15
Types
ExternFn type
type ExternFn = (...args: any[]) => unknownpackages/dataflow/src/externs.ts:18
ExternKind type
type ExternKind = 'pure' | 'world' | 'lasting' | 'action'packages/dataflow/src/graph.ts:43
T type
type T = 'num' | 'bool' | 'vec3' | 'str' | 'entity' | 'list' | 'any'packages/dataflow/src/graph.ts:10
Token type
type Token = unknownpackages/dataflow/src/graph.ts:64
Constants
ABSENT const
const ABSENT: typeof ABSENTpackages/dataflow/src/graph.ts:63
MOTION_HEADS const
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
packages/dataflow/src/check.ts:25
PURE_OPS const
const PURE_OPS: string[]packages/dataflow/src/check.ts:80