Convert replay.js to ts

This commit is contained in:
RMidhunSuresh 2021-11-24 12:59:27 +05:30
parent 2e6b909173
commit 238b9aafb1
2 changed files with 40 additions and 24 deletions

View file

@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// import {RecordRequester, ReplayRequester} from "./matrix/net/request/replay.js"; // import {RecordRequester, ReplayRequester} from "./matrix/net/request/replay";
import {SessionContainer} from "./matrix/SessionContainer.js"; import {SessionContainer} from "./matrix/SessionContainer.js";
import {RootViewModel} from "./domain/RootViewModel.js"; import {RootViewModel} from "./domain/RootViewModel.js";
import {createNavigation, createRouter} from "./domain/navigation/index.js"; import {createNavigation, createRouter} from "./domain/navigation/index.js";

View file

@ -14,29 +14,36 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { import {AbortError, ConnectionError} from "../../error.js";
AbortError, import type {IRequestOptions, RequestFunction} from "../../../platform/types/types.js";
ConnectionError import type {RequestResult} from "../../../platform/web/dom/request/fetch.js";
} from "../../error.js";
class RequestLogItem { interface IOptions extends IRequestOptions {
constructor(url, options) { method?: any;
this.url = url; delay?: boolean;
this.options = options;
this.error = null;
this.body = null;
this.status = status;
this.start = performance.now();
this.end = 0;
} }
async handleResponse(response) { class RequestLogItem {
public readonly url: string;
public readonly options: IOptions;
public error: {aborted: boolean, network: boolean, message: string};
public status: number;
public body: Response["body"];
public start: number = performance.now();
public end: number = 0;
constructor(url: string, options: IOptions) {
this.url = url;
this.options = options;
}
async handleResponse(response: Response) {
this.end = performance.now(); this.end = performance.now();
this.status = response.status; this.status = response.status;
this.body = response.body; this.body = response.body;
} }
handleError(err) { handleError(err: Error): void {
this.end = performance.now(); this.end = performance.now();
this.error = { this.error = {
aborted: err instanceof AbortError, aborted: err instanceof AbortError,
@ -47,13 +54,15 @@ class RequestLogItem {
} }
export class RecordRequester { export class RecordRequester {
constructor(request) { private readonly _origRequest: RequestFunction;
private readonly _requestLog: RequestLogItem[] = [];
constructor(request: RequestFunction) {
this._origRequest = request; this._origRequest = request;
this._requestLog = [];
this.request = this.request.bind(this); this.request = this.request.bind(this);
} }
request(url, options) { request(url: string, options: IOptions): RequestResult {
const requestItem = new RequestLogItem(url, options); const requestItem = new RequestLogItem(url, options);
this._requestLog.push(requestItem); this._requestLog.push(requestItem);
try { try {
@ -68,24 +77,27 @@ export class RecordRequester {
} }
} }
log() { log(): RequestLogItem[] {
return this._requestLog; return this._requestLog;
} }
} }
export class ReplayRequester { export class ReplayRequester {
constructor(log, options) { private readonly _log: RequestLogItem[];
private readonly _options: IOptions;
constructor(log: RequestLogItem[], options: IOptions) {
this._log = log.slice(); this._log = log.slice();
this._options = options; this._options = options;
this.request = this.request.bind(this); this.request = this.request.bind(this);
} }
request(url, options) { request(url: string, options: IOptions): ReplayRequestResult {
const idx = this._log.findIndex(item => { const idx = this._log.findIndex((item) => {
return item.url === url && options.method === item.options.method; return item.url === url && options.method === item.options.method;
}); });
if (idx === -1) { if (idx === -1) {
return new ReplayRequestResult({status: 404}, options); return new ReplayRequestResult({ status: 404 } as RequestLogItem, options);
} else { } else {
const [item] = this._log.splice(idx, 1); const [item] = this._log.splice(idx, 1);
return new ReplayRequestResult(item, options); return new ReplayRequestResult(item, options);
@ -94,17 +106,21 @@ export class ReplayRequester {
} }
class ReplayRequestResult { class ReplayRequestResult {
constructor(item, options) { private readonly _item: RequestLogItem;
private readonly _options: IOptions;
private _aborted: boolean;
constructor(item: RequestLogItem, options: IOptions) {
this._item = item; this._item = item;
this._options = options; this._options = options;
this._aborted = false; this._aborted = false;
} }
abort() { abort(): void {
this._aborted = true; this._aborted = true;
} }
async response() { async response(): Promise<RequestLogItem> {
if (this._options.delay) { if (this._options.delay) {
const delay = this._item.end - this._item.start; const delay = this._item.end - this._item.start;
await new Promise(resolve => setTimeout(resolve, delay)); await new Promise(resolve => setTimeout(resolve, delay));