forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request galaxyproject#17355 from davelopez/unify_continuou…
…s_polling Unify continuous polling with composable
- Loading branch information
Showing
13 changed files
with
148 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
export type WatchResourceHandler = () => Promise<void>; | ||
|
||
export interface WatchOptions { | ||
/** | ||
* Polling interval in milliseconds when the app is active (in the current tab). | ||
*/ | ||
shortPollingInterval?: number; | ||
/** | ||
* Polling interval in milliseconds when the app is in the background (not in the current tab). | ||
*/ | ||
longPollingInterval?: number; | ||
/** | ||
* If true, the resource is watched in the background even when the app is not active (in the current tab). | ||
*/ | ||
enableBackgroundPolling?: boolean; | ||
} | ||
|
||
const DEFAULT_WATCH_OPTIONS: WatchOptions = { | ||
shortPollingInterval: 3000, | ||
longPollingInterval: 10000, | ||
enableBackgroundPolling: true, | ||
}; | ||
|
||
/** | ||
* Creates a composable that watches a resource by polling the server continuously. | ||
* By default, the polling interval is 'short' when the app is active (in the current tab) and 'long' | ||
* when the app is in the background (not in the current tab). | ||
* You can also completely disable background polling by setting `enableBackgroundPolling` to false in the options. | ||
* @param watchHandler The handler function that watches the resource by querying the server. | ||
* @param options Options to customize the polling interval. | ||
*/ | ||
export function useResourceWatcher(watchHandler: WatchResourceHandler, options: WatchOptions = DEFAULT_WATCH_OPTIONS) { | ||
const { shortPollingInterval, longPollingInterval, enableBackgroundPolling } = { | ||
...DEFAULT_WATCH_OPTIONS, | ||
...options, | ||
}; | ||
let currentPollingInterval = shortPollingInterval; | ||
let watchTimeout: NodeJS.Timeout | null = null; | ||
let isEventSetup = false; | ||
|
||
/** | ||
* Starts watching the resource by polling the server continuously. | ||
*/ | ||
function startWatchingResource() { | ||
stopWatchingResource(); | ||
tryWatchResource(); | ||
} | ||
|
||
/** | ||
* Stops continuously watching the resource. | ||
*/ | ||
function stopWatchingResource() { | ||
if (watchTimeout) { | ||
clearTimeout(watchTimeout); | ||
watchTimeout = null; | ||
} | ||
} | ||
|
||
async function tryWatchResource() { | ||
try { | ||
await watchHandler(); | ||
} catch (error) { | ||
console.warn(error); | ||
} finally { | ||
if (currentPollingInterval) { | ||
watchTimeout = setTimeout(() => { | ||
tryWatchResource(); | ||
}, currentPollingInterval); | ||
} | ||
} | ||
} | ||
|
||
function setupVisibilityListeners() { | ||
if (!isEventSetup) { | ||
isEventSetup = true; | ||
document.addEventListener("visibilitychange", updateThrottle); | ||
} | ||
} | ||
|
||
function updateThrottle() { | ||
if (document.visibilityState === "visible") { | ||
currentPollingInterval = shortPollingInterval; | ||
startWatchingResource(); | ||
} else { | ||
currentPollingInterval = enableBackgroundPolling ? longPollingInterval : undefined; | ||
} | ||
} | ||
|
||
setupVisibilityListeners(); | ||
|
||
return { | ||
startWatchingResource, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.