Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh button to remote (lazy) widgets #2494

Merged
merged 15 commits into from
Feb 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update generic types
  • Loading branch information
christian-byrne committed Feb 11, 2025
commit d7c16b1dfad5573e327df04fb9002b246e18973b
17 changes: 9 additions & 8 deletions src/composables/useRemoteWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@ const createCacheKey = (config: RemoteWidgetConfig): string => {
const getBackoff = (retryCount: number) =>
Math.min(1000 * Math.pow(2, retryCount), 512)

const isInitialized = (entry: CacheEntry<any> | undefined) =>
const isInitialized = (entry: CacheEntry<unknown> | undefined) =>
entry?.data && entry?.timestamp && entry.timestamp > 0

const isStale = (entry: CacheEntry<any> | undefined, ttl: number) =>
const isStale = (entry: CacheEntry<unknown> | undefined, ttl: number) =>
entry?.timestamp && Date.now() - entry.timestamp >= ttl

const isFetching = (entry: CacheEntry<any> | undefined) =>
const isFetching = (entry: CacheEntry<unknown> | undefined) =>
entry?.fetchPromise !== undefined

const isFailed = (entry: CacheEntry<any> | undefined) => entry?.failed === true
const isFailed = (entry: CacheEntry<unknown> | undefined) =>
entry?.failed === true

const isBackingOff = (entry: CacheEntry<any> | undefined) =>
const isBackingOff = (entry: CacheEntry<unknown> | undefined) =>
entry?.error &&
entry?.lastErrorTime &&
Date.now() - entry.lastErrorTime < getBackoff(entry.retryCount || 0)

const fetchData = async <T>(
const fetchData = async (
config: RemoteWidgetConfig,
controller: AbortController
): Promise<T> => {
) => {
const { route, response_key, query_params, timeout = TIMEOUT } = config
const res = await axios.get(route, {
params: query_params,
Expand Down Expand Up @@ -129,7 +130,7 @@ export function useRemoteWidget<

try {
currentEntry.controller = new AbortController()
currentEntry.fetchPromise = fetchData<T>(config, currentEntry.controller)
currentEntry.fetchPromise = fetchData(config, currentEntry.controller)
const data = await currentEntry.fetchPromise

setSuccess(currentEntry, data)
Expand Down