Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Timer.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const utils_1 = require("@wdio/utils");
  4. const TIMEOUT_ERROR = 'timeout';
  5. const NOOP = () => { };
  6. class Timer {
  7. constructor(_delay, _timeout, _fn, _leading = false) {
  8. this._delay = _delay;
  9. this._timeout = _timeout;
  10. this._fn = _fn;
  11. this._leading = _leading;
  12. this._conditionExecutedCnt = 0;
  13. this._resolve = NOOP;
  14. this._reject = NOOP;
  15. this._ticks = 0;
  16. if (utils_1.hasWdioSyncSupport && !_fn.name.includes('async') && Boolean(global.browser)) {
  17. this._fn = () => utils_1.runFnInFiberContext(_fn)();
  18. }
  19. const retPromise = new Promise((resolve, reject) => {
  20. this._resolve = resolve;
  21. this._reject = reject;
  22. });
  23. this._start();
  24. return retPromise;
  25. }
  26. _start() {
  27. this._startTime = Date.now();
  28. emitTimerEvent({ id: this._startTime, start: true });
  29. if (this._leading) {
  30. this._tick();
  31. }
  32. else {
  33. this._timeoutId = setTimeout(this._tick.bind(this), this._delay);
  34. }
  35. this._mainTimeoutId = setTimeout(() => {
  36. if (!this._wasConditionExecuted()) {
  37. return;
  38. }
  39. emitTimerEvent({ id: this._startTime, timeout: true });
  40. const reason = this._lastError || new Error(TIMEOUT_ERROR);
  41. this._reject(reason);
  42. this._stop();
  43. }, this._timeout);
  44. }
  45. _stop() {
  46. if (this._timeoutId) {
  47. clearTimeout(this._timeoutId);
  48. }
  49. delete this._timeoutId;
  50. }
  51. _stopMain() {
  52. emitTimerEvent({ id: this._startTime });
  53. if (this._mainTimeoutId) {
  54. clearTimeout(this._mainTimeoutId);
  55. }
  56. }
  57. _tick() {
  58. const result = this._fn();
  59. if (typeof result.then !== 'function') {
  60. if (!result) {
  61. return this._checkCondition(new Error('return value was never truthy'));
  62. }
  63. return this._checkCondition(undefined, result);
  64. }
  65. result.then((res) => this._checkCondition(undefined, res), (err) => this._checkCondition(err));
  66. }
  67. _checkCondition(err, res) {
  68. ++this._conditionExecutedCnt;
  69. this._lastError = err;
  70. if (res) {
  71. this._resolve(res);
  72. this._stop();
  73. this._stopMain();
  74. return;
  75. }
  76. let diff = (Date.now() - (this._startTime || 0)) - (this._ticks++ * this._delay);
  77. let delay = Math.max(0, this._delay - diff);
  78. this._stop();
  79. if (this._hasTime(delay)) {
  80. this._timeoutId = setTimeout(this._tick.bind(this), delay);
  81. }
  82. else {
  83. this._stopMain();
  84. const reason = this._lastError || new Error(TIMEOUT_ERROR);
  85. this._reject(reason);
  86. }
  87. }
  88. _hasTime(delay) {
  89. return (Date.now() - (this._startTime || 0) + delay) <= this._timeout;
  90. }
  91. _wasConditionExecuted() {
  92. return this._conditionExecutedCnt > 0;
  93. }
  94. }
  95. function emitTimerEvent(payload) {
  96. if (utils_1.hasWdioSyncSupport) {
  97. process.emit('WDIO_TIMER', payload);
  98. }
  99. }
  100. exports.default = Timer;