mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
re-arranged function orders
This commit is contained in:
parent
46377b7ca3
commit
c5e741e884
57
beholder.lua
57
beholder.lua
@ -5,17 +5,6 @@
|
|||||||
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
-- 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.
|
-- 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.
|
||||||
|
|
||||||
local beholder = {}
|
|
||||||
|
|
||||||
local function initialize(self)
|
|
||||||
self._root = { callbacks={}, children={} }
|
|
||||||
self._nodesById = setmetatable({}, {__mode="k"})
|
|
||||||
end
|
|
||||||
|
|
||||||
local function checkSelf(self, methodName)
|
|
||||||
assert(type(self)=="table" and self._root and self._nodesById, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function falseIfZero(n)
|
local function falseIfZero(n)
|
||||||
return n > 0 and n
|
return n > 0 and n
|
||||||
end
|
end
|
||||||
@ -26,16 +15,7 @@ local function copy(t)
|
|||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
local function extractEventAndCallbackFromParams(params)
|
-- private node-exclusive functions
|
||||||
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 findNodeById(self, id)
|
|
||||||
return self._nodesById[id]
|
|
||||||
end
|
|
||||||
|
|
||||||
local function findOrCreateChildNode(node, key)
|
local function findOrCreateChildNode(node, key)
|
||||||
node.children[key] = node.children[key] or { callbacks = {}, children = {} }
|
node.children[key] = node.children[key] or { callbacks = {}, children = {} }
|
||||||
return node.children[key]
|
return node.children[key]
|
||||||
@ -79,6 +59,33 @@ local function executeEventCallbacks(node, event)
|
|||||||
return counter
|
return counter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function removeCallbackFromNode(node, id)
|
||||||
|
if not node then return false end
|
||||||
|
node.callbacks[id] = nil
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
-- beholder private functions
|
||||||
|
|
||||||
|
local function checkSelf(self, methodName)
|
||||||
|
assert(type(self)=="table" and self._root and self._nodesById, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
|
||||||
|
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)")
|
||||||
|
local callback = table.remove(params, #params)
|
||||||
|
return params, callback
|
||||||
|
end
|
||||||
|
|
||||||
|
local function initialize(self)
|
||||||
|
self._root = { callbacks={}, children={} }
|
||||||
|
self._nodesById = setmetatable({}, {__mode="k"})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function findNodeById(self, id)
|
||||||
|
return self._nodesById[id]
|
||||||
|
end
|
||||||
|
|
||||||
local function addCallbackToNode(self, node, callback)
|
local function addCallbackToNode(self, node, callback)
|
||||||
local id = {}
|
local id = {}
|
||||||
node.callbacks[id] = callback
|
node.callbacks[id] = callback
|
||||||
@ -86,13 +93,9 @@ local function addCallbackToNode(self, node, callback)
|
|||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
local function removeCallbackFromNode(node, id)
|
------ Public interface
|
||||||
if not node then return false end
|
|
||||||
node.callbacks[id] = nil
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
-------
|
local beholder = {}
|
||||||
|
|
||||||
function beholder:observe(...)
|
function beholder:observe(...)
|
||||||
checkSelf(self, 'observe')
|
checkSelf(self, 'observe')
|
||||||
|
Loading…
Reference in New Issue
Block a user