Last active
October 17, 2021 01:04
-
-
Save lucasmz-dev/4317b4803780b5fcc432add74f3c49ce to your computer and use it in GitHub Desktop.
Faster Task.Spawn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local FreeThread: thread? = nil | |
local function RunHandlerInFreeThread(handle, ...) | |
local thread = FreeThread :: thread | |
FreeThread = nil | |
handle(...) | |
FreeThread = thread | |
end | |
local function CreateFreeThread() | |
FreeThread = coroutine.running() | |
while true do | |
RunHandlerInFreeThread( coroutine.yield() ) | |
end | |
end | |
return function( | |
handle: (...any) -> (), | |
... | |
) | |
assert( | |
typeof(handle) == 'function', | |
"Must be function" | |
) | |
if FreeThread == nil then | |
task.spawn(CreateFreeThread) | |
end | |
task.spawn( | |
FreeThread :: thread, | |
handle, ... | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment