beholder.lua/beholder.lua

106 lines
3.5 KiB
Lua
Raw Normal View History

-- beholder.lua - v1.0 (2011-11)
-- Copyright (c) 2011 Enrique García Cota
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN callback OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2011-10-25 22:18:17 +00:00
local beholder = {}
local function copy(t)
local c={}
for i=1,#t do c[i]=t[i] end
return c
end
2011-10-26 22:22:52 +00:00
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)")
local callback = table.remove(params, #params)
return params, callback
2011-10-26 22:22:52 +00:00
end
local function findNodeById(self, id)
return self._nodesById[id]
end
local function findOrCreateChildNode(node, key)
node.children[key] = node.children[key] or { callbacks = {}, children = {} }
return node.children[key]
end
local function findOrCreateDescendantNode(node, keys)
for i=1, #keys do
node = findOrCreateChildNode(node, keys[i])
end
return node
2011-10-26 22:22:52 +00:00
end
local function executeNodeCallbacks(node, params)
local counter = 0
2011-10-30 11:33:34 +00:00
params = params or {}
for _,callback in pairs(node.callbacks) do
callback(unpack(params))
2011-10-30 11:33:34 +00:00
counter = counter + 1
end
return counter
end
local function executeAllCallbacks(node)
local counter = executeNodeCallbacks(node)
2011-10-30 11:33:34 +00:00
for _,child in pairs(node.children) do
counter = counter + executeAllCallbacks(child)
2011-10-30 11:33:34 +00:00
end
return counter
end
local function executeEventCallbacks(node, event)
local params = copy(event)
local counter = executeNodeCallbacks(node, params)
2011-10-30 11:33:34 +00:00
for i=1, #event do
node = node.children[event[i]]
if not node then break end
table.remove(params, 1)
counter = counter + executeNodeCallbacks(node, params)
end
2011-10-30 11:33:34 +00:00
return counter
2011-10-26 22:22:52 +00:00
end
local function addCallbackToNode(self, node, callback)
2011-10-26 22:22:52 +00:00
local id = {}
node.callbacks[id] = callback
self._nodesById[id] = node
2011-10-26 22:22:52 +00:00
return id
end
local function removeCallbackFromNode(node, id)
if not node then return false end
node.callbacks[id] = nil
return true
end
function beholder:reset()
self._root = { callbacks={}, children={} }
self._nodesById = setmetatable({}, {__mode="k"})
end
function beholder:observe(...)
local event, callback = extractEventAndCallbackFromParams({...})
return addCallbackToNode(self, findOrCreateDescendantNode(self._root, event), callback)
end
function beholder:stopObserving(id)
return removeCallbackFromNode(findNodeById(self, id), id)
end
function beholder:trigger(...)
2011-10-30 11:33:34 +00:00
local event = {...}
local counter = (#event == 0) and executeAllCallbacks(self._root) or executeEventCallbacks(self._root, event)
2011-10-30 11:33:34 +00:00
return counter > 0 and counter
end
2011-10-25 22:18:17 +00:00
beholder:reset()
return beholder