463e8714dcf865e056707a9a44d44aa44c34526a4689dfcf768a71ab06da002c770328c5bc1a8ca73e8339af4b7d29f9d43d930b812fab914102cf29ddd623 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. exports.parseDelay = parseDelay;
  5. var _feature = require('./../helpers/feature');
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. /**
  8. * @class Interval
  9. * @util
  10. */
  11. var Interval = function () {
  12. _createClass(Interval, null, [{
  13. key: 'create',
  14. value: function create(func, delay) {
  15. return new Interval(func, delay);
  16. }
  17. }]);
  18. function Interval(func, delay) {
  19. var _this = this;
  20. _classCallCheck(this, Interval);
  21. /**
  22. * Animation frame request id.
  23. *
  24. * @type {Number}
  25. */
  26. this.timer = null;
  27. /**
  28. * Function to invoke repeatedly.
  29. *
  30. * @type {Function}
  31. */
  32. this.func = func;
  33. /**
  34. * Number of milliseconds that function should wait before next call.
  35. */
  36. this.delay = parseDelay(delay);
  37. /**
  38. * Flag which indicates if interval object was stopped.
  39. *
  40. * @type {Boolean}
  41. * @default true
  42. */
  43. this.stopped = true;
  44. /**
  45. * Interval time (in milliseconds) of the last callback call.
  46. *
  47. * @private
  48. * @type {Number}
  49. */
  50. this._then = null;
  51. /**
  52. * Bounded function `func`.
  53. *
  54. * @private
  55. * @type {Function}
  56. */
  57. this._callback = function () {
  58. return _this.__callback();
  59. };
  60. }
  61. /**
  62. * Start loop.
  63. *
  64. * @returns {Interval}
  65. */
  66. _createClass(Interval, [{
  67. key: 'start',
  68. value: function start() {
  69. if (this.stopped) {
  70. this._then = Date.now();
  71. this.stopped = false;
  72. this.timer = (0, _feature.requestAnimationFrame)(this._callback);
  73. }
  74. return this;
  75. }
  76. /**
  77. * Stop looping.
  78. *
  79. * @returns {Interval}
  80. */
  81. }, {
  82. key: 'stop',
  83. value: function stop() {
  84. if (!this.stopped) {
  85. this.stopped = true;
  86. (0, _feature.cancelAnimationFrame)(this.timer);
  87. this.timer = null;
  88. }
  89. return this;
  90. }
  91. /**
  92. * Loop callback, fired on every animation frame.
  93. *
  94. * @private
  95. */
  96. }, {
  97. key: '__callback',
  98. value: function __callback() {
  99. this.timer = (0, _feature.requestAnimationFrame)(this._callback);
  100. if (this.delay) {
  101. var now = Date.now();
  102. var elapsed = now - this._then;
  103. if (elapsed > this.delay) {
  104. this._then = now - elapsed % this.delay;
  105. this.func();
  106. }
  107. } else {
  108. this.func();
  109. }
  110. }
  111. }]);
  112. return Interval;
  113. }();
  114. exports.default = Interval;
  115. function parseDelay(delay) {
  116. if (typeof delay === 'string' && /fps$/.test(delay)) {
  117. delay = 1000 / parseInt(delay.replace('fps', '') || 0, 10);
  118. }
  119. return delay;
  120. }