From f88cbf584d7fd9ffae48b9dd87bc1526d6f87688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Thu, 27 Oct 2011 00:22:52 +0200 Subject: [PATCH] refactor --- beholder.lua | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/beholder.lua b/beholder.lua index 38b45b0..bb01072 100644 --- a/beholder.lua +++ b/beholder.lua @@ -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