Skip to content
59 changes: 56 additions & 3 deletions modules/signals-scheduler/src/SignalsScheduler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ local isContinuing = false

local continuations: { work } = {}

--[[
Counters for a profiler, supplied by the host rather than required, so nothing
here knows about the runtime using it. Bound as upvalues rather than read out of
a table per batch, so a hook that is not installed costs what a constant did.
]]
export type Hooks = {
onBatch: (() -> ())?,
onContinuations: ((count: number) -> ())?,
}

local function noop() end

local onBatch: () -> () = noop
local onContinuations: (count: number) -> () = noop
local hooksInstalled = false

local function setHooks(hooks: Hooks?)
Comment thread
hcollinsblox marked this conversation as resolved.
Resolved
local given: any = if hooks ~= nil then hooks else {}
onBatch = given.onBatch or noop
onContinuations = given.onContinuations or noop
hooksInstalled = next(given) ~= nil
end

--[[
Both drains below are indexed rather than iterated.

Expand All @@ -16,8 +39,7 @@ local continuations: { work } = {}
keeps the append behavior clear without relying on generalised iteration over
a table that changes during the loop.
]]
local function runWork(fn: work)
fn()
local function runWork()
local i = 1
while i <= #continuations do
continuations[i]()
Expand Down Expand Up @@ -45,22 +67,52 @@ local function batch(fn: work)

if SignalsSchedulerResetStateAfterErrors then
local ok, err: any = xpcall(fn, debug.traceback)
local batchHookOk, batchHookErr: any = true, nil
if hooksInstalled and #continuations > 0 then
batchHookOk, batchHookErr = xpcall(onBatch, debug.traceback)
end
local continuationsOk, continuationsErr: any = runContinuations()
local hookOk, hookErr: any = true, nil
if hooksInstalled and #continuations > 0 then
hookOk, hookErr = xpcall(onContinuations, debug.traceback, #continuations)
end

table.clear(continuations)
isContinuing = false

if not ok then
error(err, 0)
end
if not batchHookOk then
error(batchHookErr, 0)
end
if not continuationsOk then
error(continuationsErr, 0)
end
if not hookOk then
error(hookErr, 0)
end
else
runWork(fn)
fn()
if hooksInstalled and #continuations > 0 then
local hookOk, hookErr: any = xpcall(onBatch, debug.traceback)
if not hookOk then
table.clear(continuations)
isContinuing = false
error(hookErr, 0)
end
end
runWork()
local hookOk, hookErr: any = true, nil
if hooksInstalled and #continuations > 0 then
hookOk, hookErr = xpcall(onContinuations, debug.traceback, #continuations)
end

table.clear(continuations)
isContinuing = false
if not hookOk then
error(hookErr, 0)
end
end
else
fn()
Expand All @@ -79,4 +131,5 @@ return {
batch = batch,
flush = flush,
schedule = schedule,
setHooks = setHooks,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,57 @@ local batch = SignalsScheduler.batch
local flush = SignalsScheduler.flush
local schedule = SignalsScheduler.schedule

it("reports one outer batch and all continuations drained in that batch", function()
local batches = 0
local counts = {}
SignalsScheduler.setHooks({
onBatch = function()
batches += 1
end,
onContinuations = function(count)
table.insert(counts, count)
end,
})

batch(function()
batch(function()
schedule(function()
schedule(function() end)
end)
end)
end)
expect(batches).toEqual(1)
expect(counts).toEqual({ 2 })

-- An empty flush has no work to profile.
flush()
expect(batches).toEqual(1)
expect(counts).toEqual({ 2 })
SignalsScheduler.setHooks(nil)
end)

it("resets scheduler state when a continuation hook errors", function()
SignalsScheduler.setHooks({
onContinuations = function()
error("hook failed")
end,
})
expect(function()
batch(function()
schedule(function() end)
end)
end).toThrow("hook failed")
SignalsScheduler.setHooks(nil)

local ran = false
batch(function()
schedule(function()
ran = true
end)
end)
expect(ran).toEqual(true)
end)

it("should schedule work without running until flushed", function()
local counters = {
foo = 0,
Expand Down
1 change: 1 addition & 0 deletions modules/signals-scheduler/src/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local SignalsScheduler = require(script.SignalsScheduler)

export type work = SignalsScheduler.work
export type Hooks = SignalsScheduler.Hooks

return SignalsScheduler
Loading