Created
October 16, 2022 21:04
-
-
Save MagmaBurnsV/a38d2154c49d0b9fb9e55bab07b5cb75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
--!strict | |
local Worker = {} | |
Worker.__index = Worker | |
type Callback = (...any) -> ...any | |
type self = { | |
_ParallelEvent: BindableEvent, | |
_SerialEvent: BindableEvent, | |
_IsHandled: boolean | |
} | |
export type Worker = typeof(setmetatable({} :: self, Worker)) | |
function Worker.new(): Worker | |
local self = setmetatable({} :: self, Worker) | |
self._ParallelEvent = Instance.new("BindableEvent") | |
self._SerialEvent = Instance.new("BindableEvent") | |
self._IsHandled = false | |
return self | |
end | |
function Worker:Handle(Callback: Callback): () | |
local self: Worker = self | |
if not self._IsHandled then | |
self._IsHandled = true | |
self._ParallelEvent.Event:ConnectParallel(function(...: any): () | |
self._SerialEvent:Fire(Callback(...)) | |
end) | |
end | |
end | |
function Worker:DoTask(...: any): ...any | |
local self: Worker = self | |
self._ParallelEvent:Fire(...) | |
return self._SerialEvent.Event:Wait() | |
end | |
return Worker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment