mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-01-26 20:21:40 +01:00
78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
|
import ObservableValue from "../observable/ObservableValue.js";
|
||
|
|
||
|
class Timeout {
|
||
|
constructor(elapsed, ms) {
|
||
|
this._reject = null;
|
||
|
this._handle = null;
|
||
|
const timeoutValue = elapsed.get() + ms;
|
||
|
this._waitHandle = elapsed.waitFor(t => t >= timeoutValue);
|
||
|
}
|
||
|
|
||
|
elapsed() {
|
||
|
return this._waitHandle.promise;
|
||
|
}
|
||
|
|
||
|
abort() {
|
||
|
// will reject with AbortError
|
||
|
this._waitHandle.dispose();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class TimeMeasure {
|
||
|
constructor(elapsed) {
|
||
|
this._elapsed = elapsed;
|
||
|
this._start = elapsed.get();
|
||
|
}
|
||
|
|
||
|
measure() {
|
||
|
return this._elapsed.get() - this._start;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default class Clock {
|
||
|
constructor(baseTimestamp = 0) {
|
||
|
this._baseTimestamp = baseTimestamp;
|
||
|
this._elapsed = new ObservableValue(0);
|
||
|
// should be callable as a function as well as a method
|
||
|
this.createMeasure = this.createMeasure.bind(this);
|
||
|
this.createTimeout = this.createTimeout.bind(this);
|
||
|
this.now = this.now.bind(this);
|
||
|
}
|
||
|
|
||
|
createMeasure() {
|
||
|
return new TimeMeasure(this._elapsed);
|
||
|
}
|
||
|
|
||
|
createTimeout(ms) {
|
||
|
return new Timeout(this._elapsed, ms);
|
||
|
}
|
||
|
|
||
|
now() {
|
||
|
return this._baseTimestamp + this.elapsed;
|
||
|
}
|
||
|
|
||
|
elapse(ms) {
|
||
|
this._elapsed.set(this._elapsed.get() + Math.max(0, ms));
|
||
|
}
|
||
|
|
||
|
get elapsed() {
|
||
|
return this._elapsed.get();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function tests() {
|
||
|
return {
|
||
|
"test timeout": async assert => {
|
||
|
const clock = new Clock();
|
||
|
Promise.resolve().then(() => {
|
||
|
clock.elapse(500);
|
||
|
clock.elapse(500);
|
||
|
}).catch(assert.fail);
|
||
|
const timeout = clock.createTimeout(1000);
|
||
|
const promise = timeout.elapsed();
|
||
|
assert(promise instanceof Promise);
|
||
|
await promise;
|
||
|
}
|
||
|
}
|
||
|
}
|