From 9d087ee23a9608bd3efd6f2287a96f3a8b6ade3d Mon Sep 17 00:00:00 2001 From: Joseph Birkner Date: Thu, 17 Oct 2024 21:07:04 +0200 Subject: [PATCH] Introduce MapService's clientId, which is used by mapget to implicitly abort a previous fetch operation. --- erdblick_app/app/map.service.ts | 24 +++++++++++++++++------- package-lock.json | 7 +++++++ package.json | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/erdblick_app/app/map.service.ts b/erdblick_app/app/map.service.ts index 170bd6b5..92af7468 100644 --- a/erdblick_app/app/map.service.ts +++ b/erdblick_app/app/map.service.ts @@ -12,6 +12,7 @@ import {InfoMessageService} from "./info.service"; import {MAX_ZOOM_LEVEL} from "./feature.search.service"; import {PointMergeService} from "./pointmerge.service"; import {KeyboardService} from "./keyboard.service"; +import * as uuid from 'uuid'; /** Expected structure of a LayerInfoItem's coverage entry. */ export interface CoverageRectItem extends Record { @@ -85,6 +86,7 @@ export class MapService { public loadedTileLayers: Map; private visualizedTileLayers: Map; private currentFetch: Fetch|null = null; + private currentFetchId: number = 0; private currentViewport: ViewportProperties; private currentVisibleTileIds: Set; private currentHighDetailTileIds: Set; @@ -111,6 +113,7 @@ export class MapService { } | null = null; zoomLevel: BehaviorSubject = new BehaviorSubject(0); statsDialogVisible: boolean = false; + clientId: string = ""; constructor(public styleService: StyleService, public parameterService: ParametersService, @@ -146,6 +149,10 @@ export class MapService { // Triggered when the user requests to zoom to a map layer. this.moveToWgs84PositionTopic = new Subject<{x: number, y: number}>(); + + // Unique client ID which ensures that tile fetch requests from this map-service + // are de-duplicated on the mapget server. + this.clientId = uuid.v4(); } public async initialize() { @@ -503,15 +510,15 @@ export class MapService { } } - // Abort previous fetch operation, if it is different from the new one. + // Ensure that the new fetch operation is different from the previous one. let newRequestBody = JSON.stringify({ requests: requests, - stringPoolOffsets: this.tileParser!.getFieldDictOffsets() + stringPoolOffsets: this.tileParser!.getFieldDictOffsets(), + clientId: this.clientId }); if (this.currentFetch) { if (this.currentFetch.bodyJson === newRequestBody) return; - this.currentFetch.abort(); this.currentFetch = null; // Clear any unparsed messages from the previous stream. this.tileStreamParsingQueue = []; @@ -526,15 +533,18 @@ export class MapService { this.tileParser!.reset(); // Launch the new fetch operation + let myFetchId = ++this.currentFetchId; this.currentFetch = new Fetch(tileUrl) .withChunkProcessing() .withMethod("POST") .withBody(newRequestBody) .withBufferCallback((message: any, messageType: any) => { - // Schedule the parsing of the newly arrived tile layer, - // but don't do it synchronously to avoid stalling the ongoing - // fetch operation. - this.tileStreamParsingQueue.push([message, messageType]); + // Schedule the parsing of the newly arrived tile layer. + // Ignore the new data, if this fetch operation is not + // the most recent one anymore. + if (myFetchId == this.currentFetchId) { + this.tileStreamParsingQueue.push([message, messageType]); + } }); this.currentFetch.go(); } diff --git a/package-lock.json b/package-lock.json index 6152a365..b81cf8de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,6 +48,7 @@ "@ngx-formly/primeng": "^6.3.9", "@types/jasmine": "~5.1.4", "@types/js-yaml": "^4.0.9", + "@types/uuid": "^10.0.0", "@typescript-eslint/eslint-plugin": "^8.2.0", "@typescript-eslint/parser": "^8.2.0", "eslint": "^9.9.0", @@ -5199,6 +5200,12 @@ "@types/node": "*" } }, + "node_modules/@types/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==", + "dev": true + }, "node_modules/@types/wrap-ansi": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz", diff --git a/package.json b/package.json index 00b3a259..9ae54b57 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@ngx-formly/primeng": "^6.3.9", "@types/jasmine": "~5.1.4", "@types/js-yaml": "^4.0.9", + "@types/uuid": "^10.0.0", "@typescript-eslint/eslint-plugin": "^8.2.0", "@typescript-eslint/parser": "^8.2.0", "eslint": "^9.9.0",