This commit is contained in:
Enrique García 2011-10-27 00:22:52 +02:00
parent d66b78dd8b
commit f88cbf584d

View File

@ -9,27 +9,51 @@
local beholder = {}
local function findNode(self, event)
return self._nodes[event]
end
local function findNodeById(self, id)
return self._ids[id]
end
local function createNode(self, event)
self._nodes[event] = {}
return self._nodes[event]
end
local function findOrCreateNode(self, event)
return findNode(self, event) or createNode(self, event)
end
local function registerActionInNode(self, node, action)
local id = {}
node[id] = action
self._ids[id] = node
return id
end
local function unregisterActionFromNode(self, node, id)
node[id] = nil
self._ids[id] = nil
end
function beholder:reset()
self._actions = {}
self._nodes = {}
self._ids = {}
end
function beholder:observe(event, action)
local id = {}
self._actions[event] = self._actions[event] or {}
self._actions[event][id] = action
self._ids[id] = event
return id
return registerActionInNode(self, findOrCreateNode(self, event), action)
end
function beholder:stopObserving(id)
local event = self._ids[id]
self._actions[event][id] = nil
self._ids[id]=nil
unregisterActionFromNode(self, findNodeById(self, id), id)
end
function beholder:trigger(event,...)
local actions = self._actions[event] or {}
local actions = self._nodes[event] or {}
for _,action in pairs(actions) do
action(...)
end