From 7b80426a8e5bf176f4ff1e154c08d1480c389e4a Mon Sep 17 00:00:00 2001 From: flyingtof Date: Thu, 5 Sep 2024 17:47:50 +0200 Subject: [PATCH] polish: small changes after code review --- .../src/app/components/2d/path-element.ts | 2 +- apps/fxc-front/src/app/logic/score/scorer.ts | 37 +++++-------------- libs/optimizer/src/lib/optimizer.ts | 4 +- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/apps/fxc-front/src/app/components/2d/path-element.ts b/apps/fxc-front/src/app/components/2d/path-element.ts index f67e42e5..57c32fcb 100644 --- a/apps/fxc-front/src/app/components/2d/path-element.ts +++ b/apps/fxc-front/src/app/components/2d/path-element.ts @@ -270,7 +270,7 @@ export class PathElement extends connect(store)(LitElement) { // add number of turn points for an open distance circuit = scoringResult.circuit === CircuitType.OpenDistance - ? CIRCUIT_SHORT_NAME[scoringResult.circuit] + scoringResult.turnpoints.length + ? CIRCUIT_SHORT_NAME[scoringResult.circuit] + String(scoringResult.turnpoints.length) : CIRCUIT_SHORT_NAME[scoringResult.circuit]; window.parent.postMessage( JSON.stringify({ diff --git a/apps/fxc-front/src/app/logic/score/scorer.ts b/apps/fxc-front/src/app/logic/score/scorer.ts index 1db1f626..6ceb428a 100644 --- a/apps/fxc-front/src/app/logic/score/scorer.ts +++ b/apps/fxc-front/src/app/logic/score/scorer.ts @@ -21,42 +21,17 @@ export class Scorer { /** * Scores a track * - * `handleScoringResult` function is invoked in a worker context which means that 'this' keyword is a reference - * to the worker itself. If a function body uses 'this' keyword and is sent to the constructor as a reference, - * it will not work. In this case, the function should be wrapped in an arrow function (see example bellow).
- * - * E.g: - * ``` - * class MyClass { - * ... - * handleResult(result: ScoringResult){ - * ... - * this.doSomethingWithResult(result); - * ... - * } - * ... - * // although this is a valid code, it will not work because 'this' is the worker. - * const scorer = new Scorer(); - * const scoringRequestId = scorer.score(track, league, this.handleResult); - * // the correct syntax is: - * const scoringRequestId = scorer.core(track, league, (result)=>this.handleResult(result)); - * } - * ``` - * * @param track * @param league - * @param handleScoringResult {ScoringResultHandler} - * Takes the ScoringResult into account - * @return {number} a number that identifies this scoring request + * @param handleScoringResult Callback called with the scoring solution + * @return a number that identifies this scoring request */ public score(track: LatLonAltTime[], league: LeagueCode, handleScoringResult: ScoringResultHandler): number { - // lazy creation of the worker - this.scoringWorker ??= this.createWorker(); // stores the handler for retrieval when handling worker response message const id = ++this.currentScoringRequestId; this.handlers.set(id, handleScoringResult); try { - this.scoringWorker.postMessage({ + this.getWorker().postMessage({ request: { track: { points: track, @@ -79,6 +54,12 @@ export class Scorer { this.scoringWorker?.terminate(); } + // get worker with lazy instanciation + private getWorker(): Worker { + this.scoringWorker ??= this.createWorker(); + return this.scoringWorker; + } + private createWorker(): Worker { const scoringWorker = new ScoringWorker(); scoringWorker.onmessage = (msg: MessageEvent) => { diff --git a/libs/optimizer/src/lib/optimizer.ts b/libs/optimizer/src/lib/optimizer.ts index 173e9881..743a3e59 100644 --- a/libs/optimizer/src/lib/optimizer.ts +++ b/libs/optimizer/src/lib/optimizer.ts @@ -61,7 +61,7 @@ export interface ScoringResult { in: LatLon; out: LatLon; }; - // optimized path suitable for gmaps APIs + // optimized path path: { lat: number; lng: number }[]; } @@ -200,7 +200,7 @@ function toOptimizationResult(solution: Solution, track: ScoringTrack): ScoringR closingPoints, path: [closingPoints?.in, startPoint, ...turnpoints, endPoint, closingPoints?.out] .filter((p) => p != null) - .map((latLon) => ({ lat: latLon?.lat, lng: latLon?.lon })), + .map((latLon) => ({ lat: latLon.lat, lng: latLon.lon })), }; }