mashkov_dev

Ronaldo Signal

Nov 7th, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | None | 0 0
  1. --!strict
  2. local freeRunnerThread: thread? = nil
  3.  
  4. -- Function which acquires the currently idle handler runner thread, runs the
  5. -- function fn on it, and then releases the thread, returning it to being the
  6. -- currently idle one.
  7. -- If there was a currently idle runner thread already, that's okay, that old
  8. -- one will just get thrown and eventually GCed.
  9. local function acquireRunnerThreadAndCallEventHandler(fn, ...)
  10.     local acquiredRunnerThread = freeRunnerThread
  11.     freeRunnerThread = nil
  12.     fn(...)
  13.     -- The handler finished running, this runner thread is free again.
  14.     freeRunnerThread = acquiredRunnerThread
  15. end
  16.  
  17. -- Coroutine runner that we create coroutines of. The coroutine can be
  18. -- repeatedly resumed with functions to run followed by the argument to run
  19. -- them with.
  20. local function runEventHandlerInFreeThread()
  21.     -- Note: We cannot use the initial set of arguments passed to
  22.     -- runEventHandlerInFreeThread for a call to the handler, because those
  23.     -- arguments would stay on the stack for the duration of the thread's
  24.     -- existence, temporarily leaking references. Without access to raw bytecode
  25.     -- there's no way for us to clear the "..." references from the stack.
  26.     while true do
  27.         acquireRunnerThreadAndCallEventHandler(coroutine.yield())
  28.     end
  29. end
  30.  
  31.  
  32. local Connection = {}
  33. Connection.__index = Connection
  34.  
  35.  
  36. function Connection.new(signal: SimpleSignal, callback: (...any) -> ()): Connection
  37.     local self = setmetatable({}, Connection) :: Connection
  38.     self._connected = true
  39.     self._signal = signal
  40.     self._callback = callback
  41.     self._next = nil
  42.     self._prev = nil
  43.     return self
  44. end
  45.  
  46.  
  47. function Connection.Disconnect(self: Connection): ()
  48.     if not self._connected then return end
  49.     self._connected = false
  50.    
  51.     if self._signal._head == self then
  52.         self._signal._head = self._next
  53.     end
  54.    
  55.     if self._prev then
  56.         self._prev._next = self._next
  57.     end
  58.    
  59.     if self._next then
  60.         self._next._prev = self._prev
  61.     end
  62. end
  63.  
  64.  
  65. local RonaldoSignal = {}
  66. RonaldoSignal.__index = RonaldoSignal
  67.  
  68.  
  69. function RonaldoSignal.new(): unknown
  70.     local self = setmetatable({}, RonaldoSignal) :: SimpleSignal
  71.     self._head = nil
  72.  
  73.     local function Fire(...: any): ()
  74.         local connection = self._head
  75.         while connection do
  76.             if connection._connected then
  77.                 if not freeRunnerThread then
  78.                     freeRunnerThread = coroutine.create(runEventHandlerInFreeThread)
  79.                     -- Get the freeRunnerThread to the first yield
  80.                     coroutine.resume(freeRunnerThread :: thread)
  81.                 end
  82.                 task.spawn(freeRunnerThread :: thread, connection._callback, ...)
  83.             end
  84.             connection = connection._next
  85.         end
  86.     end
  87.  
  88.     self.Fire = Fire
  89.  
  90.     return self
  91. end
  92.  
  93.  
  94. function RonaldoSignal.Connect(self: SimpleSignal, callback: (...any) -> ()): Connection
  95.     local connection = Connection.new(self, callback)
  96.     if self._head then
  97.         self._head._prev = connection
  98.         connection._next = self._head
  99.     end
  100.     self._head = connection
  101.     return connection
  102. end
  103.  
  104.  
  105. function RonaldoSignal.Once(self: SimpleSignal, callback: (...any) -> ()): Connection
  106.     local connection: Connection
  107.     connection = self:Connect(function(...: any)
  108.         connection:Disconnect()
  109.         callback(...)
  110.     end)
  111.     return connection
  112. end
  113.  
  114.  
  115. function RonaldoSignal.DisconnectAll(self: SimpleSignal): ()
  116.     self._head = nil
  117. end
  118.  
  119.  
  120. function RonaldoSignal.Wait(self: SimpleSignal): ...any
  121.     local thread = coroutine.running()
  122.     self:Once(function(...: any)
  123.         task.spawn(thread, ...)
  124.     end)
  125.     return coroutine.yield()
  126. end
  127.  
  128.  
  129. export type RonaldoSignal<T, U...> = {
  130.     Fire: T,
  131.     Connect: (self: RonaldoSignal<T, U...>, func: T) -> Connection,
  132.     Once: (self: RonaldoSignal<T, U...>, func: T) -> Connection,
  133.     DisconnectAll: (self: RonaldoSignal<T, U...>) -> (),
  134.     Wait: (self: RonaldoSignal<T, U...>) -> U...
  135. }
  136.  
  137.  
  138. type SimpleSignal = {
  139.     Fire: (...any) -> (),
  140.     Connect: (self: SimpleSignal, func: (...any) -> ()) -> Connection,
  141.     Once: (self: SimpleSignal, func: (...any) -> ()) -> Connection,
  142.     DisconnectAll: (self: SimpleSignal) -> (),
  143.     Wait: (self: SimpleSignal) -> ...any,
  144.    
  145.     _head: Connection?
  146. }
  147.  
  148.  
  149. export type Connection = {
  150.     Disconnect: (self: Connection) -> (),
  151.    
  152.     _connected: boolean,
  153.     _signal: SimpleSignal,
  154.     _callback: (...any) -> (),
  155.    
  156.     _prev: Connection?,
  157.     _next: Connection?
  158. }
  159.  
  160.  
  161. return RonaldoSignal
Advertisement
Add Comment
Please, Sign In to add comment