Skip to content

Commit b93de34

Browse files
author
Alexander
committed
fix: issues with queue requesting elevator from floor in idle state
1 parent 8f2cc97 commit b93de34

3 files changed

Lines changed: 25 additions & 28 deletions

File tree

packages/back/src/elevator/ElevatorController.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class ElevatorController {
77
private _currentFloor: number = 0;
88

99
constructor(
10-
readonly door: ElevatorDoor,
10+
public readonly door: ElevatorDoor,
1111
private readonly onChange?: (status: ElevatorStatus) => void,
1212
) {
1313
// todo: I don't like it, but works for now
@@ -42,25 +42,7 @@ export class ElevatorController {
4242

4343
private _activeQ: ElevatorQueue | null = null;
4444

45-
useDownQ(): void {
46-
this._activeQ = this.downQ;
47-
48-
this.callOnChange();
49-
}
50-
51-
useUpQ(): void {
52-
this._activeQ = this.upQ;
53-
54-
this.callOnChange();
55-
}
56-
5745
get direction(): Direction {
58-
if (this._activeQ === this.upQ) {
59-
return "up";
60-
} else if (this._activeQ === this.downQ) {
61-
return "down";
62-
}
63-
6446
switch (this._activeQ) {
6547
case this.upQ: {
6648
return "up";
@@ -95,10 +77,18 @@ export class ElevatorController {
9577
}
9678

9779
queueFloor(floor: number): void {
80+
let updatedQ: ElevatorQueue;
81+
9882
if (floor > this.currentFloor) {
9983
this.upQ.add(floor);
84+
updatedQ = this.upQ;
10085
} else {
10186
this.downQ.add(floor);
87+
updatedQ = this.downQ;
88+
}
89+
90+
if (this.direction === "none") {
91+
this._activeQ = updatedQ;
10292
}
10393

10494
this.callOnChange();
@@ -124,8 +114,21 @@ export class ElevatorController {
124114
}
125115
break;
126116
}
127-
default:
117+
case "none": {
118+
if (floor > this.currentFloor) {
119+
this.upQ.add(floor);
120+
this._activeQ = this.upQ;
121+
} else {
122+
this.downQ.add(floor);
123+
this._activeQ = this.downQ;
124+
}
128125
break;
126+
}
127+
default: {
128+
break;
129+
}
129130
}
131+
132+
this.callOnChange();
130133
}
131134
}

packages/back/src/elevator/states/MovingState.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ export class MovingState extends BaseElevator {
2626
return;
2727
}
2828

29-
if (nextFloor > this.ctrl.currentFloor) {
30-
this.ctrl.useUpQ();
31-
} else {
32-
this.ctrl.useDownQ();
33-
}
34-
3529
this.startMoving();
3630
}
3731

packages/front/src/state/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function createState(): State {
2424
});
2525

2626
// todo: don't like that it's defined here, probably should go to some common place for effects
27-
effect(function goToElevatorIfElevatorArrived() {
27+
effect(function goToElevatorWhenArrived() {
2828
const elevatorArrived =
2929
user.currentFloor.value === elevator.value.currentFloor &&
3030
elevator.value.doorStatus === "open";
@@ -41,7 +41,7 @@ export function createState(): State {
4141
});
4242

4343
// user will go out on the latest requested floor
44-
effect(function goToTheFloorIfUserArrived() {
44+
effect(function goToTheFloorWhenArrived() {
4545
const userArrived =
4646
user.requestedFloor.value === elevator.value.currentFloor &&
4747
elevator.value.doorStatus === "open";

0 commit comments

Comments
 (0)