From 0f45d3ce1403225833a9fe496caaa1704186e6fd Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Mon, 31 Jul 2023 15:11:10 +0900 Subject: [PATCH] =?UTF-8?q?App=E3=81=AE=E7=99=BA=E8=A1=8C=E4=BE=9D?= =?UTF-8?q?=E9=A0=BC=E6=83=85=E5=A0=B1=E3=82=92=E3=82=A4=E3=83=B3=E3=82=BF?= =?UTF-8?q?=E3=83=BC=E3=83=90=E3=83=AB=E3=81=A7fetch=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 排他エラーとなる確率を下げるため --- src/contexts/AppContext.tsx | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/contexts/AppContext.tsx b/src/contexts/AppContext.tsx index c0d28a8..7567f9f 100644 --- a/src/contexts/AppContext.tsx +++ b/src/contexts/AppContext.tsx @@ -45,11 +45,18 @@ export function AppContextProvider({ children }: Props) { const checkTokenAPI = useAPICall({ apiMethod: checkToken, onSuccess: (res, sendData) => { - setTokenResult("ok"); - _setToken(sendData.access_token); - setReceiptIssuingOrder(res.data.receipt_issuing_order); + if (receiptIssuingOrder === null) { + setTokenResult("ok"); + _setToken(sendData.access_token); + setStore(StoreId.ACCESS_TOKEN, sendData.access_token); + } - setStore(StoreId.ACCESS_TOKEN, sendData.access_token); + // 再Fetch用 タイムスタンプが違う場合のみ取り込む + const lastTimestamp = receiptIssuingOrder?.updated_at ?? ""; + if (lastTimestamp !== res.data.receipt_issuing_order.updated_at) { + console.info("発行依頼情報読込"); + setReceiptIssuingOrder(res.data.receipt_issuing_order); + } }, onFailed: () => { setTokenResult("ng"); @@ -89,6 +96,20 @@ export function AppContextProvider({ children }: Props) { } }, [paramToken]); + // 定期的にFetchする + // 排他エラーとなる確率を下げるため + useEffect(() => { + if (token) { + const interval = setInterval(() => { + fetch(); + }, 5 * 1000); + + return () => { + clearInterval(interval); + }; + } + }, [token]); + return (