1- --[[
2- This is a simplified version of Quenty's Nevemore Signal Class.
3- I've stripped this down for improved traceback debugging as I don't mind if the table received is not the same which was passed.
4- If passing the same table is important for you, see: https://github.com/Quenty/NevermoreEngine/blob/a98f213bb46a3c1dbe311b737689c5cc820a4901/Modules/Shared/Events/Signal.lua
5- --]]
6-
7-
8-
1+ local HttpService = game :GetService (" HttpService" )
2+ local RunService = game :GetService (" RunService" )
3+ local heartbeat = RunService .Heartbeat
94local Signal = {}
105Signal .__index = Signal
116Signal .ClassName = " Signal"
@@ -14,67 +9,95 @@ Signal.totalConnections = 0
149
1510
1611-- CONSTRUCTOR
17- function Signal .new (trackConnectionsChanged )
12+ function Signal .new (createConnectionsChangedSignal )
1813 local self = setmetatable ({}, Signal )
19-
20- self ._bindableEvent = Instance .new (" BindableEvent" )
21- if trackConnectionsChanged then
14+
15+ if createConnectionsChangedSignal then
2216 self .connectionsChanged = Signal .new ()
2317 end
2418
19+ self .connections = {}
20+ self .totalConnections = 0
21+ self .waiting = {}
22+ self .totalWaiting = 0
23+
2524 return self
2625end
2726
2827
2928
3029-- METHODS
3130function Signal :Fire (...)
32- self ._bindableEvent :Fire (... )
31+ for _ , connection in pairs (self .connections ) do
32+ connection .Handler (... )
33+ end
34+ if self .totalWaiting > 0 then
35+ local packedArgs = table.pack (... )
36+ for waitingId , _ in pairs (self .waiting ) do
37+ self .waiting [waitingId ] = packedArgs
38+ end
39+ end
3340end
41+ Signal .fire = Signal .Fire
3442
3543function Signal :Connect (handler )
3644 if not (type (handler ) == " function" ) then
3745 error ((" connect(%s)" ):format (typeof (handler )), 2 )
3846 end
3947
40- local connection = self ._bindableEvent .Event :Connect (function (...)
41- handler (... )
42- end )
43-
44- -- If ``true`` is passed for trackConnectionsChanged within the constructor this will track the amount of active connections
48+ local signal = self
49+ local connectionId = HttpService :GenerateGUID (false )
50+ local connection = {}
51+ connection .Connected = true
52+ connection .ConnectionId = connectionId
53+ connection .Handler = handler
54+ self .connections [connectionId ] = connection
55+
56+ function connection :Disconnect ()
57+ signal .connections [connectionId ] = nil
58+ connection .Connected = false
59+ signal .totalConnections -= 1
60+ if signal .connectionsChanged then
61+ signal .connectionsChanged :Fire (- 1 )
62+ end
63+ end
64+ connection .Destroy = connection .Disconnect
65+ connection .destroy = connection .Disconnect
66+ connection .disconnect = connection .Disconnect
67+ self .totalConnections += 1
4568 if self .connectionsChanged then
46- self .totalConnections += 1
4769 self .connectionsChanged :Fire (1 )
48- local heartbeatConection
49- heartbeatConection = game :GetService (" RunService" ).Heartbeat :Connect (function ()
50- if connection .Connected == false then
51- heartbeatConection :Disconnect ()
52- if self .connectionsChanged then
53- self .totalConnections -= 1
54- self .connectionsChanged :Fire (- 1 )
55- end
56- end
57- end )
5870 end
5971
6072 return connection
6173end
74+ Signal .connect = Signal .Connect
6275
6376function Signal :Wait ()
64- local args = self ._bindableEvent .Event :Wait ()
77+ local waitingId = HttpService :GenerateGUID (false )
78+ self .waiting [waitingId ] = true
79+ self .totalWaiting += 1
80+ repeat heartbeat :Wait () until self .waiting [waitingId ] ~= true
81+ self .totalWaiting -= 1
82+ local args = self .waiting [waitingId ]
83+ self .waiting [waitingId ] = nil
6584 return unpack (args )
6685end
86+ Signal .wait = Signal .Wait
6787
6888function Signal :Destroy ()
69- if self ._bindableEvent then
70- self ._bindableEvent :Destroy ()
71- self ._bindableEvent = nil
89+ if self .bindableEvent then
90+ self .bindableEvent :Destroy ()
91+ self .bindableEvent = nil
7292 end
7393 if self .connectionsChanged then
7494 self .connectionsChanged :Fire (- self .totalConnections )
7595 self .connectionsChanged :Destroy ()
7696 self .connectionsChanged = nil
77- self .totalConnections = 0
97+ end
98+ self .totalConnections = 0
99+ for connectionId , connection in pairs (self .connections ) do
100+ self .connections [connectionId ] = nil
78101 end
79102end
80103Signal .destroy = Signal .Destroy
0 commit comments