Skip to content

Typing ​

Canopy is built for the Luau analyzer. Give setup its types, and every state, tree and machine built from it is checked:

luau
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>>()
TypeWhat it isWhere it's checked
ContextThe data shared by the whole machineThe first argument of every state function, S.new, machine:getContext()
EventThe names of the events the machine can be sentmachine:send, the keys of on
NameThe names of the statesinitial, handlers, S.goTo, machine:goTo, machine:isIn, tree methods
DataWhat events and goTos carry, nil when they carry nothingmachine:send, machine:goTo, S.goTo, run.data, handler and memory factory arguments
RecordWhat a recorded state adds to the history, nil when nothing is recordedWhat 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.goTo or machine: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 record that returns something other than Record, and a sequence of the wrong type given to endsWith.
  • A table that was not built with S.state or S.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.

luau
const S = Canopy.setup<<Context, Event, Name, Data, nil>>({
	events = {
		"knockedBack",
		"landed",
	},
})

Types ​

Run Added in 0.1.0 ​

luau
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.

FieldTypeDescription
nameNameThe state's name.
dataData?The data of the goTo or event that entered the state, nil when there was none.
memoryMemoryThe state's own data, from its withMemory factory, or nil.
startedAtnumberWhen the state was entered.
endsAtnumberWhen onTimeUp runs, or math.huge with no duration. onStart may change it.
isTimeUpbooleanWhether the time-up has run in this stay.

RunInfo Added in 0.1.0 ​

luau
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 ​

luau
type GoTo<Name, Data> = {
	identifier: GoToIdentifier,
	target: Name,
	data: Data?,
}

Where a state leads, made with S.goTo.

Stay Added in 0.1.0 ​

luau
type Stay = {
	identifier: StayIdentifier,
}

Takes an event without moving, made with S.stay.

Duration Added in 0.1.0 ​

luau
type Duration = number | { number }

Seconds, or { min, max } rolled on each entry.

Handler Added in 0.1.0 ​

luau
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 ​

luau
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: