Typing
Canopy is built for the Luau analyzer. Give setup its types, and every state, tree and machine built from it is checked:
const Canopy = require("@Canopy")
type Context = { health: number }
type Event = "knockedBack" | "landed"
type Name = "free" | "knockedBack" | "flying" | "groundImpact"
const S = Canopy.setup<<Context, Event, Name, nil, nil>>()| Type | What it is | Where it's checked |
|---|---|---|
Context | The data shared by the whole machine | The first argument of every state function, S.new, machine:getContext() |
Event | The names of the events the machine can be sent | machine:send, the keys of on |
Name | The names of the states | initial, handlers, S.goTo, machine:goTo, machine:isIn, tree methods |
Data | What events and goTos carry, nil when they carry nothing | machine:send, machine:goTo, S.goTo, run.data, handler and memory factory arguments |
Record | What a recorded state adds to the history, nil when nothing is recorded | What record returns, getRecords, endsWith |
Give all five types. A type left out of << >> is not checked.
A state's memory takes its type from its withMemory factory, so run.memory is typed inside that state's own functions.
What the analyzer reports
- A misspelt state name in a handler,
onTimeUp,initial,S.goToormachine:goTo. - An unknown event in
machine:send, and data of the wrong type sent with it. - A wrong field in the context or in a state's memory.
- A
recordthat returns something other thanRecord, and a sequence of the wrong type given toendsWith. - A table that was not built with
S.stateorS.withMemory, used as a state.
A known gap
A misspelt event key inside on = { ... } is not reported by the new type solver, which does not check the keys of a table literal passed to a function. Give setup the list of events to close the gap: the type checker checks the list against Event, and S.state errors on any on key that is not in it, when the module that builds the tree loads.
const S = Canopy.setup<<Context, Event, Name, Data, nil>>({
events = {
"knockedBack",
"landed",
},
})Types
Run Added in 0.1.0
type Run<Name, Data, Memory> = {
name: Name,
data: Data?,
memory: Memory,
startedAt: number,
endsAt: number,
isTimeUp: boolean,
}One stay in a state, as that state's own functions see it, made when the state is entered.
| Field | Type | Description |
|---|---|---|
name | Name | The state's name. |
data | Data? | The data of the goTo or event that entered the state, nil when there was none. |
memory | Memory | The state's own data, from its withMemory factory, or nil. |
startedAt | number | When the state was entered. |
endsAt | number | When onTimeUp runs, or math.huge with no duration. onStart may change it. |
isTimeUp | boolean | Whether the time-up has run in this stay. |
RunInfo Added in 0.1.0
type RunInfo<Name, Data> = {
name: Name,
data: Data?,
startedAt: number,
endsAt: number,
isTimeUp: boolean,
}The same stay as code outside the state sees it, from getRun and the hooks. A state's memory is its own, so it is left out.
GoTo Added in 0.1.0
type GoTo<Name, Data> = {
identifier: GoToIdentifier,
target: Name,
data: Data?,
}Where a state leads, made with S.goTo.
Stay Added in 0.1.0
type Stay = {
identifier: StayIdentifier,
}Takes an event without moving, made with S.stay.
Duration Added in 0.1.0
type Duration = number | { number }Seconds, or { min, max } rolled on each entry.
Handler Added in 0.1.0
type Handler<Context, Name, Data, Memory> = Name | (context: Context, run: Run<Name, Data, Memory>, data: Data?) -> (GoTo<Name, Data> | Stay)?A state's name, or a function that returns a GoTo, a Stay, or nil to let the outer states handle it. See Handlers.
State Added in 0.1.0
type State<Context, Event, Name, Data, Record = nil> = {
identifier: StateIdentifier,
}A checked state, made with S.state or S.withMemory. Only Canopy reads its fields, so its type shows only the identifier that Canopy sets. A hand-written table has no identifier, so it does not type-check as a state.
Other types
These are listed on the page of the API that uses them: