Trees
A tree is built once with S.tree and shared by every machine made from it. It is frozen, and its methods answer questions about its states without a machine.
The examples on this page use the tree from Getting started, where knockedBack holds flying and groundImpact.
Tree methods
hasState Added in 0.1.0
tree:hasState(name: string): booleanReturns whether the tree has a state named name, at any depth. name is any string, so use it to check a name that comes from data.
getPath Added in 0.1.0
tree:getPath(name: Name): { Name }?Returns the names from the outermost state down to name, or nil when the tree has no such state. tree:getPath("flying") is { "knockedBack", "flying" }. The table is frozen.
getParent Added in 0.1.0
tree:getParent(name: Name): Name?Returns the state that holds name, or nil for an outermost state or a name the tree does not have.
isWithin Added in 0.1.0
tree:isWithin(name: Name, withinName: Name): booleanReturns whether name is withinName or a state inside it. tree:isWithin("flying", "knockedBack") is true.
Checking objects
Each object Canopy builds carries a private identifier, compared by identity, so a hand-written table with the same fields is never taken for one. S.tree and S.new use these checks to refuse anything Canopy did not build.
isState Added in 0.1.0
Canopy.isState(value: unknown): booleanReturns whether value was made by state or withMemory.
isTree Added in 0.1.0
Canopy.isTree(value: unknown): booleanReturns whether value was made by tree.
isGoTo Added in 0.1.0
Canopy.isGoTo(value: unknown): booleanReturns whether value was made by goTo.
isStay Added in 0.1.0
Canopy.isStay(value: unknown): booleanReturns whether value was made by stay.
isMachine Added in 0.1.0
Canopy.isMachine(value: unknown): booleanReturns whether value was made by new.
isHistory Added in 0.1.0
Canopy.isHistory(value: unknown): booleanReturns whether value was made by history.
isValidDuration Added in 0.1.0
Canopy.isValidDuration(duration: Duration): booleanReturns whether duration is seconds (0 or more), or { min, max } with 0 <= min <= max. Use it to check a duration that comes from data before building a state with it.