From 520f6372b8b7bdf10ae50a65c98c1e747aa3f19a Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Fri, 7 Dec 2018 13:56:46 -0700 Subject: [PATCH] Map inspector requests by id so single requests can be reset at a time (#26770) (#26810) --- .../adapters/request/request_adapter.ts | 21 +++++++++++++++---- .../inspector/adapters/request/types.ts | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ui/public/inspector/adapters/request/request_adapter.ts b/src/ui/public/inspector/adapters/request/request_adapter.ts index 9ae429901ea63..a1a1463b8f2f6 100644 --- a/src/ui/public/inspector/adapters/request/request_adapter.ts +++ b/src/ui/public/inspector/adapters/request/request_adapter.ts @@ -18,6 +18,8 @@ */ import { EventEmitter } from 'events'; +import _ from 'lodash'; +import uuid from 'uuid/v4'; import { RequestResponder } from './request_responder'; import { Request, RequestParams, RequestStatus } from './types'; @@ -29,7 +31,12 @@ import { Request, RequestParams, RequestStatus } from './types'; * @extends EventEmitter */ class RequestAdapter extends EventEmitter { - private requests: Request[] = []; + private requests: Map; + + constructor() { + super(); + this.requests = new Map(); + } /** * Start logging a new request into this request adapter. The new request will @@ -47,19 +54,25 @@ class RequestAdapter extends EventEmitter { name, startTime: Date.now(), status: RequestStatus.PENDING, + id: _.get(params, 'id', uuid()), }; - this.requests.push(req); + this.requests.set(req.id, req); this._onChange(); return new RequestResponder(req, () => this._onChange()); } public reset(): void { - this.requests = []; + this.requests = new Map(); + this._onChange(); + } + + public resetRequest(id: string): void { + this.requests.delete(id); this._onChange(); } public getRequests(): Request[] { - return this.requests; + return Array.from(this.requests.values()); } private _onChange(): void { diff --git a/src/ui/public/inspector/adapters/request/types.ts b/src/ui/public/inspector/adapters/request/types.ts index 8e0b6276ae69d..16c9f8d0ab1ab 100644 --- a/src/ui/public/inspector/adapters/request/types.ts +++ b/src/ui/public/inspector/adapters/request/types.ts @@ -36,6 +36,7 @@ export enum RequestStatus { } export interface Request extends RequestParams { + id: string; name: string; json?: object; response?: Response; @@ -46,6 +47,7 @@ export interface Request extends RequestParams { } export interface RequestParams { + id?: string; description?: string; }