|
| 1 | +local runService = game:GetService("RunService") |
| 2 | + |
| 3 | +local Signal = {} |
| 4 | +local signalFuncs = {} |
| 5 | +local connectionFuncs = {} |
| 6 | + |
| 7 | +local function doesYield(func, ...) |
| 8 | + local packed = table.pack(...) |
| 9 | + local completed = false |
| 10 | + local thread = coroutine.create(function() |
| 11 | + func(table.unpack(packed)) |
| 12 | + completed = true |
| 13 | + end) |
| 14 | + coroutine.resume(thread) |
| 15 | + return not completed |
| 16 | +end |
| 17 | + |
| 18 | + |
| 19 | +function Signal.new() |
| 20 | + return setmetatable({ |
| 21 | + _connections = {}; |
| 22 | + }, { |
| 23 | + __index = signalFuncs |
| 24 | + }) |
| 25 | +end |
| 26 | + |
| 27 | +function signalFuncs.Destroy(self) |
| 28 | + for i, connection in ipairs(self._connections) do |
| 29 | + connection:Disconnect() |
| 30 | + self._connections[i] = nil |
| 31 | + end |
| 32 | + setmetatable(self, nil) |
| 33 | + table.clear(self) |
| 34 | +end |
| 35 | + |
| 36 | +function signalFuncs.Fire(self, ...) |
| 37 | + for _, connection in pairs(self._connections) do |
| 38 | + if connection._fromScript.Parent == nil then |
| 39 | + connection:Disconnect() |
| 40 | + end |
| 41 | + if connection.Function then |
| 42 | + local thread = coroutine.create(connection.Function) |
| 43 | + coroutine.resume(thread, ...) |
| 44 | + end |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +function signalFuncs.FireNoYield(self, ...) |
| 49 | + for _, connection in pairs(self._connections) do |
| 50 | + if connection._fromScript and connection._fromScript.Parent == nil then |
| 51 | + connection:Disconnect() |
| 52 | + end |
| 53 | + if connection.Function then |
| 54 | + local yields = doesYield(connection.Function, ...) |
| 55 | + if yields then |
| 56 | + error(":FireQueueNoYield() doesn't allow any connections to yield! Script interrupted!") |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +function signalFuncs.Wait(self) |
| 63 | + local fired = false; |
| 64 | + |
| 65 | + local connection = self:Connect(function() |
| 66 | + fired = true |
| 67 | + end) |
| 68 | + local startTime = os.clock(); |
| 69 | + |
| 70 | + repeat |
| 71 | + runService.Stepped:Wait() |
| 72 | + until fired or not connection.Connected |
| 73 | + |
| 74 | + connection:Disconnect() |
| 75 | + return os.clock() - startTime |
| 76 | +end |
| 77 | + |
| 78 | +function signalFuncs.Connect(self, givenFunction) |
| 79 | + assert(typeof(givenFunction) == "function", "You need to give a function.") |
| 80 | + |
| 81 | + local fromScript = getfenv(givenFunction).script |
| 82 | + |
| 83 | + local connection = setmetatable({ |
| 84 | + Function = givenFunction; |
| 85 | + _fromScript = fromScript; |
| 86 | + Connected = true; |
| 87 | + }, { |
| 88 | + __index = connectionFuncs; |
| 89 | + }) |
| 90 | + table.insert(self._connections, connection) |
| 91 | + return connection |
| 92 | +end |
| 93 | + |
| 94 | +function connectionFuncs.Disconnect(self) |
| 95 | + self.Function = nil; |
| 96 | + self.Connected = false |
| 97 | + setmetatable(self, nil) |
| 98 | +end |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +return Signal |
0 commit comments