-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakebindable.lua
106 lines (81 loc) · 1.94 KB
/
fakebindable.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local bindableevent = {}
local event = {}
local connection = {}
bindableevent.__index = bindableevent
event.__index = event
connection.__index = connection
bindableevent.__tostring = function()
return "BindableEvent"
end
event.__tostring = function(self)
return `Signal {self._Name}`
end
connection.__tostring = function()
return "Connection"
end
function connection:Disconnect()
self.Connected = false
self._Callback = nil
table.remove(self._Parent._Connections, table.find(self._Parent._Connections, self))
end
connection.disconnect = connection.Disconnect
function connection.new(parentevent, callback, isonce)
local new = setmetatable({
Connected = true,
_Parent = parentevent,
_Once = isonce,
_Callback = callback,
}, connection)
return new
end
function event:Connect(callback)
local c = connection.new(self, callback)
table.insert(self._Connections, c)
return c
end
function event:Wait()
local currentargs = self._LatestArgs
repeat
task.wait()
until self._LatestArgs ~= currentargs
return unpack(self._LatestArgs)
end
function event:Once(callback)
local c = connection.new(self, callback, true)
table.insert(self._Connections, c)
return c
end
event.connect = event.Connect
event.wait = event.Wait
event.once = event.Once
function event.new(name)
local new = setmetatable({
_Name = name,
_LatestArgs = {},
_Connections = {},
}, event)
return new
end
function bindableevent:Fire(...)
self.Event._LatestArgs = {...}
for i, connection in self.Event._Connections do
task.spawn(connection._Callback, ...)
if connection._Once then
connection:Disconnect()
end
end
end
function bindableevent:Destroy()
for _, c in self.Event._Connections do
c:Disconnect()
end
end
bindableevent.fire = bindableevent.Fire
bindableevent.destroy = bindableevent.Destroy
function bindableevent.new(name): BindableEvent
local new = setmetatable({
Event = event.new(name)
}, bindableevent)
return new
end
return bindableevent