From c980f682c64bbcbc153bce7aac0b7c87bcda20af Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sat, 4 Apr 2020 17:34:46 +0200 Subject: [PATCH] create DOMClock, abstraction of clock functionalities for DOM --- src/matrix/error.js | 3 +-- src/matrix/net/fetch.js | 4 ++-- src/matrix/net/replay.js | 6 ++--- src/matrix/sync.js | 4 ++-- src/utils/DOMClock.js | 49 ++++++++++++++++++++++++++++++++++++++++ src/utils/error.js | 2 ++ 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 src/utils/DOMClock.js create mode 100644 src/utils/error.js diff --git a/src/matrix/error.js b/src/matrix/error.js index cf6db9c5..42dcac73 100644 --- a/src/matrix/error.js +++ b/src/matrix/error.js @@ -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 { } diff --git a/src/matrix/net/fetch.js b/src/matrix/net/fetch.js index 48a13969..36713475 100644 --- a/src/matrix/net/fetch.js +++ b/src/matrix/net/fetch.js @@ -1,5 +1,5 @@ import { - RequestAbortError, + AbortError, NetworkError } from "../error.js"; @@ -50,7 +50,7 @@ export default function fetchRequest(url, options) { return {status, body}; }, err => { if (err.name === "AbortError") { - throw new RequestAbortError(); + throw new AbortError(); } else if (err instanceof TypeError) { // 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 diff --git a/src/matrix/net/replay.js b/src/matrix/net/replay.js index b6ddcb74..f47e6d47 100644 --- a/src/matrix/net/replay.js +++ b/src/matrix/net/replay.js @@ -1,5 +1,5 @@ import { - RequestAbortError, + AbortError, NetworkError } from "../error.js"; @@ -23,7 +23,7 @@ class RequestLogItem { handleError(err) { this.end = performance.now(); this.error = { - aborted: err instanceof RequestAbortError, + aborted: err instanceof AbortError, network: err instanceof NetworkError, message: err.message, }; @@ -96,7 +96,7 @@ class ReplayRequestResult { if (this._item.error || this._aborted) { const error = this._item.error; if (error.aborted || this._aborted) { - throw new RequestAbortError(error.message); + throw new AbortError(error.message); } else if (error.network) { throw new NetworkError(error.message); } else { diff --git a/src/matrix/sync.js b/src/matrix/sync.js index fb31e422..bc5f5797 100644 --- a/src/matrix/sync.js +++ b/src/matrix/sync.js @@ -1,4 +1,4 @@ -import {RequestAbortError} from "./error.js"; +import {AbortError} from "./error.js"; import EventEmitter from "../EventEmitter.js"; const INCREMENTAL_TIMEOUT = 30000; @@ -57,7 +57,7 @@ export default class Sync extends EventEmitter { syncToken = await this._syncRequest(syncToken, INCREMENTAL_TIMEOUT); } catch (err) { this._isSyncing = false; - if (!(err instanceof RequestAbortError)) { + if (!(err instanceof AbortError)) { console.error("stopping sync because of error"); console.error(err); this.emit("status", "error", err); diff --git a/src/utils/DOMClock.js b/src/utils/DOMClock.js new file mode 100644 index 00000000..eeb5ebcc --- /dev/null +++ b/src/utils/DOMClock.js @@ -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(); + } +} diff --git a/src/utils/error.js b/src/utils/error.js new file mode 100644 index 00000000..f10f7590 --- /dev/null +++ b/src/utils/error.js @@ -0,0 +1,2 @@ +export class AbortError extends Error { +} \ No newline at end of file