removed tags

This commit is contained in:
kikito 2013-09-25 15:58:06 +02:00
parent 3e1d11a95e
commit 824b52478d
3 changed files with 1 additions and 148 deletions

View File

@ -13,7 +13,6 @@ API
* `cron.cancel(id)` will stop a timed action from happening, and will interrupt the periodical execution of a periodic action.
* `cron.reset()` removes all timed and periodic actions, and resets the time passed back to 0.
* `cron.update(dt)` is needed to be executed on the main program loop. `dt` is the amount of time that has passed since the last iteration. When `cron.update` is executed, cron will check the list of pending actions and execute them if needed.
* `cron.tagged(tag1, tag2, tag3 ...)` filters other cron methods (`cron.after`, `cron.every`, `cron.update` & `cron.cancel` but not `cron.reset`) so that they create clocks with specific tags/ act on clocks with specific tags.
Examples
========
@ -41,35 +40,6 @@ Examples
cron.reset() -- stops all the current actions, both timed ones and periodical ones.
Examples using tags:
-- This has the same effect of cron.after(2, showMenu), except that the
-- time entry is tagged with the tags 'main-menu' and 'menu'
cron.tagged('main-menu', 'menu').every(2, showMenu)
-- after also has a tagged version:
cron.tagged('menu').after(10, doSomething)
-- This updates the time entries tagged with the tag 'menu', but not the rest
cron.tagged('menu').update(2)
-- cron.update updates all the time entries, no matter how they are tagged:
cron.update(2) -- updates everything
-- the tagged version of cron.cancel does not take params:
cron.tagged('main-menu').cancel() -- cancels any entry tagged 'main-menu'
-- A very nice thing: You are not restrited to using strings for tags. Any Lua
-- object (including tables) can be used. This way, you can link time entries
-- to specific instances.
local player = ... -- some table representing the player
cron.tagged(player, 'movement').after(10, startBoredAnimation)
...
cron.tagged(player).cancel() -- cancel all time entries tagged with the player
cron.tagged(player, 'movement') -- cancell the player entries related with movement only
Gotchas / Warnings
==================

View File

@ -28,8 +28,7 @@ local cron = {
-- Private functions
local entries, taggedEntries, scopeCacheRoot -- initialized in cron.reset
local cron = {}
local entries -- initialized in cron.reset
local function isCallable(callback)
local tc = type(callback)
@ -75,85 +74,10 @@ local function updatePeriodicEntry(self, dt)
end
end
local function addTags(...)
local tags = {...}
local len = #tags
for i=1, len do
local tag = tags[i]
taggedEntries[tag] = taggedEntries[tag] or setmetatable({}, {__mode = 'k'})
end
return tags, len
end
local function scopedEntry(scope, entry)
for i=1, scope.len do
taggedEntries[scope.tags[i]][entry] = entry
end
entry.tags = scope.tags
return entry
end
local function scopedUpdate(scope, dt)
for i=1, scope.len do
for _,entry in pairs(taggedEntries[scope.tags[i]]) do
entry:update(dt)
end
end
end
local function scopedCancel(scope)
for i=1, scope.len do
local tag = scope.tags[i]
for _,entry in pairs(taggedEntries[tag]) do
cron.cancel(entry)
end
taggedEntries[tag] = nil
end
end
local function getScopeFromCache(tags, len)
local node = scopeCacheRoot
for i=1, len do
node = node.children[tags[i]]
if not node then return nil end
end
return node.scope
end
local function storeScopeInCache(scope)
local node = scopeCacheRoot
for i=1, scope.len do
local tag = scope.tags[i]
node.children[tag] = node.children[tag] or { children=setmetatable({},{__mode='k'}) }
node = node.children[tag]
end
node.scope = scope
end
local function newTaggedScope(tags, len)
local scope = getScopeFromCache(tags, len)
if not scope then
scope = { tags = tags, len = len }
scope.after = function(...) return scopedEntry(scope, cron.after(...)) end
scope.every = function(...) return scopedEntry(scope, cron.every(...)) end
scope.update = function(dt) scopedUpdate(scope, dt) end
scope.cancel = function(dt) scopedCancel(scope) end
storeScopeInCache(scope)
end
return scope
end
-- Public functions
function cron.cancel(id)
entries[id] = nil
if id.tags then
for i=1, #id.tags do
taggedEntries[id.tags[i]][id] = nil
end
end
end
function cron.after(time, callback, ...)
@ -172,20 +96,10 @@ function cron.update(dt)
for _, entry in pairs(entries) do entry:update(dt) end
end
function cron.tagged(first, ...)
assert(first ~= nil, "cron.tagged requires at least one tag")
return newTaggedScope(addTags(first, ...))
end
function cron.reset()
entries = {}
taggedEntries = setmetatable({}, {__mode='k'})
scopeCacheRoot = { children = setmetatable({}, {__mode='k'}) }
end
-- tagged functions
cron.reset()
return cron

View File

@ -129,35 +129,4 @@ describe( 'cron', function()
end)
end)
describe('.tagged', function()
before_each(function()
cron.tagged('hello').every(5, count) -- A
cron.tagged('hello').after(2, count) -- B
cron.every(1, count) -- C
end)
it('requires at least one tag', function()
assert.error(cron.tagged)
end)
it('filters update', function()
cron.tagged('hello').update(5)
assert.equal(counter, 2) -- A + B, but not C
end)
it('filters cancel', function()
cron.tagged('hello', 'girl').every(5, count) -- D
cron.tagged('hello').update(5) -- A + B + D - C
assert.equal(counter, 3)
cron.tagged('girl').cancel()
cron.tagged('hello').update(5) -- A + B - C
assert.equal(counter, 4)
cron.tagged('girl').update(5) -- nothing (D is cancelled)
assert.equal(counter, 4)
end)
end)
end)