Skip to content

Commit

Permalink
Merge branch 'development' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
1ForeverHD committed May 21, 2021
2 parents fc3d298 + 325aa92 commit 8cdcafd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 115 deletions.
149 changes: 35 additions & 114 deletions src/Zone/Signal.lua
Original file line number Diff line number Diff line change
@@ -1,123 +1,49 @@
-- Signal
-- Author: Stephen Leitnick
-- Source: https://github.com/Sleitnick/AeroGameFramework/blob/43e4e02717e36ac83c820abc4461fb8afa2cd967/src/ReplicatedStorage/Aero/Shared/Signal.lua
-- License: MIT (https://github.com/Sleitnick/AeroGameFramework/blob/master/LICENSE)
-- Modified for use in Nanoblox


local Connection = {}
Connection.__index = Connection

function Connection.new(signal, connection)
local self = setmetatable({
_signal = signal;
_conn = connection;
Connected = true;
}, Connection)
return self
end

function Connection:Disconnect()
if (self._conn) then
self._conn:Disconnect()
self._conn = nil
end
if (not self._signal) then return end
self.Connected = false
local connections = self._signal._connections
local connectionIndex = table.find(connections, self)
if (connectionIndex) then
local n = #connections
connections[connectionIndex] = connections[n]
connections[n] = nil
end
self._signal = nil
end

function Connection:IsConnected()
if (self._conn) then
return self._conn.Connected
end
return false
end
--[[
This is a simplified version of Quenty's Nevemore Signal Class.
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.
If passing the same table is important for you, see: https://github.com/Quenty/NevermoreEngine/blob/a98f213bb46a3c1dbe311b737689c5cc820a4901/Modules/Shared/Events/Signal.lua
--]]

Connection.Destroy = Connection.Disconnect

--------------------------------------------

local Signal = {}
Signal.totalConnections = 0
Signal.__index = Signal
Signal.ClassName = "Signal"
Signal.totalConnections = 0



-- CONSTRUCTOR
function Signal.new(trackConnectionsChanged)
local self = setmetatable({
_bindable = Instance.new("BindableEvent");
_connections = {};
_args = {};
_threads = 0;
_id = 0;
}, Signal)
local self = setmetatable({}, Signal)

self._bindableEvent = Instance.new("BindableEvent")
if trackConnectionsChanged then
self.connectionsChanged = Signal.new()
end
return self
end

function Signal:_setProxy(rbxScriptSignal)
assert(typeof(rbxScriptSignal) == "RBXScriptSignal", "Argument #1 must be of type RBXScriptSignal")
self:_clearProxy()
self._proxyHandle = rbxScriptSignal:Connect(function(...)
self:Fire(...)
end)
return self
end


function Signal:_clearProxy()
if (self._proxyHandle) then
self._proxyHandle:Disconnect()
self._proxyHandle = nil
end
end


-- METHODS
function Signal:Fire(...)
local totalListeners = (#self._connections + self._threads)
if (totalListeners == 0) then return end
local id = self._id
self._id += 1
self._args[id] = {totalListeners, {n = select("#", ...), ...}}
self._threads = 0
self._bindable:Fire(id)
self._bindableEvent:Fire(...)
end


function Signal:Wait()
self._threads += 1
local id = self._bindable.Event:Wait()
local args = self._args[id]
args[1] -= 1
if (args[1] <= 0) then
self._args[id] = nil
end
return table.unpack(args[2], 1, args[2].n)
end


function Signal:Connect(handler)
local connection = Connection.new(self, self._bindable.Event:Connect(function(id)
local args = self._args[id]
args[1] -= 1
if (args[1] <= 0) then
self._args[id] = nil
end
handler(table.unpack(args[2], 1, args[2].n))
end))
table.insert(self._connections, connection)
--
-- this enables us to determine when a signal is connected to from an outside source
self.totalConnections += 1
if not (type(handler) == "function") then
error(("connect(%s)"):format(typeof(handler)), 2)
end

local connection = self._bindableEvent.Event:Connect(function(...)
handler(...)
end)

-- If ``true`` is passed for trackConnectionsChanged within the constructor this will track the amount of active connections
if self.connectionsChanged then
self.totalConnections += 1
self.connectionsChanged:Fire(1)
local heartbeatConection
heartbeatConection = game:GetService("RunService").Heartbeat:Connect(function()
Expand All @@ -130,26 +56,20 @@ function Signal:Connect(handler)
end
end)
end
--

return connection
end


function Signal:DisconnectAll()
for _,c in ipairs(self._connections) do
if (c._conn) then
c._conn:Disconnect()
end
end
self._connections = {}
self._args = {}
function Signal:Wait()
local args = self._bindableEvent.Event:Wait()
return unpack(args)
end


function Signal:Destroy()
self:DisconnectAll()
self:_clearProxy()
self._bindable:Destroy()
if self._bindableEvent then
self._bindableEvent:Destroy()
self._bindableEvent = nil
end
if self.connectionsChanged then
self.connectionsChanged:Fire(-self.totalConnections)
self.connectionsChanged:Destroy()
Expand All @@ -159,7 +79,8 @@ function Signal:Destroy()
end
Signal.destroy = Signal.Destroy
Signal.Disconnect = Signal.Destroy
Signal.Disconnect = Signal.Destroy
Signal.disconnect = Signal.Destroy



return Signal
2 changes: 1 addition & 1 deletion src/Zone/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ function Zone:_update()
--]]

self:_updateTouchedConnections()

self.updated:Fire()
end

Expand Down

0 comments on commit 8cdcafd

Please sign in to comment.