Skip to content

Commit

Permalink
[Rafactor] 통신 관련 코드 일부 수정
Browse files Browse the repository at this point in the history
- 통신 로직 관련하여 if/else if 조건문 조금 더 명료하게 수정

Issues #14
  • Loading branch information
novice1993 committed Sep 7, 2023
1 parent 07853bf commit bb96383
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
12 changes: 4 additions & 8 deletions client/src/hooks/useGetStockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ const useGetStockData = (companyId: number) => {

// 시간대 (timeZone) 별로 queryKey를 다르게 설정해서, 서버 데이터가 동일할 때는 캐싱된 데이터 활용하고 서버 데이터가 갱신됐을 때는 새롭게 받아옴 (서버 데이터 30분마다 갱신)
const currentTime = new Date();
const [month, day, hour, minute] = [currentTime.getMonth(), currentTime.getDay(), currentTime.getHours, currentTime.getMinutes()];
const [month, day, hour, minute] = [currentTime.getMonth(), currentTime.getDate(), currentTime.getHours, currentTime.getMinutes()];
const timeZone = minute === 0 || minute === 30 ? "30 or 60" : 0 < minute && minute < 30 ? "1~29" : "31~59";
const queryKey = `${month}${day}${hour}${timeZone}`;

// 현재 시각이 30분, 정각이 아닌 경우 남은 시간 계산하여 checkTime 함수 다시 실행
useEffect(() => {
if (minute === 0 || minute === 30) {
setAutoRefetch(true);
}

if (0 < minute && minute < 30) {
} else if (0 < minute && minute < 30) {
const delayTime = (30 - minute) * 60000;
setTimeout(() => {
refetch();
setAutoRefetch(true);
}, delayTime);
}

if (30 < minute && minute < 60) {
} else if (30 < minute && minute < 60) {
const delayTime = (60 - minute) * 60000;
setTimeout(() => {
refetch();
Expand All @@ -36,7 +32,7 @@ const useGetStockData = (companyId: number) => {

const { data, isLoading, error, refetch } = useQuery(`chartData${companyId} ${queryKey}`, () => getChartData(companyId), {
enabled: true,
refetchInterval: autoRefetch && 60000 * 10, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭
refetchInterval: autoRefetch ? 60000 * 10 : false, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭
onSuccess: () => {
console.log(new Date());
console.log(data);
Expand Down
12 changes: 4 additions & 8 deletions client/src/hooks/useGetStockInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ const useGetStockInfo = (companyId: number) => {

// 시간대 (timeZone) 별로 queryKey를 다르게 설정해서, 서버 데이터가 동일할 때는 캐싱된 데이터 활용하고 서버 데이터가 갱신됐을 때는 새롭게 받아옴 (서버 데이터 30분마다 갱신)
const currentTime = new Date();
const [month, day, hour, minute] = [currentTime.getMonth(), currentTime.getDay(), currentTime.getHours, currentTime.getMinutes()];
const [month, day, hour, minute] = [currentTime.getMonth(), currentTime.getDate(), currentTime.getHours, currentTime.getMinutes()];
const timeZone = minute === 0 || minute === 30 ? "30 or 60" : 0 < minute && minute < 30 ? "1~29" : "31~59";
const queryKey = `${month}${day}${hour}${timeZone}`;

// 현재 시각이 30분, 정각이 아닌 경우 남은 시간 계산하여 checkTime 함수 다시 실행
useEffect(() => {
if (minute === 0 || minute === 30) {
setAutoRefetch(true);
}

if (0 < minute && minute < 30) {
} else if (0 < minute && minute < 30) {
const delayTime = (30 - minute) * 60000;
setTimeout(() => {
refetch();
setAutoRefetch(true);
}, delayTime);
}

if (30 < minute && minute < 60) {
} else if (30 < minute && minute < 60) {
const delayTime = (60 - minute) * 60000;
setTimeout(() => {
refetch();
Expand All @@ -36,7 +32,7 @@ const useGetStockInfo = (companyId: number) => {

const { data, isLoading, error, refetch } = useQuery(`stockInfo${companyId} ${queryKey}}`, () => getStockInfo(companyId), {
enabled: true,
refetchInterval: autoRefetch && 60000 * 10, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭
refetchInterval: autoRefetch ? 60000 * 10 : false, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭
onSuccess: () => {
console.log(new Date());
console.log(data);
Expand Down

0 comments on commit bb96383

Please sign in to comment.