Skip to content

Commit

Permalink
Code cleanup in planner
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Dec 29, 2023
1 parent 4390124 commit 35518a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Url Serialization', () => {
const dispatcher = new ReloadDispatcher();
units.imperialUnits = imperial;
const schedules = new DiveSchedules(units, dispatcher);
const planner = new PlannerService(irrelevantFactory, schedules, dispatcher, units);
const planner = new PlannerService(schedules, dispatcher, irrelevantFactory, units);
const viewSwitch = new ViewSwitchService(schedules);
const preferences = new Preferences(viewSwitch, units, schedules, new ViewStates());
const urlSerialization = new PlanUrlSerialization(viewSwitch, units, schedules, preferences);
Expand Down
1 change: 0 additions & 1 deletion projects/planner/src/app/shared/depths.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ describe('Depths service', () => {

describe('When Calculated', () => {
it('Max bottom time is applied', () => {
console.log(depthService.segments);
depthService.applyMaxDuration();
expect(depthService.planDuration).toBe(expectedDuration);
});
Expand Down
6 changes: 6 additions & 0 deletions projects/planner/src/app/shared/diveresults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ export class DiveResults {
this.ceilings = [];
this.events = [];
}

public endCalculation(): void {
this.profileCalculated = true;
this.diveInfoCalculated = true;
this.calculated = true;
}
}
51 changes: 26 additions & 25 deletions projects/planner/src/app/shared/planner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ export class PlannerService extends Streamed {
private waypoints: WayPointsService;

constructor(
private workerFactory: WorkersFactoryCommon,
private schedules: DiveSchedules,
private dispatcher: ReloadDispatcher,
workerFactory: WorkersFactoryCommon,
units: UnitConversion) {
super();

this.waypoints = new WayPointsService(units);
this.profileTask = this.workerFactory.createProfileWorker();
this.profileTask = workerFactory.createProfileWorker();
this.profileTask.calculated$.pipe(takeUntil(this.unsubscribe$))
.subscribe((data) => this.continueCalculation(data));
this.profileTask.failed$.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => this.profileFailed());

this.diveInfoTask = this.workerFactory.createDiveInfoWorker();
this.diveInfoTask = workerFactory.createDiveInfoWorker();
this.diveInfoTask.calculated$.pipe(takeUntil(this.unsubscribe$))
.subscribe((calculated) => this.finishDiveInfo(calculated));
this.diveInfoTask.failed$.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => this.profileFailed());

this.consumptionTask = this.workerFactory.createConsumptionWorker();
this.consumptionTask = workerFactory.createConsumptionWorker();
this.consumptionTask.calculated$.pipe(takeUntil(this.unsubscribe$))
.subscribe((data) => this.finishCalculation(data));
this.consumptionTask.failed$.pipe(takeUntil(this.unsubscribe$))
Expand Down Expand Up @@ -88,9 +88,7 @@ export class PlannerService extends Streamed {
this.calculatingProfile = false;
this.calculatingDiveInfo = false;
const dive = this.diveResult(diveId);
dive.profileCalculated = true;
dive.diveInfoCalculated = true;
dive.calculated = true;
dive.endCalculation();
}

private showStillRunning(diveId: number): void {
Expand Down Expand Up @@ -119,37 +117,40 @@ export class PlannerService extends Streamed {
const tankData = dive.tanksService.tankData;
const calculatedProfile = DtoSerialization.toProfile(result.profile, tankData);
const events = DtoSerialization.toEvents(result.events);
const diveResult = this.diveResult(result.diveId);
const diveResult = dive.diveResult;
diveResult.wayPoints = this.wayPointsFromResult(calculatedProfile);
diveResult.ceilings = calculatedProfile.ceilings;
diveResult.events = events.items;
diveResult.finalTissues = calculatedProfile.tissues;
diveResult.averageDepth = Segments.averageDepth(calculatedProfile.segments);

if (diveResult.endsOnSurface) {
const infoRequest = this.createProfileRequest(calculatedProfile.tissues, result.diveId);
this.diveInfoTask.calculate(infoRequest);

const consumptionRequest = {
diveId: result.diveId,
plan: infoRequest.plan,
profile: DtoSerialization.fromSegments(calculatedProfile.segments),
options: infoRequest.options,
diver: DtoSerialization.fromDiver(dive.optionsService.getDiver()),
tanks: infoRequest.tanks
};
this.consumptionTask.calculate(consumptionRequest);

diveResult.profileCalculated = true;
this.calculatingProfile = false;
this.dispatcher.sendWayPointsCalculated();
this.processCalculatedProfile(calculatedProfile, result, dive);
} else {
// fires info finished before the profile finished, case of error it doesn't matter
this.profileFailed(result.diveId);
console.table(calculatedProfile.errors);
}
}

private processCalculatedProfile(calculatedProfile: CalculatedProfile, result: ProfileResultDto, dive: DiveSchedule) {
const infoRequest = this.createProfileRequest(calculatedProfile.tissues, result.diveId);
this.diveInfoTask.calculate(infoRequest);

const consumptionRequest = {
diveId: result.diveId,
plan: infoRequest.plan,
profile: DtoSerialization.fromSegments(calculatedProfile.segments),
options: infoRequest.options,
diver: DtoSerialization.fromDiver(dive.optionsService.getDiver()),
tanks: infoRequest.tanks
};
this.consumptionTask.calculate(consumptionRequest);

dive.diveResult.profileCalculated = true;
this.calculatingProfile = false;
this.dispatcher.sendWayPointsCalculated();
}

private createProfileRequest(previousDivetissues: LoadedTissue[], diveId: number): ProfileRequestDto {
const dive = this.diveBy(diveId);
const serializableTanks = dive.tanksService.tanks as ITankBound[];
Expand Down

0 comments on commit 35518a1

Please sign in to comment.