create DOMClock, abstraction of clock functionalities for DOM

This commit is contained in:
Bruno Windels 2020-04-04 17:34:46 +02:00
parent bc69e49cfb
commit c980f682c6
6 changed files with 59 additions and 9 deletions

View file

@ -12,8 +12,7 @@ export class HomeServerError extends Error {
} }
} }
export class RequestAbortError extends Error { export {AbortError} from "../utils/error.js";
}
export class NetworkError extends Error { export class NetworkError extends Error {
} }

View file

@ -1,5 +1,5 @@
import { import {
RequestAbortError, AbortError,
NetworkError NetworkError
} from "../error.js"; } from "../error.js";
@ -50,7 +50,7 @@ export default function fetchRequest(url, options) {
return {status, body}; return {status, body};
}, err => { }, err => {
if (err.name === "AbortError") { if (err.name === "AbortError") {
throw new RequestAbortError(); throw new AbortError();
} else if (err instanceof TypeError) { } else if (err instanceof TypeError) {
// Network errors are reported as TypeErrors, see // Network errors are reported as TypeErrors, see
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful

View file

@ -1,5 +1,5 @@
import { import {
RequestAbortError, AbortError,
NetworkError NetworkError
} from "../error.js"; } from "../error.js";
@ -23,7 +23,7 @@ class RequestLogItem {
handleError(err) { handleError(err) {
this.end = performance.now(); this.end = performance.now();
this.error = { this.error = {
aborted: err instanceof RequestAbortError, aborted: err instanceof AbortError,
network: err instanceof NetworkError, network: err instanceof NetworkError,
message: err.message, message: err.message,
}; };
@ -96,7 +96,7 @@ class ReplayRequestResult {
if (this._item.error || this._aborted) { if (this._item.error || this._aborted) {
const error = this._item.error; const error = this._item.error;
if (error.aborted || this._aborted) { if (error.aborted || this._aborted) {
throw new RequestAbortError(error.message); throw new AbortError(error.message);
} else if (error.network) { } else if (error.network) {
throw new NetworkError(error.message); throw new NetworkError(error.message);
} else { } else {

View file

@ -1,4 +1,4 @@
import {RequestAbortError} from "./error.js"; import {AbortError} from "./error.js";
import EventEmitter from "../EventEmitter.js"; import EventEmitter from "../EventEmitter.js";
const INCREMENTAL_TIMEOUT = 30000; const INCREMENTAL_TIMEOUT = 30000;
@ -57,7 +57,7 @@ export default class Sync extends EventEmitter {
syncToken = await this._syncRequest(syncToken, INCREMENTAL_TIMEOUT); syncToken = await this._syncRequest(syncToken, INCREMENTAL_TIMEOUT);
} catch (err) { } catch (err) {
this._isSyncing = false; this._isSyncing = false;
if (!(err instanceof RequestAbortError)) { if (!(err instanceof AbortError)) {
console.error("stopping sync because of error"); console.error("stopping sync because of error");
console.error(err); console.error(err);
this.emit("status", "error", err); this.emit("status", "error", err);

49
src/utils/DOMClock.js Normal file
View file

@ -0,0 +1,49 @@
import {AbortError} from "./error.js";
class DOMTimeout {
constructor(ms) {
this._reject = null;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
setTimeout(() => {
this._reject = null;
resolve();
}, ms);
});
}
get elapsed() {
return this._promise;
}
abort() {
if (this._reject) {
this._reject(new AbortError());
this._reject = null;
}
}
}
class DOMTimeMeasure {
constructor() {
this._start = window.performance.now();
}
measure() {
return window.performance.now() - this._start;
}
}
export class DOMClock {
createMeasure() {
return new DOMTimeMeasure();
}
createTimeout(ms) {
return new DOMTimeout(ms);
}
now() {
return Date.now();
}
}

2
src/utils/error.js Normal file
View file

@ -0,0 +1,2 @@
export class AbortError extends Error {
}