Files
jrshikoku/lib/useInterval.ts
harukin-expo-dev-env 1ecd026908 feat: Implement official position metadata handling and caching
- Add `currentTrainMetadata.ts` to manage carrying forward official position metadata between train snapshots.
- Introduce `lineDisplayName.ts` for mapping line IDs to display names.
- Create `officialPositionCache.ts` for parsing and serializing official position data with cache validation.
- Enhance `officialPositionService.ts` to support persistent caching of official positions and improve resolver functionality.
- Update `requestCoordinator.ts` to allow cancellation of polling requests without disposing of the coordinator.
- Modify `trainTimeFiltering.ts` to incorporate line ID context when checking for duplicate train data.
- Refactor `useCurrentTrain.tsx` to support polling for current train data with official position enrichment.
- Add tests for new features, including persistent cache behavior and handling of ambiguous train data.
2026-09-15 13:48:21 +09:00

87 lines
2.5 KiB
TypeScript

import { useEffect, useRef, useState } from "react";
import { AppState, AppStateStatus, Platform } from "react-native";
type Control = {
start: () => void;
stop: () => void;
};
type State = "RUNNING" | "STOPPED";
type Fn = () => void;
export const useInterval = (fn: Fn, interval: number, autostart = true, keepAliveInBackground = false) => {
const onUpdateRef = useRef<Fn>(undefined);
const [state, setState] = useState<State>(
() => (autostart ? "RUNNING" : "STOPPED"),
);
// ユーザー操作によるSTOP(AppStateによる一時停止と区別する)
const userStoppedRef = useRef(!autostart);
const keepAliveRef = useRef(keepAliveInBackground);
useEffect(() => {
keepAliveRef.current = keepAliveInBackground;
}, [keepAliveInBackground]);
const start = () => {
userStoppedRef.current = false;
setState("RUNNING");
};
const stop = () => {
userStoppedRef.current = true;
setState("STOPPED");
};
useEffect(() => {
onUpdateRef.current = fn;
}, [fn]);
useEffect(() => {
if (autostart) {
userStoppedRef.current = false;
setState("RUNNING");
} else {
userStoppedRef.current = true;
setState("STOPPED");
}
}, [autostart]);
// バックグラウンド移行時に停止、フォアグラウンド復帰時に即時実行して再開
useEffect(() => {
if (Platform.OS === "web") return;
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === "active") {
if (!userStoppedRef.current) {
// 復帰直後に即時フェッチして最新データを取得
onUpdateRef.current?.();
setState("RUNNING");
}
} else if (nextAppState === "background" || nextAppState === "inactive") {
if (!userStoppedRef.current && !keepAliveRef.current) {
// バックグラウンド中はインターバルを停止してムダなfetchエラーを防ぐ
setState("STOPPED");
}
}
};
const subscription = AppState.addEventListener("change", handleAppStateChange);
return () => {
subscription.remove();
};
}, []);
useEffect(() => {
let timerId: ReturnType<typeof setInterval> | undefined;
if (state === "RUNNING") {
timerId = setInterval(() => {
onUpdateRef.current?.();
}, interval);
} else {
if (timerId) clearInterval(timerId);
}
return () => {
if (timerId) clearInterval(timerId);
};
}, [interval, state]);
return [state, { start, stop }] as const;
};
export default useInterval;