1. -- CLASS FIFO
  2. --
  3. -- Firsts-In-First-Out queue for asynchronous calls
  4. --
  5. -- REQUIREMENT:
  6. -- each asynchronous call added to the queue must have this FIFO instance as callback target
  7. -- and #_finished as callback handler!
  8.  
  9. property pFIFO
  10. property pNextAction
  11. property pCompleteCbHandler
  12. property pCompleteCbTarget
  13.  
  14. --**************************************
  15. -- PUBLIC
  16. --**************************************
  17.  
  18. ----------------------------------------
  19. --
  20. ----------------------------------------
  21. on new (me)
  22.   me._reset()
  23.   return me
  24. end
  25.  
  26. ----------------------------------------
  27. -- Can be used to specify a callback that is called when all calls are finished (i.e. after the last)
  28. ----------------------------------------
  29. on mSetCompleteCallback (me, completeCbHandler, completeCbTarget)
  30.   me.pCompleteCbHandler = completeCbHandler
  31.   if voidP(completeCbTarget) then completeCbTarget=_movie
  32.   me.pCompleteCbTarget = completeCbTarget
  33. end
  34.  
  35. ----------------------------------------
  36. -- Adds a call to the queue
  37. --
  38. -- actionHandler: symbol (required)
  39. -- actionTarget: script/instance/object (optional, default=_movie)
  40. -- actionParams: list (optional) - currently up to 3 parameters supported (can be increased)
  41. ----------------------------------------
  42. on mAddCall (me, actionHandler, actionTarget, actionParams)
  43.   if voidP(actionTarget) then actionTarget = _movie
  44.  
  45.   if not listP(actionParams) then actionParams = []
  46.   repeat with i = count(actionParams)+1 to 3 -- increase if you need more than 3!
  47.     actionParams[i] = VOID
  48.   end repeat
  49.  
  50.   me.pFIFO.add(["handler":actionHandler, "target":actionTarget, "params":actionParams])
  51. end
  52.  
  53. ----------------------------------------
  54. -- starts processing of queue
  55. ----------------------------------------
  56. on mStart (me)
  57.   if count(me.pFIFO) then
  58.     me.pNextAction = me.pFIFO[1]
  59.     if _movie.actorlist.getPos(me)=0 then _movie.actorlist.add(me)
  60.   end if
  61. end
  62.  
  63. ----------------------------------------
  64. -- aborts processing of queue
  65. ----------------------------------------
  66. on mAbort (me)
  67.   if _movie.actorlist.getPos(me) then _movie.actorlist.deleteOne(me)
  68.   me._reset()
  69. end
  70.  
  71.  
  72. --**************************************
  73. -- PRIVATE
  74. --**************************************
  75.  
  76. ----------------------------------------
  77. -- Inits/resets the queue
  78. ----------------------------------------
  79. on _reset (me)
  80.   me.pFIFO = []
  81.   me.pNextAction = VOID
  82.   me.pCompleteCbHandler = VOID
  83. end
  84.  
  85. ----------------------------------------
  86. --
  87. ----------------------------------------
  88. on _finished (me)
  89.   me.pFIFO.deleteAt(1)
  90.   if count(me.pFIFO)>0 then
  91.     me.pNextAction = me.pFIFO[1]
  92.   else
  93.     if _movie.actorlist.getPos(me) then _movie.actorlist.deleteOne(me)
  94.     if not voidP(me.pCompleteCbHandler) then
  95.       call(me.pCompleteCbHandler, me.pCompleteCbTarget)
  96.     end if
  97.   end if
  98. end
  99.  
  100. ----------------------------------------
  101. --
  102. ----------------------------------------
  103. on stepFrame (me)
  104.   if not voidP(pNextAction) then
  105.     tAction = pNextAction.duplicate()
  106.     pNextAction = VOID
  107.     p = tAction["params"]
  108.     call(tAction["handler"], tAction["target"], p[1], p[2], p[3]) -- p[4], p[5] ...
  109.   end if
  110. end
  111.  
  112.  
[raw code]