Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _M = {}
- local listeners = {}
- if not table.deepcopy then
- function table.deepcopy(t)
- if type(t) ~= 'table' then return t end
- local mt = getmetatable(t)
- local res = {}
- for k,v in pairs(t) do
- if type(v) == 'table' then
- v = table.deepcopy(v)
- end
- res[k] = v
- end
- setmetatable(res,mt)
- return res
- end
- end
- function _M.addListener(name, listener)
- if not name or not listener then return end
- if not listeners[name] then listeners[name] = {} end
- table.insert(listeners[name], listener)
- end
- function _M.removeListener(name, listener)
- if not name or not listener or not listeners[name] then return end
- local ind = table.indexOf(listeners[name], listener)
- if ind then table.remove(listeners[name], ind) end
- end
- function _M.dispatch(name, params)
- if not name or not listeners[name] then return end
- local l = listeners[name]
- for i = 1, #l do
- l[i](params and table.deepcopy(params))
- end
- end
- return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement