Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --!strict
- local freeRunnerThread: thread? = nil
- -- Function which acquires the currently idle handler runner thread, runs the
- -- function fn on it, and then releases the thread, returning it to being the
- -- currently idle one.
- -- If there was a currently idle runner thread already, that's okay, that old
- -- one will just get thrown and eventually GCed.
- local function acquireRunnerThreadAndCallEventHandler(fn, ...)
- local acquiredRunnerThread = freeRunnerThread
- freeRunnerThread = nil
- fn(...)
- -- The handler finished running, this runner thread is free again.
- freeRunnerThread = acquiredRunnerThread
- end
- -- Coroutine runner that we create coroutines of. The coroutine can be
- -- repeatedly resumed with functions to run followed by the argument to run
- -- them with.
- local function runEventHandlerInFreeThread()
- -- Note: We cannot use the initial set of arguments passed to
- -- runEventHandlerInFreeThread for a call to the handler, because those
- -- arguments would stay on the stack for the duration of the thread's
- -- existence, temporarily leaking references. Without access to raw bytecode
- -- there's no way for us to clear the "..." references from the stack.
- while true do
- acquireRunnerThreadAndCallEventHandler(coroutine.yield())
- end
- end
- local Connection = {}
- Connection.__index = Connection
- function Connection.new(signal: SimpleSignal, callback: (...any) -> ()): Connection
- local self = setmetatable({}, Connection) :: Connection
- self._connected = true
- self._signal = signal
- self._callback = callback
- self._next = nil
- self._prev = nil
- return self
- end
- function Connection.Disconnect(self: Connection): ()
- if not self._connected then return end
- self._connected = false
- if self._signal._head == self then
- self._signal._head = self._next
- end
- if self._prev then
- self._prev._next = self._next
- end
- if self._next then
- self._next._prev = self._prev
- end
- end
- local RonaldoSignal = {}
- RonaldoSignal.__index = RonaldoSignal
- function RonaldoSignal.new(): unknown
- local self = setmetatable({}, RonaldoSignal) :: SimpleSignal
- self._head = nil
- local function Fire(...: any): ()
- local connection = self._head
- while connection do
- if connection._connected then
- if not freeRunnerThread then
- freeRunnerThread = coroutine.create(runEventHandlerInFreeThread)
- -- Get the freeRunnerThread to the first yield
- coroutine.resume(freeRunnerThread :: thread)
- end
- task.spawn(freeRunnerThread :: thread, connection._callback, ...)
- end
- connection = connection._next
- end
- end
- self.Fire = Fire
- return self
- end
- function RonaldoSignal.Connect(self: SimpleSignal, callback: (...any) -> ()): Connection
- local connection = Connection.new(self, callback)
- if self._head then
- self._head._prev = connection
- connection._next = self._head
- end
- self._head = connection
- return connection
- end
- function RonaldoSignal.Once(self: SimpleSignal, callback: (...any) -> ()): Connection
- local connection: Connection
- connection = self:Connect(function(...: any)
- connection:Disconnect()
- callback(...)
- end)
- return connection
- end
- function RonaldoSignal.DisconnectAll(self: SimpleSignal): ()
- self._head = nil
- end
- function RonaldoSignal.Wait(self: SimpleSignal): ...any
- local thread = coroutine.running()
- self:Once(function(...: any)
- task.spawn(thread, ...)
- end)
- return coroutine.yield()
- end
- export type RonaldoSignal<T, U...> = {
- Fire: T,
- Connect: (self: RonaldoSignal<T, U...>, func: T) -> Connection,
- Once: (self: RonaldoSignal<T, U...>, func: T) -> Connection,
- DisconnectAll: (self: RonaldoSignal<T, U...>) -> (),
- Wait: (self: RonaldoSignal<T, U...>) -> U...
- }
- type SimpleSignal = {
- Fire: (...any) -> (),
- Connect: (self: SimpleSignal, func: (...any) -> ()) -> Connection,
- Once: (self: SimpleSignal, func: (...any) -> ()) -> Connection,
- DisconnectAll: (self: SimpleSignal) -> (),
- Wait: (self: SimpleSignal) -> ...any,
- _head: Connection?
- }
- export type Connection = {
- Disconnect: (self: Connection) -> (),
- _connected: boolean,
- _signal: SimpleSignal,
- _callback: (...any) -> (),
- _prev: Connection?,
- _next: Connection?
- }
- return RonaldoSignal
Advertisement
Add Comment
Please, Sign In to add comment