This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibCustomEvent.lua
127 lines (115 loc) · 4.09 KB
/
LibCustomEvent.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
--[[
File name: LibCustomEvent.lua
Author: RadiatedExodus (RealEthanPlayz/RealEthanPlayzDev/ItzEthanPlayz_YT)
Version: 2 (revision 1)
NOTES:
- v2 is more readable
--]]
--// Classes
local LibCustomEventConstructor = {} --// Constructor class
local LibCustomEvent = {} --// Base class
local LibCustomEventConnection = {} --// Connection class
--// Class setup
LibCustomEventConstructor.ClassName = "LibCustomEventConstructor"
LibCustomEvent.ClassName = "LibCustomEvent"
LibCustomEventConnection.ClassName = "LibCustomEventConnection"
--// __index
LibCustomEventConstructor.__index = LibCustomEventConstructor
LibCustomEvent.__index = LibCustomEvent
LibCustomEventConnection.__index = LibCustomEventConnection
--// __tostring
function LibCustomEventConstructor:__tostring()
return self.ClassName
end
function LibCustomEvent:__tostring()
return self.ClassName
end
function LibCustomEventConnection:__tostring()
return self.ClassName
end
--// __newindex
LibCustomEventConstructor.__newindex = function()
error("LibCustomEventConstructor: Attempt to modify a readonly table", 2)
end
LibCustomEvent.__newindex = function()
error("LibCustomEvent: Attempt to modify a readonly table", 2)
end
LibCustomEventConnection.__newindex = function()
error("LibCustomEventConnection: Attempt to modify a readonly table", 2)
end
--// CLASS : LibCustomEventConstructor : CONSTRUCTOR \\--
--// function <LibCustomEvent> LibCustomEventConstructor.new()
function LibCustomEventConstructor.new()
return setmetatable({
__LCE_EVENTS = {};
__LCE_DESTROYED = false;
}, LibCustomEvent)
end
--// CLASS : LibCustomEvent : BASE CLASS \\
--// function <LibCustomEventConnection> LibCustomEvent:Connect(func: function)
function LibCustomEvent:Connect(func)
assert(not self.__LCE_DESTROYED, "LibCustomEvent: event is already destroyed")
assert(typeof(func) == "function", [[LibCustomEvent: invalid argument #1 to 'Connect' (function expected, got ]]..typeof(func)..[[)]])
local index = #self.__LCE_EVENTS + 1
local LCEC = setmetatable({
__LCEC_LCEINST = self;
__LCEC_LCEINDEX = index;
}, LibCustomEventConnection)
rawset(self.__LCE_EVENTS, index, { func; LCEC; })
return
end
--// function <void> LibCustomEvent:Fire(...)
function LibCustomEvent:Fire(...)
assert(not self.__LCE_DESTROYED, "LibCustomEvent: event is already destroyed")
for _, functable in next, self.__LCE_EVENTS do
if typeof(functable[1]) ~= "function" then continue end
local ta = {...}
spawn(function()
functable[1](unpack(ta))
end)
end
end
--// function <void> LibCustomEvent:Fire(...)
function LibCustomEvent:FireSync(...)
assert(not self.__LCE_DESTROYED, "LibCustomEvent: event is already destroyed")
for _, functable in next, self.__LCE_EVENTS do
if typeof(functable[1]) ~= "function" then continue end
xpcall(functable[1], function(err)
spawn(function()
error(err, 2)
end)
end, ...)
end
return
end
--// ALIAS OF LibCustomEvent:Fire(...) : function <void> LibCustomEvent:FireAsync(...)
function LibCustomEvent:FireAsync(...)
return self:Fire(...)
end
--// DECONSTRUCTOR : function <void> LibCustomEvent:Destroy()
function LibCustomEvent:Destroy()
for _, ftable in next, self.__LCE_EVENTS do
if typeof(ftable) ~= "table" then continue end
ftable[2]:Disconnect()
end
rawset(self, "__LCE_EVENTS", {})
rawset(self, "__LCE_DESTROYED", true)
return
end
--// CLASS : LibCustomEventConnection : CONNECTION CLASS \\
--// boolean LibCustomEventConnection.Connected
LibCustomEventConnection.Connected = true
--// function <void> LibCustomEventConnection:Disconnect()
function LibCustomEventConnection:Disconnect()
--// Unregister
if self.Connected then
rawset(self.__LCEC_LCEINST.__LCE_EVENTS, self.__LCEC_LCEINDEX, nil)
rawset(self, "Connected", false)
end
--// Cleanup
rawset(self, "__LCEC_LCEINST", nil)
rawset(self, "__LCEC_LCEINDEX", nil)
return
end
--// Return the constructor class
return LibCustomEventConstructor