mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
Add simple druid event library
This commit is contained in:
parent
b198e09560
commit
0dd37a03cd
37
druid/event.lua
Normal file
37
druid/event.lua
Normal file
@ -0,0 +1,37 @@
|
||||
--- Lua event small library
|
||||
|
||||
local class = require("druid.system.middleclass")
|
||||
|
||||
local M = class("druid.event")
|
||||
|
||||
|
||||
function M.initialize(self)
|
||||
self._callbacks = {}
|
||||
end
|
||||
|
||||
|
||||
function M.subscribe(self, callback)
|
||||
assert(type(callback) == "function", "Callback should be function")
|
||||
|
||||
table.insert(self._callbacks, callback)
|
||||
end
|
||||
|
||||
|
||||
function M.unsubscribe(self, callback)
|
||||
for i = 1, #self._callbacks do
|
||||
if self._callbacks[i] == callback then
|
||||
table.remove(self._callbacks, i)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M.trigger(self, ...)
|
||||
for i = 1, #self._callbacks do
|
||||
self._callbacks[i](...)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
Loading…
x
Reference in New Issue
Block a user