Skip to content

Commit

Permalink
Move time helper to @/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbaroni committed Jan 10, 2025
1 parent 958cd90 commit cd887a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
11 changes: 1 addition & 10 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -353,16 +354,6 @@ const SHOULD_PERSIST_INTERNAL_STATE_MAP: Record<string, boolean> = {

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<
Expand Down
3 changes: 2 additions & 1 deletion src/state/internal/createRainbowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down Expand Up @@ -106,7 +107,7 @@ interface LazyPersistParams<S, PersistedState extends Partial<S>> {
value: StorageValue<S> | StorageValue<PersistedState>;
}

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.
Expand Down
3 changes: 2 additions & 1 deletion src/state/internal/tests/QueryStoreTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
40 changes: 40 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit cd887a1

Please sign in to comment.