Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Properly handle varargs with trailing 'nil' arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Jan 6, 2020
1 parent 5ea9344 commit 5e80d1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/ReplicatedStorage/Aero/Shared/Thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,34 @@ function Thread.SpawnNow(func, ...)
https://github.com/Quenty/NevermoreEngine/blob/version2/LICENSE.md
--]]
local args = {...}
local n = select("#", ...)
local bindable = Instance.new("BindableEvent")
bindable.Event:Connect(function() func(table.unpack(args)) end)
bindable.Event:Connect(function() func(table.unpack(args, 1, n)) end)
bindable:Fire()
bindable:Destroy()
end


function Thread.Spawn(func, ...)
local args = {...}
local n = select("#", ...)
local hb
hb = heartbeat:Connect(function()
hb:Disconnect()
func(table.unpack(args))
func(table.unpack(args, 1, n))
end)
end


function Thread.Delay(waitTime, func, ...)
local args = {...}
local n = select("#", ...)
local executeTime = (tick() + waitTime)
local hb
hb = heartbeat:Connect(function()
if (tick() >= executeTime) then
hb:Disconnect()
func(table.unpack(args))
func(table.unpack(args, 1, n))
end
end)
return hb
Expand All @@ -149,12 +152,13 @@ end

function Thread.DelayRepeat(intervalTime, func, ...)
local args = {...}
local n = select("#", ...)
local nextExecuteTime = (tick() + intervalTime)
local hb
hb = heartbeat:Connect(function()
if (tick() >= nextExecuteTime) then
nextExecuteTime = (tick() + intervalTime)
func(table.unpack(args))
func(table.unpack(args, 1, n))
end
end)
return hb
Expand Down
3 changes: 2 additions & 1 deletion src/ServerStorage/Aero/Modules/Data/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ end
local function HeartbeatSpawn(callback, ...)
local hb
local args = {...}
local n = select("#", ...)
hb = game:GetService("RunService").Heartbeat:Connect(function()
hb:Disconnect()
callback(unpack(args))
callback(table.unpack(args, 1, n))
end)
end

Expand Down

0 comments on commit 5e80d1b

Please sign in to comment.