This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/ui/web/dom/Clock.js
Bruno Windels 001dbefbcf stop using default exports
because it becomes hard to remember where you used them and where not
2020-04-20 21:26:39 +02:00

52 lines
1 KiB
JavaScript

import {AbortError} from "../../../utils/error.js";
class Timeout {
constructor(ms) {
this._reject = null;
this._handle = null;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
this._handle = setTimeout(() => {
this._reject = null;
resolve();
}, ms);
});
}
elapsed() {
return this._promise;
}
abort() {
if (this._reject) {
this._reject(new AbortError());
clearTimeout(this._handle);
this._handle = null;
this._reject = null;
}
}
}
class TimeMeasure {
constructor() {
this._start = window.performance.now();
}
measure() {
return window.performance.now() - this._start;
}
}
export class Clock {
createMeasure() {
return new TimeMeasure();
}
createTimeout(ms) {
return new Timeout(ms);
}
now() {
return Date.now();
}
}