summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/GameLab/Delegate.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Data/BuiltIn/Libraries/GameLab/Delegate.lua')
-rw-r--r--Data/BuiltIn/Libraries/GameLab/Delegate.lua38
1 files changed, 38 insertions, 0 deletions
diff --git a/Data/BuiltIn/Libraries/GameLab/Delegate.lua b/Data/BuiltIn/Libraries/GameLab/Delegate.lua
new file mode 100644
index 0000000..baa8e86
--- /dev/null
+++ b/Data/BuiltIn/Libraries/GameLab/Delegate.lua
@@ -0,0 +1,38 @@
+local delegate = {}
+
+delegate.__index = delegate
+
+delegate.Add = function(self, action)
+ table.insert(self.actions, action)
+ return self
+end
+
+delegate.Remove = function(self, action)
+ table.remove(self.actions, action)
+ return self
+end
+
+delegate.Invoke = function(self, ...)
+ for _, f in ipairs(self.actions) do
+ if f then
+ f(...)
+ end
+ end
+end
+
+delegate.__call = function(self, ...)
+ self:Invoke(...)
+end
+
+local mt = {}
+mt.__call = function()
+ local d = {}
+ d.actions = {}
+ setmetatable(d, delegate)
+ return d
+end
+setmetatable(delegate, mt)
+
+GameLab.Delegate = delegate
+
+return delegate \ No newline at end of file