JobExecutor.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @description job executor for animation.
  3. * @author <a href="mailto:lingosurf168@gmail.com">Lingo Surf168</a>
  4. *
  5. * @class
  6. * @param {Replay} replay replay
  7. */
  8. JobExecutor = function(replay) {
  9. this.replay = replay;
  10. this.running = false;
  11. };
  12. JobExecutor.prototype = {
  13. /**
  14. * @description start running.
  15. */
  16. start: function() {
  17. if (this.running !== true) {
  18. this.running = true;
  19. this.tid = new Date().getTime();
  20. this.run(this.tid);
  21. }
  22. },
  23. /**
  24. * @description running.
  25. * @param tid transactionId use to avoid the speed up when multi replays
  26. */
  27. run: function(tid) {
  28. if (this.running !== true) {
  29. return;
  30. }
  31. if (tid != this.tid) {
  32. return;
  33. }
  34. var count = 0;
  35. var tokens = Array.prototype.slice.call(this.replay.tokens, 0);
  36. for (var i = 0; i < tokens.length; i++) {
  37. var token = tokens[i];
  38. if (token.status === 'running') {
  39. count++;
  40. token.move();
  41. }
  42. }
  43. if (count !== 0) {
  44. var self = this;
  45. setTimeout(function() {
  46. self.run(tid);
  47. }, 100);
  48. } else {
  49. this.running = false;
  50. var tokens = [];
  51. for (var i = 0; i < this.replay.tokens.length; i++) {
  52. var token = this.replay.tokens[i];
  53. if (token.status !== 'removed') {
  54. tokens.push(token);
  55. }
  56. }
  57. this.replay.tokens = tokens;
  58. }
  59. }
  60. };