Skip to content

Testing ​

Transitions move with real frames, and frames are never the same length twice. A test that waits with task.wait sees slightly different values on every run. The manual clock fixes that: transitions only move when the test says so, by an exact amount of time.

luau
const Vide = require("@packages/Vide")
const VideState = require("@VideState")

const schemas = VideState.schemas
const rules = VideState.rules

-- A handler with one field, `opacity`, that is 0 in `Idle` and 1 in `Active`.
const handler = rules.dynamic(schemas.new({
    opacity = schemas.float(),
}), { name = "Fade" }).new({
    Idle = { opacity = Vide.source(0) },
    Active = { opacity = Vide.source(1) },
}, "Idle")

local lastOpacity = 0

VideState.setManualClock(true)

handler.onTransition(function(_, _, current)
    lastOpacity = current.opacity
end, {
    tweenDefault = {
        duration = 1,
    },
})

handler.setState("Active")      -- the first frame: opacity is 0
VideState.advanceClock(0.25)    -- opacity is 0.25
VideState.advanceClock(0.75)    -- opacity is 1, and onFinish runs

VideState.setManualClock(false)
  • While the manual clock is on, real frames do not move any transition. Each advanceClock call steps every running transition of every handler once, as one frame of that length.
  • The time is scaled like a real frame, so pause, a speed source, and scaleSpeed still apply, and so do delays.
  • setState, finish, and an instant change work as usual. setState still sends its first frame at once.
  • Turning the clock off hands the running transitions back to real frames, from where they are.
  • The clock is shared by every handler. Turn it off at the end of each test, so the next test runs with real frames.
  • To run everything to its end without counting the time, call advanceClockUntilIdle. It steps frames until no transition, delay, or sequence hold is left.

Functions ​

setManualClock Added in 0.5.0 ​

luau
VideState.setManualClock(
    enabled: boolean -- Whether transitions wait for advanceClock
) -> ()

Turns the manual clock on or off. It is off by default.

Parameters

NameTypeDescription
enabledbooleantrue stops real frames from moving transitions. false hands them back to real frames.

Errors

CauseMessage
Errorenabled is not a booleansetManualClock expects a boolean, got <type>

isManualClock Added in 0.5.0 ​

luau
VideState.isManualClock() -> boolean

Returns whether the manual clock is on.

Returns

TypeDescription
booleantrue while transitions wait for advanceClock. Defaults to false.

advanceClock Added in 0.5.0 Changed in 0.8.0 ​

luau
VideState.advanceClock(
    seconds: number -- How much time passes
) -> ()

Do not call it from inside a transition callback. It raises an error there, because stepping again in the middle of a frame would corrupt the frame that is running.

Steps every running transition by seconds, as one frame, before it returns. Listeners, animate and progress sources, and onFinish run during the call.

Parameters

NameTypeDescription
secondsnumberThe length of the frame. Must be finite and non-negative.

Errors

CauseMessage
ErrorThe manual clock is offadvanceClock needs the manual clock. Call setManualClock(true) first
Errorseconds is not a finite non-negative numberadvanceClock expects a finite non-negative number of seconds
ErrorIt is called from inside a transition callback, while the frames are being steppedadvanceClock cannot be called from inside a transition callback

advanceClockUntilIdle Added in 0.9.0 ​

luau
VideState.advanceClockUntilIdle(
    frameSeconds: number?, -- The length of each frame. Defaults to 1/60
    maxSeconds: number? -- How long to step before giving up. Defaults to 60
) -> number

Steps every running transition in frames of frameSeconds until every handler is idle: no transition is running, no connection is waiting out a delay, and no playSequence is holding a step. It returns the time it advanced, which is 0 when everything was already idle.

luau
const Vide = require("@packages/Vide")
const VideState = require("@VideState")

const schemas = VideState.schemas
const rules = VideState.rules

const handler = rules.dynamic(schemas.new({
    opacity = schemas.float(),
}), { name = "Menu" }).new({
    Closed = { opacity = Vide.source(0) },
    Open = { opacity = Vide.source(1) },
}, "Closed")

const animated = handler.animate({
    tweenDefault = {
        duration = 0.3,
        delay = 0.1,
    },
})

VideState.setManualClock(true)

handler.setState("Open")
VideState.advanceClockUntilIdle()   -- returns about 0.4
assert(animated.opacity() == 1)

VideState.setManualClock(false)

A step of a sequence can start up to one frame late, because something that starts during a frame is first stepped on the next one, so the time it returns is a whole number of frames. A paused handler and a sequence with loop = true never become idle, so the call raises an error once maxSeconds have passed.

Parameters

NameTypeDescription
frameSecondsnumber?The length of each frame. Must be finite and positive. Defaults to 1/60.
maxSecondsnumber?The longest it steps before it raises an error. Must be finite and positive. Defaults to 60.

Returns

TypeDescription
numberThe time it advanced, in seconds.

Errors

CauseMessage
ErrorThe manual clock is offadvanceClockUntilIdle needs the manual clock. Call setManualClock(true) first
ErrorframeSeconds or maxSeconds is not a finite positive numberadvanceClockUntilIdle expects frameSeconds to be a finite positive number, and the same for maxSeconds
ErrorSomething is still running after maxSeconds, such as a paused handler or a looping sequenceadvanceClockUntilIdle advanced <maxSeconds> seconds and a transition is still running. A paused handler or a looping sequence never becomes idle
ErrorIt is called from inside a transition callbackadvanceClockUntilIdle cannot be called from inside a transition callback