diff --git a/src/context/ServiceInfoContext.tsx b/src/context/ServiceInfoContext.tsx index 36167719..219fbf78 100644 --- a/src/context/ServiceInfoContext.tsx +++ b/src/context/ServiceInfoContext.tsx @@ -14,7 +14,7 @@ import { useCurrentWallet, useClearCurrentWallet } from './WalletContext' import { useWebsocket } from './WebsocketContext' import { clearSession } from '../session' import { CJ_STATE_TAKER_RUNNING, CJ_STATE_MAKER_RUNNING } from '../constants/config' -import { toSemVer, UNKNOWN_VERSION } from '../utils' +import { noop, setIntervalDebounced, toSemVer, UNKNOWN_VERSION } from '../utils' import * as Api from '../libs/JmWalletApi' @@ -224,16 +224,21 @@ const ServiceInfoProvider = ({ children }: PropsWithChildren<{}>) => { useEffect(() => { const abortCtrl = new AbortController() - const refreshSession = () => { - reloadServiceInfo({ signal: abortCtrl.signal }).catch((err) => { - if (abortCtrl.signal.aborted) return - console.error(err) - }) + const refreshSession = (): Promise => { + return reloadServiceInfo({ signal: abortCtrl.signal }) + .then(noop) + .catch((err) => { + if (!abortCtrl.signal.aborted) { + console.error(err) + } + }) } refreshSession() - const interval = setInterval(refreshSession, SESSION_REQUEST_INTERVAL) + let interval: NodeJS.Timer + setIntervalDebounced(refreshSession, SESSION_REQUEST_INTERVAL, (timerId) => (interval = timerId)) + return () => { clearInterval(interval) abortCtrl.abort() diff --git a/src/context/WebsocketContext.jsx b/src/context/WebsocketContext.jsx index 7cb96293..6fd00628 100644 --- a/src/context/WebsocketContext.jsx +++ b/src/context/WebsocketContext.jsx @@ -1,6 +1,8 @@ -import React, { createContext, useEffect, useState, useContext } from 'react' +import { createContext, useEffect, useState, useContext } from 'react' import { useCurrentWallet } from './WalletContext' +import { noop } from '../utils' +import { isDevMode } from '../constants/debugFeatures' const WEBSOCKET_RECONNECT_DELAY_STEP = 1_000 const WEBSOCKET_RECONNECT_MAX_DELAY = 10_000 @@ -23,8 +25,7 @@ const connectionRetryDelayLinear = (attempt = 0) => { // path that will be proxied to the backend server const WEBSOCKET_ENDPOINT_PATH = `${window.JM.PUBLIC_PATH}/jmws` -const NOOP = () => {} -const logToDebugConsoleInDevMode = process.env.NODE_ENV === 'development' ? console.debug : NOOP +const logToDebugConsoleInDevMode = isDevMode() ? console.debug : noop const createWebSocket = () => { const { protocol, host } = window.location diff --git a/src/utils.ts b/src/utils.ts index 0f834e03..c1134af0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -94,3 +94,20 @@ export const toSemVer = (raw?: string): SemVer => { } export const scrollToTop = () => window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }) + +export const noop = () => {} + +export const setIntervalDebounced = ( + callback: () => Promise, + delay: Milliseconds, + onTimerIdChanged: (timerId: NodeJS.Timer) => void, +) => { + ;(function loop() { + onTimerIdChanged( + setTimeout(async () => { + await callback() + loop() + }, delay), + ) + })() +}