Skip to content

Commit

Permalink
とりあえず正統派の軽減方法
Browse files Browse the repository at this point in the history
  • Loading branch information
imamiya-masaki committed Dec 7, 2024
1 parent 98fe414 commit 7abe111
Showing 1 changed file with 66 additions and 24 deletions.
90 changes: 66 additions & 24 deletions frontend/app/components/hooks/use-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,50 @@ const currentCoodinatePost = async (coordinate: Coordinate) => {
}));
};

const postEnroute = (rideId: string, coordinate: Coordinate) => {
const postEnroute = (
rideId: string,
coordinate: Coordinate,
abortSignal: AbortSignal,
) => {
setSimulatorStartCoordinate(coordinate);
void fetchChairPostRideStatus({
body: { status: "ENROUTE" },
pathParams: {
rideId,
void fetchChairPostRideStatus(
{
body: { status: "ENROUTE" },
pathParams: {
rideId,
},
},
});
abortSignal,
);
};

const postCarring = (rideId: string) => {
void fetchChairPostRideStatus({
body: { status: "CARRYING" },
pathParams: {
rideId,
const postCarring = (rideId: string, abortSignal: AbortSignal) => {
void fetchChairPostRideStatus(
{
body: { status: "CARRYING" },
pathParams: {
rideId,
},
},
});
abortSignal,
);
};

const forcePickup = (pickup_coordinate: Coordinate) =>
setTimeout(() => {
void currentCoodinatePost(pickup_coordinate);
}, 60_000);

const forceCarry = (pickup_coordinate: Coordinate, rideId: RideId) =>
const forceCarry = (
pickup_coordinate: Coordinate,
rideId: RideId,
abortSignal: AbortSignal,
) =>
setTimeout(() => {
try {
void (async () => {
void (await currentCoodinatePost(pickup_coordinate));
postCarring(rideId);
postCarring(rideId, abortSignal);
})();
} catch (error) {
console.error(error);
Expand All @@ -90,21 +104,30 @@ export const useEmulator = () => {
useSimulatorContext();
const { pickup_coordinate, destination_coordinate, ride_id, status } =
data ?? {};
const currentCoordinate = chair?.coordinate;

useEffect(() => {
if (!isAnotherSimulatorBeingUsed) return;
if (!(pickup_coordinate && destination_coordinate && ride_id)) return;
let timeoutId: ReturnType<typeof setTimeout>;
const abortController = new AbortController();
switch (status) {
case "ENROUTE":
timeoutId = forcePickup(pickup_coordinate);
break;
case "PICKUP":
timeoutId = forceCarry(pickup_coordinate, ride_id);
timeoutId = forceCarry(
pickup_coordinate,
ride_id,
abortController.signal,
);
break;
case "CARRYING":
timeoutId = forceArrive(destination_coordinate);
break;
}
return () => {
abortController.abort();
clearTimeout(timeoutId);
};
}, [
Expand All @@ -125,22 +148,43 @@ export const useEmulator = () => {
setCoordinate?.(destination_coordinate);
}, [status, destination_coordinate, setCoordinate]);

useEffect(() => {
if (isAnotherSimulatorBeingUsed) return;
if (!ride_id || status !== "PICKUP") return;
const abortController = new AbortController();
const timeoutId = setTimeout(
() => postCarring(ride_id, abortController.signal),
1000,
);
return () => {
clearTimeout(timeoutId);
abortController.abort();
};
}, [status, ride_id, isAnotherSimulatorBeingUsed]);

useEffect(() => {
if (isAnotherSimulatorBeingUsed) return;
if (!ride_id || !currentCoordinate || status !== "MATCHING") return;
const abortController = new AbortController();
const timeoutId = setTimeout(
() => postEnroute(ride_id, currentCoordinate, abortController.signal),
1000,
);
return () => {
clearTimeout(timeoutId);
abortController.abort();
};
}, [status, ride_id, currentCoordinate, isAnotherSimulatorBeingUsed]);

useEffect(() => {
if (isAnotherSimulatorBeingUsed) return;
if (!(chair && data)) {
return;
}

const timeoutId = setTimeout(() => {
void currentCoodinatePost(chair.coordinate);
try {
switch (data.status) {
case "MATCHING":
postEnroute(data.ride_id, chair.coordinate);
break;
case "PICKUP":
postCarring(data.ride_id);
break;
case "ENROUTE":
setCoordinate?.(move(chair.coordinate, data.pickup_coordinate));
break;
Expand All @@ -149,8 +193,6 @@ export const useEmulator = () => {
move(chair.coordinate, data.destination_coordinate),
);
break;
case "ARRIVED":
setCoordinate?.(data.destination_coordinate);
}
} catch (e) {
// statusの更新タイミングの都合で到着状態を期待しているが必ず取れるとは限らない
Expand Down

0 comments on commit 7abe111

Please sign in to comment.