mirror of
https://github.com/kikito/cron.lua.git
synced 2024-12-19 18:34:20 +00:00
fixed errors. added more specs, refining .tagged
This commit is contained in:
parent
bb0a0d0815
commit
ffcc2b6b11
47
cron.lua
47
cron.lua
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
-- Private functions
|
-- Private functions
|
||||||
|
|
||||||
local entries, tagGroups -- initialized in cron.reset
|
local entries, taggedEntries, scopeCacheRoot -- initialized in cron.reset
|
||||||
local cron = {}
|
local cron = {}
|
||||||
|
|
||||||
local function isCallable(callback)
|
local function isCallable(callback)
|
||||||
@ -70,7 +70,7 @@ local function addTags(...)
|
|||||||
return tags, len
|
return tags, len
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerEntry(scope, entry)
|
local function scopedEntry(scope, entry)
|
||||||
for i=1, scope.len do
|
for i=1, scope.len do
|
||||||
taggedEntries[scope.tags[i]][entry] = entry
|
taggedEntries[scope.tags[i]][entry] = entry
|
||||||
end
|
end
|
||||||
@ -78,7 +78,7 @@ local function registerEntry(scope, entry)
|
|||||||
return entry
|
return entry
|
||||||
end
|
end
|
||||||
|
|
||||||
function tagged_update(scope, dt)
|
local function scopedUpdate(scope, dt)
|
||||||
for i=1, scope.len do
|
for i=1, scope.len do
|
||||||
for _,entry in pairs(taggedEntries[scope.tags[i]]) do
|
for _,entry in pairs(taggedEntries[scope.tags[i]]) do
|
||||||
entry:update(dt)
|
entry:update(dt)
|
||||||
@ -86,7 +86,7 @@ function tagged_update(scope, dt)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function tagged_cancel(scope)
|
local function scopedCancel(scope)
|
||||||
for i=1, scope.len do
|
for i=1, scope.len do
|
||||||
local tag = scope.tags[i]
|
local tag = scope.tags[i]
|
||||||
for _,entry in pairs(taggedEntries[tag]) do
|
for _,entry in pairs(taggedEntries[tag]) do
|
||||||
@ -96,14 +96,37 @@ function tagged_cancel(scope)
|
|||||||
end
|
end
|
||||||
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={} }
|
||||||
|
node = node.children[tag]
|
||||||
|
end
|
||||||
|
node.scope = scope
|
||||||
|
end
|
||||||
|
|
||||||
local function newTaggedScope(tags, len)
|
local function newTaggedScope(tags, len)
|
||||||
local scope = { tags = tags, len = len }
|
local scope = getScopeFromCache(tags, len)
|
||||||
|
if not scope then
|
||||||
|
scope = { tags = tags, len = len }
|
||||||
|
|
||||||
scope.after = function(...) return registerEntry(scope, cron.after(...)) end
|
scope.after = function(...) return scopedEntry(scope, cron.after(...)) end
|
||||||
scope.every = function(...) return registerEntry(scope, cron.every(...)) end
|
scope.every = function(...) return scopedEntry(scope, cron.every(...)) end
|
||||||
scope.update = function(dt) tagged_update(scope, dt) end
|
scope.update = function(dt) scopedUpdate(scope, dt) end
|
||||||
scope.cancel = function(dt) tagged_cancel(scope) end
|
scope.cancel = function(dt) scopedCancel(scope) end
|
||||||
|
|
||||||
|
storeScopeInCache(scope)
|
||||||
|
end
|
||||||
return scope
|
return scope
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -134,13 +157,15 @@ function cron.update(dt)
|
|||||||
for _, entry in pairs(entries) do entry:update(dt) end
|
for _, entry in pairs(entries) do entry:update(dt) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function cron.tagged(...)
|
function cron.tagged(first, ...)
|
||||||
return newTaggedScope(addTags(...))
|
assert(first ~= nil, "cron.tagged requires at least one tag")
|
||||||
|
return newTaggedScope(addTags(first, ...))
|
||||||
end
|
end
|
||||||
|
|
||||||
function cron.reset()
|
function cron.reset()
|
||||||
entries = {}
|
entries = {}
|
||||||
taggedEntries = setmetatable({}, {__mode='k'})
|
taggedEntries = setmetatable({}, {__mode='k'})
|
||||||
|
scopeCacheRoot = { children = {}}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- tagged functions
|
-- tagged functions
|
||||||
|
@ -136,6 +136,10 @@ describe( 'cron', function()
|
|||||||
cron.every(1, count) -- C
|
cron.every(1, count) -- C
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('requires at least one tag', function()
|
||||||
|
assert_error(cron.tagged)
|
||||||
|
end)
|
||||||
|
|
||||||
it('filters update', function()
|
it('filters update', function()
|
||||||
cron.tagged('hello').update(5)
|
cron.tagged('hello').update(5)
|
||||||
assert_equal(counter, 2) -- A + B, but not C
|
assert_equal(counter, 2) -- A + B, but not C
|
||||||
@ -154,5 +158,9 @@ describe( 'cron', function()
|
|||||||
cron.tagged('girl').update(5) -- nothing (D is cancelled)
|
cron.tagged('girl').update(5) -- nothing (D is cancelled)
|
||||||
assert_equal(counter, 4)
|
assert_equal(counter, 4)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('caches scopes', function()
|
||||||
|
assert_equal(cron.tagged('hello', 'world'), cron.tagged('hello', 'world'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user