removed unneeded OO from code

This commit is contained in:
Enrique García Cota 2012-01-29 14:14:43 +01:00
parent 0b03e09803
commit e8e2149187
2 changed files with 34 additions and 35 deletions

View File

@ -13,33 +13,36 @@ end
-- private Node class -- private Node class
local Node = { local nodesById = nil
_nodesById = setmetatable({}, {__mode="k"}) local root = nil
}
function Node:new() local function newNode()
local node = { callbacks = {}, children = setmetatable({}, {__mode="k"}) } return { callbacks = {}, children = setmetatable({}, {__mode="k"}) }
return setmetatable( node, { __index = Node } )
end end
function Node:findById(id) local function initialize()
return self._nodesById[id] root = newNode()
nodesById = setmetatable({}, {__mode="k"})
end end
function Node:findOrCreateChild(key) local function findNodeById(id)
self.children[key] = self.children[key] or Node:new() return nodesById[id]
end
local function findOrCreateChildNode(self, key)
self.children[key] = self.children[key] or newNode()
return self.children[key] return self.children[key]
end end
function Node:findOrCreateDescendant(keys) local function findOrCreateDescendantNode(self, keys)
local node = self local node = self
for i=1, #keys do for i=1, #keys do
node = node:findOrCreateChild(keys[i]) node = findOrCreateChildNode(node, keys[i])
end end
return node return node
end end
function Node:invokeCallbacks(params) local function invokeNodeCallbacks(self, params)
local counter = 0 local counter = 0
for _,callback in pairs(self.callbacks) do for _,callback in pairs(self.callbacks) do
callback(unpack(params)) callback(unpack(params))
@ -48,39 +51,39 @@ function Node:invokeCallbacks(params)
return counter return counter
end end
function Node:invokeAllCallbacksInSubTree(params) local function invokeAllNodeCallbacksInSubTree(self, params)
local counter = self:invokeCallbacks(params) local counter = invokeNodeCallbacks(self, params)
for _,child in pairs(self.children) do for _,child in pairs(self.children) do
counter = counter + child:invokeAllCallbacksInSubTree(params) counter = counter + invokeAllNodeCallbacksInSubTree(child, params)
end end
return counter return counter
end end
function Node:invokeCallbacksFromPath(path) local function invokeNodeCallbacksFromPath(self, path)
local node = self local node = self
local params = copy(path) local params = copy(path)
local counter = node:invokeCallbacks(params) local counter = invokeNodeCallbacks(node, params)
for i=1, #path do for i=1, #path do
node = node.children[path[i]] node = node.children[path[i]]
if not node then break end if not node then break end
table.remove(params, 1) table.remove(params, 1)
counter = counter + node:invokeCallbacks(params) counter = counter + invokeNodeCallbacks(node, params)
end end
return counter return counter
end end
function Node:addCallback(callback) local function addCallbackToNode(self, callback)
local id = {} local id = {}
self.callbacks[id] = callback self.callbacks[id] = callback
Node._nodesById[id] = self nodesById[id] = self
return id return id
end end
function Node:removeCallback(id) local function removeCallbackFromNode(self, id)
self.callbacks[id] = nil self.callbacks[id] = nil
Node._nodesById[id] = nil nodesById[id] = nil
end end
@ -90,43 +93,39 @@ local beholder = {}
-- beholder private functions -- beholder private functions
local root = nil
local function falseIfZero(n) local function falseIfZero(n)
return n > 0 and n return n > 0 and n
end end
local function extractEventAndCallbackFromParams(params) 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) local callback = table.remove(params, #params)
return params, callback return params, callback
end end
local function initialize()
root = Node:new()
end
------ Public interface ------ Public interface
function beholder.observe(...) function beholder.observe(...)
local event, callback = extractEventAndCallbackFromParams({...}) local event, callback = extractEventAndCallbackFromParams({...})
return root:findOrCreateDescendant(event):addCallback(callback) local node = findOrCreateDescendantNode(root, event)
return addCallbackToNode(node, callback)
end end
function beholder.stopObserving(id) function beholder.stopObserving(id)
local node = Node:findById(id) local node = findNodeById(id)
if not node then return false end if not node then return false end
node:removeCallback(id) removeCallbackFromNode(node, id)
return true return true
end end
function beholder.trigger(...) function beholder.trigger(...)
return falseIfZero( root:invokeCallbacksFromPath({...}) ) return falseIfZero( invokeNodeCallbacksFromPath(root, {...}) )
end end
function beholder.triggerAll(...) function beholder.triggerAll(...)
return falseIfZero( root:invokeAllCallbacksInSubTree({...}) ) return falseIfZero( invokeAllNodeCallbacksInSubTree(root, {...}) )
end end
function beholder.reset() function beholder.reset()

View File

@ -7,7 +7,7 @@ describe("Acceptance", function()
beholder.reset() beholder.reset()
end) end)
test("Normal behavior", function() test("Basic behavior", function()
local counter = 0 local counter = 0