FrameLoop

The Frame loop: the one loop per Environment that steps every running animation once a tick, in one pass, so many animations cost one loop and not a fiber each. A binding runs its animations on it with Machine.run, each Frame handed to its sink. CheckFrames turns on a check for development, which fails a run whose step gives a NaN or an Infinity, with NonFiniteFrame, and layerMetrics the loop's metrics, for the devtools or an exporter.

import * as FrameLoop from "@wardic/core/FrameLoop"

// In development, beside the Environment you hand your provider:
const checked = Layer.mergeAll(environment, Layer.succeed(FrameLoop.CheckFrames, true))

Its exports also name types the module doesn't export: its internal types.

Classes

FrameLoop

The Frame loop: run runs a machine on it, as Machine.run does, and runLast one stepped after every other in each tick, for one that reads what the others wrote.

Extends


NonFiniteFrame

A Frame with a NaN or an Infinity in its value: the defect a checked run dies with. key is the key that held it, None when the value is the number itself, and value is what it held.

Extends

  • YieldableError<this> & object & Readonly<{ key: Option<string>; message: string; value: unknown; }>

Constructors

Constructor
new NonFiniteFrame(args): NonFiniteFrame;
Parameters
args
key

Option<string>

message

string

value

unknown

Returns

NonFiniteFrame

Inherited from

Data.TaggedError("NonFiniteFrame")<{ readonly key: Option.Option<string> readonly value: unknown readonly message: string }>.constructor

Properties

key
readonly key: Option<string>;
Inherited from

Data.TaggedError("NonFiniteFrame").key

message
message: string;
Inherited from

Data.TaggedError("NonFiniteFrame").message

value
readonly value: unknown;
Inherited from

Data.TaggedError("NonFiniteFrame").value

Type Aliases

Sink

type Sink<F> = (frame, dt) => undefined;

Where a run's Frames go: a plain call, never an Effect. It returns undefined, so a sink that returns an Effect doesn't compile, where a void one would drop the Effect silently. A Frame handed to a sink is the run's until the next tick, which may rewrite it in place: a sink that keeps a Frame, its value or its velocity, copies it.

It's handed dt too: the time its run was just stepped over, in seconds, the step's own. That's the tick's, for a step a tick or its Late window makes, and 0 for a last-lane run's step again in the window. A sink that works out a speed from its Frames divides by it.

Type Parameters

F

F

Parameters

frame

F

dt

number

Returns

undefined

Variables

CheckFrames

const CheckFrames: Context.Reference<boolean>;

A check for development, off by default: on, every run started where it's on checks each Frame before its sink gets it, and fails with NonFiniteFrame on a NaN or an Infinity. Read once a run, from the caller's fiber, so an app turns it on by merging Layer.succeed(CheckFrames, true) beside its Environment.


layer

const layer: Layer<FrameLoop, never, Clock>;

The Frame loop over the Environment's Clock. Every Environment merges it in.


layerMetrics

const layerMetrics: Layer.Layer<never>;

The Frame loop's metrics, for an app that reads them: wardic_frameloop_animations, the animations running now; wardic_frameloop_settled and wardic_frameloop_failed, counts of the runs that ended; and wardic_frameloop_tick_ms, each tick's work, as a histogram. Merge it beside the Environment, as you give the devtools or an exporter. Without it, the loop records no metric.

Example

const env = Layer.mergeAll(DomLive, devtools, FrameLoop.layerMetrics)