/home/lindsay/xeolabs/xeogl-next/xeogl/src/tasks.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/src/tasks.js

  1. import {Queue} from './utils/queue.js';
  2.  
  3. const taskQueue = new Queue(); // Task queue, which is pumped on each frame; tasks are pushed to it with calls to xeogl.schedule
  4.  
  5. const tasks = {
  6.  
  7. /**
  8. Schedule a task for xeogl to run at the next frame.
  9.  
  10. Internally, this pushes the task to a FIFO queue. Within each frame interval, xeogl processes the queue
  11. for a certain period of time, popping tasks and running them. After each frame interval, tasks that did not
  12. get a chance to run during the task are left in the queue to be run next time.
  13.  
  14. @method scheduleTask
  15. @param {Function} callback Callback that runs the task.
  16. @param {Object} [scope] Scope for the callback.
  17. */
  18. scheduleTask(callback, scope) {
  19. taskQueue.push(callback);
  20. taskQueue.push(scope);
  21. },
  22.  
  23. runTasks(until) { // Pops and processes tasks in the queue, until the given number of milliseconds has elapsed.
  24. let time = (new Date()).getTime();
  25. let callback;
  26. let scope;
  27. let tasksRun = 0;
  28. while (taskQueue.length > 0 && time < until) {
  29. callback = taskQueue.shift();
  30. scope = taskQueue.shift();
  31. if (scope) {
  32. callback.call(scope);
  33. } else {
  34. callback();
  35. }
  36. time = (new Date()).getTime();
  37. tasksRun++;
  38. }
  39. return tasksRun;
  40. },
  41.  
  42. getNumTasks() {
  43. return taskQueue.length;
  44. }
  45. };
  46.  
  47. export {tasks};