Source: trackers/TrackerTask.js

  1. (function() {
  2. /**
  3. * TrackerTask utility.
  4. * @constructor
  5. * @extends {tracking.EventEmitter}
  6. */
  7. tracking.TrackerTask = function(tracker) {
  8. tracking.TrackerTask.base(this, 'constructor');
  9. if (!tracker) {
  10. throw new Error('Tracker instance not specified.');
  11. }
  12. this.setTracker(tracker);
  13. };
  14. tracking.inherits(tracking.TrackerTask, tracking.EventEmitter);
  15. /**
  16. * Holds the tracker instance managed by this task.
  17. * @type {tracking.Tracker}
  18. * @private
  19. */
  20. tracking.TrackerTask.prototype.tracker_ = null;
  21. /**
  22. * Holds if the tracker task is in running.
  23. * @type {boolean}
  24. * @private
  25. */
  26. tracking.TrackerTask.prototype.running_ = false;
  27. /**
  28. * Gets the tracker instance managed by this task.
  29. * @return {tracking.Tracker}
  30. */
  31. tracking.TrackerTask.prototype.getTracker = function() {
  32. return this.tracker_;
  33. };
  34. /**
  35. * Returns true if the tracker task is in running, false otherwise.
  36. * @return {boolean}
  37. * @private
  38. */
  39. tracking.TrackerTask.prototype.inRunning = function() {
  40. return this.running_;
  41. };
  42. /**
  43. * Sets if the tracker task is in running.
  44. * @param {boolean} running
  45. * @private
  46. */
  47. tracking.TrackerTask.prototype.setRunning = function(running) {
  48. this.running_ = running;
  49. };
  50. /**
  51. * Sets the tracker instance managed by this task.
  52. * @return {tracking.Tracker}
  53. */
  54. tracking.TrackerTask.prototype.setTracker = function(tracker) {
  55. this.tracker_ = tracker;
  56. };
  57. /**
  58. * Emits a `run` event on the tracker task for the implementers to run any
  59. * child action, e.g. `requestAnimationFrame`.
  60. * @return {object} Returns itself, so calls can be chained.
  61. */
  62. tracking.TrackerTask.prototype.run = function() {
  63. var self = this;
  64. if (this.inRunning()) {
  65. return;
  66. }
  67. this.setRunning(true);
  68. this.reemitTrackEvent_ = function(event) {
  69. self.emit('track', event);
  70. };
  71. this.tracker_.on('track', this.reemitTrackEvent_);
  72. this.emit('run');
  73. return this;
  74. };
  75. /**
  76. * Emits a `stop` event on the tracker task for the implementers to stop any
  77. * child action being done, e.g. `requestAnimationFrame`.
  78. * @return {object} Returns itself, so calls can be chained.
  79. */
  80. tracking.TrackerTask.prototype.stop = function() {
  81. if (!this.inRunning()) {
  82. return;
  83. }
  84. this.setRunning(false);
  85. this.emit('stop');
  86. this.tracker_.removeListener('track', this.reemitTrackEvent_);
  87. return this;
  88. };
  89. }());