2023-01-22 22:06:35 +00:00
|
|
|
-- Deferring a task should return the thread that can then be cancelled
|
|
|
|
|
|
|
|
local thread = task.defer(function() end)
|
|
|
|
assert(type(thread) == "thread", "Defer should return the thread spawned")
|
|
|
|
|
2023-01-21 20:07:18 +00:00
|
|
|
-- Deferred functions should run after other threads
|
|
|
|
|
|
|
|
local flag: boolean = false
|
|
|
|
task.defer(function()
|
|
|
|
flag = true
|
|
|
|
end)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(not flag, "Defer should not run instantly or block")
|
2023-01-23 22:50:11 +00:00
|
|
|
task.wait(0.1)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(flag, "Defer should run")
|
2023-01-21 20:07:18 +00:00
|
|
|
|
|
|
|
-- Deferred functions should work with yielding
|
|
|
|
|
|
|
|
local flag2: boolean = false
|
|
|
|
task.defer(function()
|
2023-01-23 22:50:11 +00:00
|
|
|
task.wait(0.1)
|
2023-01-21 20:07:18 +00:00
|
|
|
flag2 = true
|
|
|
|
end)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(not flag2, "Defer should work with yielding (1)")
|
2023-01-23 22:50:11 +00:00
|
|
|
task.wait(0.2)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(flag2, "Defer should work with yielding (2)")
|
2023-01-21 20:07:18 +00:00
|
|
|
|
|
|
|
-- Deferred functions should run after other spawned threads
|
2023-01-24 17:51:12 +00:00
|
|
|
local flag3: number = 1
|
2023-01-21 20:07:18 +00:00
|
|
|
task.defer(function()
|
2023-01-24 17:51:12 +00:00
|
|
|
if flag3 == 2 then
|
|
|
|
flag3 = 3
|
2023-01-21 20:07:18 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
task.spawn(function()
|
2023-01-24 17:51:12 +00:00
|
|
|
if flag3 == 1 then
|
|
|
|
flag3 = 2
|
2023-01-21 20:07:18 +00:00
|
|
|
end
|
|
|
|
end)
|
2023-01-24 17:51:12 +00:00
|
|
|
task.wait()
|
|
|
|
assert(flag3 == 3, "Defer should run after spawned threads")
|
2023-01-21 20:07:18 +00:00
|
|
|
|
|
|
|
-- Varargs should get passed correctly
|
|
|
|
|
2023-01-24 17:24:57 +00:00
|
|
|
local function fcheck(index: number, type: string, value: any)
|
|
|
|
if typeof(value) ~= type then
|
|
|
|
console.error(
|
|
|
|
string.format(
|
|
|
|
"Expected argument #%d to be of type %s, got %s",
|
|
|
|
index,
|
|
|
|
type,
|
|
|
|
console.format(value)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function f(...: any)
|
|
|
|
fcheck(1, "string", select(1, ...))
|
|
|
|
fcheck(2, "number", select(2, ...))
|
|
|
|
fcheck(3, "function", select(3, ...))
|
2023-01-21 20:07:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
task.defer(f, "", 1, f)
|
|
|
|
task.defer(f, "inf", math.huge, f)
|
|
|
|
task.defer(f, "NaN", 0 / 0, f)
|