diff options
Diffstat (limited to 'src/lib/utils')
| -rw-r--r-- | src/lib/utils/retry.ts | 57 | ||||
| -rw-r--r-- | src/lib/utils/setImageUrl.ts | 17 |
2 files changed, 36 insertions, 38 deletions
diff --git a/src/lib/utils/retry.ts b/src/lib/utils/retry.ts index 34bc73b..ed78528 100644 --- a/src/lib/utils/retry.ts +++ b/src/lib/utils/retry.ts @@ -9,37 +9,36 @@ export const RETRY_JITTER_MAX = 3_000; const waitFor = (ms: number) => new Promise((res) => setTimeout(res, ms)); const exponentialStrategyWithJitter: RetryStrategyF = (retries: number) => - WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries) + - RETRY_JITTER_MAX * Math.random(); + WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries) + + RETRY_JITTER_MAX * Math.random(); export const continueRetryUntilValidation = async <T>( - promiseFn: () => Promise<T> | T | Promise<void> | void, - validationFn: (x: T) => Promise<boolean> | boolean = (x: T) => - Promise.resolve(!!x), - maxRetries = MAX_DEFAULT_RETRY_AMOUNT, - waitTimeStrategy: RetryStrategyF = exponentialStrategyWithJitter, - retries = 0, - lastError: undefined | unknown = undefined + promiseFn: () => Promise<T> | T | Promise<void> | void, + validationFn: (x: T) => Promise<boolean> | boolean = (x: T) => Promise.resolve(!!x), + maxRetries = MAX_DEFAULT_RETRY_AMOUNT, + waitTimeStrategy: RetryStrategyF = exponentialStrategyWithJitter, + retries = 0, + lastError: undefined | unknown = undefined ): Promise<T> => { - if (retries >= maxRetries) { - if (lastError) throw lastError; - throw new Error("Exceeded maximum retry amount"); - } - try { - if (retries) await waitFor(waitTimeStrategy(retries)); + if (retries >= maxRetries) { + if (lastError) throw lastError; + throw new Error('Exceeded maximum retry amount'); + } + try { + if (retries) await waitFor(waitTimeStrategy(retries)); - const res = await promiseFn(); - if (res && (await validationFn(res))) return res; + const res = await promiseFn(); + if (res && (await validationFn(res))) return res; - throw new Error("Validation predicate unsuccessful"); - } catch (e: unknown) { - return continueRetryUntilValidation( - promiseFn, - validationFn, - maxRetries, - waitTimeStrategy, - retries + 1, - e - ); - } -};
\ No newline at end of file + throw new Error('Validation predicate unsuccessful'); + } catch (e: unknown) { + return continueRetryUntilValidation( + promiseFn, + validationFn, + maxRetries, + waitTimeStrategy, + retries + 1, + e + ); + } +}; diff --git a/src/lib/utils/setImageUrl.ts b/src/lib/utils/setImageUrl.ts index 15566a8..d2db853 100644 --- a/src/lib/utils/setImageUrl.ts +++ b/src/lib/utils/setImageUrl.ts @@ -1,12 +1,11 @@ import { supabase } from '../supabase'; export const setImageUrl = (imageSpec) => { - const { publicURL, error } = supabase - .storage - .from('mistymountains') - .getPublicUrl(imageSpec.image); - if (!error) { - return { ...imageSpec, image: publicURL }; - } - return imageSpec; -} + const { publicURL, error } = supabase.storage + .from('mistymountains') + .getPublicUrl(imageSpec.image); + if (!error) { + return { ...imageSpec, image: publicURL }; + } + return imageSpec; +}; |
