Skip to content

Commit

Permalink
chore(session): reschedule polling session api endpoint manually
Browse files Browse the repository at this point in the history
..by using setIntervalDebounced instead of setInterval
  • Loading branch information
theborakompanioni committed Oct 10, 2023
1 parent 687c031 commit 72092e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/context/ServiceInfoContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<void> => {
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()
Expand Down
7 changes: 4 additions & 3 deletions src/context/WebsocketContext.jsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>,
delay: Milliseconds,
onTimerIdChanged: (timerId: NodeJS.Timer) => void,
) => {
;(function loop() {
onTimerIdChanged(
setTimeout(async () => {
await callback()
loop()
}, delay),
)
})()
}

0 comments on commit 72092e5

Please sign in to comment.