From e8e214918778929e001cf3af1e2a400abea8bd59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sun, 29 Jan 2012 14:14:43 +0100 Subject: [PATCH] removed unneeded OO from code --- beholder.lua | 67 ++++++++++++++++++++++----------------------- spec/acceptance.lua | 2 +- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/beholder.lua b/beholder.lua index d7f51a2..c8e6748 100644 --- a/beholder.lua +++ b/beholder.lua @@ -13,33 +13,36 @@ end -- private Node class -local Node = { - _nodesById = setmetatable({}, {__mode="k"}) -} +local nodesById = nil +local root = nil -function Node:new() - local node = { callbacks = {}, children = setmetatable({}, {__mode="k"}) } - return setmetatable( node, { __index = Node } ) +local function newNode() + return { callbacks = {}, children = setmetatable({}, {__mode="k"}) } end -function Node:findById(id) - return self._nodesById[id] +local function initialize() + root = newNode() + nodesById = setmetatable({}, {__mode="k"}) end -function Node:findOrCreateChild(key) - self.children[key] = self.children[key] or Node:new() +local function findNodeById(id) + return nodesById[id] +end + +local function findOrCreateChildNode(self, key) + self.children[key] = self.children[key] or newNode() return self.children[key] end -function Node:findOrCreateDescendant(keys) +local function findOrCreateDescendantNode(self, keys) local node = self for i=1, #keys do - node = node:findOrCreateChild(keys[i]) + node = findOrCreateChildNode(node, keys[i]) end return node end -function Node:invokeCallbacks(params) +local function invokeNodeCallbacks(self, params) local counter = 0 for _,callback in pairs(self.callbacks) do callback(unpack(params)) @@ -48,39 +51,39 @@ function Node:invokeCallbacks(params) return counter end -function Node:invokeAllCallbacksInSubTree(params) - local counter = self:invokeCallbacks(params) +local function invokeAllNodeCallbacksInSubTree(self, params) + local counter = invokeNodeCallbacks(self, params) for _,child in pairs(self.children) do - counter = counter + child:invokeAllCallbacksInSubTree(params) + counter = counter + invokeAllNodeCallbacksInSubTree(child, params) end return counter end -function Node:invokeCallbacksFromPath(path) +local function invokeNodeCallbacksFromPath(self, path) local node = self local params = copy(path) - local counter = node:invokeCallbacks(params) + local counter = invokeNodeCallbacks(node, params) for i=1, #path do node = node.children[path[i]] if not node then break end table.remove(params, 1) - counter = counter + node:invokeCallbacks(params) + counter = counter + invokeNodeCallbacks(node, params) end return counter end -function Node:addCallback(callback) +local function addCallbackToNode(self, callback) local id = {} self.callbacks[id] = callback - Node._nodesById[id] = self + nodesById[id] = self return id end -function Node:removeCallback(id) +local function removeCallbackFromNode(self, id) self.callbacks[id] = nil - Node._nodesById[id] = nil + nodesById[id] = nil end @@ -90,43 +93,39 @@ local beholder = {} -- beholder private functions -local root = nil local function falseIfZero(n) return n > 0 and n end local function extractEventAndCallbackFromParams(params) - assert(#params > 0, "beholder:observe requires at least one parameter - the callback. You usually want to use two, i.e.: beholder:observe('EVENT', callback)") + assert(#params > 0, "beholder.observe requires at least one parameter - the callback. You usually want to use two, i.e.: beholder.observe('EVENT', callback)") local callback = table.remove(params, #params) return params, callback end -local function initialize() - root = Node:new() -end - ------ Public interface function beholder.observe(...) local event, callback = extractEventAndCallbackFromParams({...}) - return root:findOrCreateDescendant(event):addCallback(callback) + local node = findOrCreateDescendantNode(root, event) + return addCallbackToNode(node, callback) end function beholder.stopObserving(id) - local node = Node:findById(id) + local node = findNodeById(id) if not node then return false end - node:removeCallback(id) + removeCallbackFromNode(node, id) return true end function beholder.trigger(...) - return falseIfZero( root:invokeCallbacksFromPath({...}) ) + return falseIfZero( invokeNodeCallbacksFromPath(root, {...}) ) end function beholder.triggerAll(...) - return falseIfZero( root:invokeAllCallbacksInSubTree({...}) ) + return falseIfZero( invokeAllNodeCallbacksInSubTree(root, {...}) ) end function beholder.reset() diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 585769a..bd74003 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -7,7 +7,7 @@ describe("Acceptance", function() beholder.reset() end) - test("Normal behavior", function() + test("Basic behavior", function() local counter = 0