diff --git a/src/state/internal/createQueryStore.ts b/src/state/internal/createQueryStore.ts index 08eac573608..19dbb811f65 100644 --- a/src/state/internal/createQueryStore.ts +++ b/src/state/internal/createQueryStore.ts @@ -3,6 +3,7 @@ import { StateCreator, StoreApi, UseBoundStore, create } from 'zustand'; import { subscribeWithSelector } from 'zustand/middleware'; import { IS_DEV } from '@/env'; import { RainbowError, logger } from '@/logger'; +import { time } from '@/utils'; import { RainbowPersistConfig, createRainbowStore, omitStoreMethods } from './createRainbowStore'; import { $, AttachValue, SignalFunction, Unsubscribe, attachValueSubscriptionMap } from './signal'; @@ -353,16 +354,6 @@ const SHOULD_PERSIST_INTERNAL_STATE_MAP: Record = { const ABORT_ERROR = new Error('[createQueryStore: AbortError] Fetch interrupted'); -export const time = { - seconds: (n: number) => n * 1000, - minutes: (n: number) => time.seconds(n * 60), - hours: (n: number) => time.minutes(n * 60), - days: (n: number) => time.hours(n * 24), - weeks: (n: number) => time.days(n * 7), - infinity: Infinity, - zero: 0, -}; - const MIN_STALE_TIME = time.seconds(5); export function createQueryStore< diff --git a/src/state/internal/createRainbowStore.ts b/src/state/internal/createRainbowStore.ts index e2751164f65..4249298687b 100644 --- a/src/state/internal/createRainbowStore.ts +++ b/src/state/internal/createRainbowStore.ts @@ -4,6 +4,7 @@ import { StateCreator, create } from 'zustand'; import { PersistOptions, PersistStorage, StorageValue, persist, subscribeWithSelector } from 'zustand/middleware'; import { IS_IOS } from '@/env'; import { RainbowError, logger } from '@/logger'; +import { time } from '@/utils'; const rainbowStorage = new MMKV({ id: 'rainbow-storage' }); @@ -106,7 +107,7 @@ interface LazyPersistParams> { value: StorageValue | StorageValue; } -const DEFAULT_PERSIST_THROTTLE_MS = IS_IOS ? 3000 : 5000; +const DEFAULT_PERSIST_THROTTLE_MS = IS_IOS ? time.seconds(3) : time.seconds(5); /** * Creates a persist storage object for the Rainbow store. diff --git a/src/state/internal/tests/QueryStoreTest.tsx b/src/state/internal/tests/QueryStoreTest.tsx index 39d231bd112..ca938d21c7b 100644 --- a/src/state/internal/tests/QueryStoreTest.tsx +++ b/src/state/internal/tests/QueryStoreTest.tsx @@ -13,7 +13,8 @@ // import { ParsedAssetsDictByChain } from '@/__swaps__/types/assets'; // import { AddressAssetsReceivedMessage } from '@/__swaps__/types/refraction'; // import { useBackendNetworksStore } from '@/state/backendNetworks/backendNetworks'; -// import { createQueryStore, time } from '../createQueryStore'; +// import { time } from '@/utils'; +// import { createQueryStore } from '../createQueryStore'; // import { createRainbowStore } from '../createRainbowStore'; // const ENABLE_LOGS = false; diff --git a/src/utils/index.ts b/src/utils/index.ts index aad92e7e346..2d9d56d92c6 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -36,3 +36,4 @@ export { default as withSpeed } from './withSpeed'; export { default as FallbackIcon } from './CoinIcons/FallbackIcon'; export { default as getExchangeIconUrl } from './getExchangeIconUrl'; export { resolveFirstRejectLast } from './resolveFirstRejectLast'; +export { time } from './time'; diff --git a/src/utils/time.ts b/src/utils/time.ts new file mode 100644 index 00000000000..cbf59b0e2fd --- /dev/null +++ b/src/utils/time.ts @@ -0,0 +1,40 @@ +type TimeInMs = number; +type TimeUtils = { + /** Convert seconds to milliseconds */ + seconds: (seconds: number) => TimeInMs; + /** Convert minutes to milliseconds */ + minutes: (minutes: number) => TimeInMs; + /** Convert hours to milliseconds */ + hours: (hours: number) => TimeInMs; + /** Convert days to milliseconds */ + days: (days: number) => TimeInMs; + /** Convert weeks to milliseconds */ + weeks: (weeks: number) => TimeInMs; + /** Represents infinite time */ + infinity: typeof Infinity; + /** Represents zero time */ + zero: 0; +}; + +/** + * Utility object for time conversions and helpers. + * All methods convert the input unit to milliseconds. + * @example + * time.seconds(5) // 5 seconds + * time.minutes(2) // 2 minutes + * time.hours(1) // 1 hour + * time.days(5) // 5 days + * time.weeks(2) // 2 weeks + * –– + * time.infinity // Infinity + * time.zero // 0 + */ +export const time: TimeUtils = { + seconds: seconds => seconds * 1000, + minutes: minutes => time.seconds(minutes * 60), + hours: hours => time.minutes(hours * 60), + days: days => time.hours(days * 24), + weeks: weeks => time.days(weeks * 7), + infinity: Infinity, + zero: 0, +};