Skip to content

Commit b2f72cd

Browse files
authored
Merge pull request #1 from Zamdie/main
Use only the Signal table for all object functions.
2 parents 049f503 + cf5d954 commit b2f72cd

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

init.lua

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
local runService = game:GetService("RunService")
22

33
local Signal = {}
4-
local signalFuncs = {}
5-
local connectionFuncs = {}
4+
Signal.__index = Signal
65

76
local function doesYield(func, ...)
87
local packed = table.pack(...)
@@ -15,25 +14,24 @@ local function doesYield(func, ...)
1514
return not completed
1615
end
1716

18-
1917
function Signal.new()
20-
return setmetatable({
21-
_connections = {};
22-
}, {
23-
__index = signalFuncs
24-
})
18+
19+
local self = setmetatable({
20+
_connections = {}
21+
}, Signal)
22+
23+
return self
2524
end
2625

27-
function signalFuncs.Destroy(self)
26+
function Signal:Destroy()
2827
for i, connection in ipairs(self._connections) do
2928
connection:Disconnect()
3029
self._connections[i] = nil
3130
end
32-
setmetatable(self, nil)
33-
table.clear(self)
31+
self = nil
3432
end
3533

36-
function signalFuncs.Fire(self, ...)
34+
function Signal:Fire(...)
3735
for _, connection in pairs(self._connections) do
3836
if connection.Function then
3937
local thread = coroutine.create(connection.Function)
@@ -42,7 +40,7 @@ function signalFuncs.Fire(self, ...)
4240
end
4341
end
4442

45-
function signalFuncs.FireNoYield(self, ...)
43+
function Signal:FireNoYield(...)
4644
for _, connection in pairs(self._connections) do
4745
if connection.Function then
4846
local yields = doesYield(connection.Function, ...)
@@ -53,14 +51,14 @@ function signalFuncs.FireNoYield(self, ...)
5351
end
5452
end
5553

56-
function signalFuncs.Wait(self)
57-
local fired = false;
54+
function Signal:Wait()
55+
local fired = false
5856

5957
local connection = self:Connect(function()
6058
fired = true
6159
end)
62-
local startTime = os.clock();
63-
60+
61+
local startTime = os.clock()
6462
repeat
6563
runService.Stepped:Wait()
6664
until fired or not connection.Connected
@@ -69,23 +67,20 @@ function signalFuncs.Wait(self)
6967
return os.clock() - startTime
7068
end
7169

72-
function signalFuncs.Connect(self, givenFunction)
70+
function Signal:Connect(givenFunction)
7371
assert(typeof(givenFunction) == "function", "You need to give a function.")
74-
75-
local connection = setmetatable({
76-
Function = givenFunction;
77-
Connected = true;
78-
}, {
79-
__index = connectionFuncs;
80-
})
81-
table.insert(self._connections, connection)
72+
73+
local connection = {
74+
Function = givenFunction,
75+
Connected = true
76+
}
77+
table.insert(self, #self + 1, connection)
78+
8279
return connection
8380
end
8481

85-
function connectionFuncs.Disconnect(self)
86-
self.Function = nil;
87-
self.Connected = false
88-
setmetatable(self, nil)
82+
function Signal:Disconnect()
83+
table.clear(self._connections)
8984
end
9085

9186

0 commit comments

Comments
 (0)