Skip to content

Commit

Permalink
polish: small changes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingtof committed Sep 5, 2024
1 parent e9693e5 commit 7b80426
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/fxc-front/src/app/components/2d/path-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
37 changes: 9 additions & 28 deletions apps/fxc-front/src/app/logic/score/scorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).<br/>
*
* 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,
Expand All @@ -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<WorkerResponse>) => {
Expand Down
4 changes: 2 additions & 2 deletions libs/optimizer/src/lib/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface ScoringResult {
in: LatLon;
out: LatLon;
};
// optimized path suitable for gmaps APIs
// optimized path
path: { lat: number; lng: number }[];
}

Expand Down Expand Up @@ -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 })),
};
}

Expand Down

0 comments on commit 7b80426

Please sign in to comment.