-- CLASS FIFO
--
-- Firsts-In-First-Out queue for asynchronous calls
--
-- REQUIREMENT:
-- each asynchronous call added to the queue must have this FIFO instance as callback target
-- and #_finished as callback handler!
property pFIFO
property pNextAction
property pCompleteCbHandler
property pCompleteCbTarget
--**************************************
-- PUBLIC
--**************************************
----------------------------------------
--
----------------------------------------
on new (me)
me._reset()
return me
end
----------------------------------------
-- Can be used to specify a callback that is called when all calls are finished (i.e. after the last)
----------------------------------------
on mSetCompleteCallback (me, completeCbHandler, completeCbTarget)
me.pCompleteCbHandler = completeCbHandler
if voidP(completeCbTarget) then completeCbTarget=_movie
me.pCompleteCbTarget = completeCbTarget
end
----------------------------------------
-- Adds a call to the queue
--
-- actionHandler: symbol (required)
-- actionTarget: script/instance/object (optional, default=_movie)
-- actionParams: list (optional) - currently up to 3 parameters supported (can be increased)
----------------------------------------
on mAddCall (me, actionHandler, actionTarget, actionParams)
if voidP(actionTarget) then actionTarget = _movie
if not listP(actionParams) then actionParams = []
repeat with i = count(actionParams)+1 to 3 -- increase if you need more than 3!
actionParams[i] = VOID
end repeat
me.pFIFO.add(["handler":actionHandler, "target":actionTarget, "params":actionParams])
end
----------------------------------------
-- starts processing of queue
----------------------------------------
on mStart (me)
if count(me.pFIFO) then
me.pNextAction = me.pFIFO[1]
if _movie.actorlist.getPos(me)=0 then _movie.actorlist.add(me)
end if
end
----------------------------------------
-- aborts processing of queue
----------------------------------------
on mAbort (me)
if _movie.actorlist.getPos(me) then _movie.actorlist.deleteOne(me)
me._reset()
end
--**************************************
-- PRIVATE
--**************************************
----------------------------------------
-- Inits/resets the queue
----------------------------------------
on _reset (me)
me.pFIFO = []
me.pNextAction = VOID
me.pCompleteCbHandler = VOID
end
----------------------------------------
--
----------------------------------------
on _finished (me)
me.pFIFO.deleteAt(1)
if count(me.pFIFO)>0 then
me.pNextAction = me.pFIFO[1]
else
if _movie.actorlist.getPos(me) then _movie.actorlist.deleteOne(me)
if not voidP(me.pCompleteCbHandler) then
call(me.pCompleteCbHandler, me.pCompleteCbTarget)
end if
end if
end
----------------------------------------
--
----------------------------------------
on stepFrame (me)
if not voidP(pNextAction) then
tAction = pNextAction.duplicate()
pNextAction = VOID
p = tAction["params"]
call(tAction["handler"], tAction["target"], p[1], p[2], p[3]) -- p[4], p[5] ...
end if
end