Skip to content

Commit

Permalink
Organize types, skip string conversion if unneeded in getOrCreateAtta…
Browse files Browse the repository at this point in the history
…chValue
  • Loading branch information
christianbaroni committed Dec 22, 2024
1 parent b8c4a3c commit c677ad2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
34 changes: 17 additions & 17 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,6 @@ export type RainbowQueryStoreConfig<TQueryFnData, TParams extends Record<string,
* Receives parameters of type `TParams` and returns either a promise or a raw data value of type `TQueryFnData`.
*/
fetcher: (params: TParams) => TQueryFnData | Promise<TQueryFnData>;
/**
* The maximum number of times to retry a failed fetch operation.
* @default 3
*/
maxRetries?: number;
/**
* The delay between retries after a fetch error occurs, in milliseconds, defined as a number or a function that
* receives the error and current retry count and returns a number.
* @default time.seconds(5)
*/
retryDelay?: number | ((retryCount: number, error: Error) => number);
/**
* A callback invoked whenever a fetch operation fails.
* Receives the error and the current retry count.
Expand All @@ -199,7 +188,7 @@ export type RainbowQueryStoreConfig<TQueryFnData, TParams extends Record<string,
* A function that overrides the default behavior of setting the fetched data in the store's query cache.
* Receives an object containing the transformed data, the query parameters, the query key, and the store's set function.
*
* When using `setData`, it’s important to note that you are taking full responsibility for managing query data. if your
* When using `setData`, it’s important to note that you are taking full responsibility for managing query data. If your
* query supports variable parameters (and thus multiple query keys) and you want to cache data for each key, you’ll need
* to manually handle storing data based on the provided `params` or `queryKey`. Naturally, you will also bear
* responsibility for pruning this data in the event you do not want it persisted indefinitely.
Expand All @@ -214,11 +203,6 @@ export type RainbowQueryStoreConfig<TQueryFnData, TParams extends Record<string,
queryKey: string;
set: (partial: S | Partial<S> | ((state: S) => S | Partial<S>)) => void;
}) => void;
/**
* Suppresses warnings in the event a `staleTime` under the minimum is desired.
* @default false
*/
suppressStaleTimeWarning?: boolean;
/**
* A function to transform the raw fetched data (`TQueryFnData`) into another form (`TData`).
* If not provided, the raw data returned by `fetcher` is used.
Expand All @@ -245,13 +229,24 @@ export type RainbowQueryStoreConfig<TQueryFnData, TParams extends Record<string,
* @default true
*/
enabled?: boolean;
/**
* The maximum number of times to retry a failed fetch operation.
* @default 3
*/
maxRetries?: number;
/**
* Parameters to be passed to the fetcher, defined as either direct values or `ParamResolvable` functions.
* Dynamic parameters using `AttachValue` will cause the store to refetch when their values change.
*/
params?: {
[K in keyof TParams]: ParamResolvable<TParams[K], TParams, S, TData>;
};
/**
* The delay between retries after a fetch error occurs, in milliseconds, defined as a number or a function that
* receives the error and current retry count and returns a number.
* @default time.seconds(5)
*/
retryDelay?: number | ((retryCount: number, error: Error) => number);
/**
* The duration, in milliseconds, that data is considered fresh after fetching.
* After becoming stale, the store may automatically refetch data in the background if there are active subscribers.
Expand All @@ -260,6 +255,11 @@ export type RainbowQueryStoreConfig<TQueryFnData, TParams extends Record<string,
* @default time.minutes(2)
*/
staleTime?: number;
/**
* Suppresses warnings in the event a `staleTime` under the minimum is desired.
* @default false
*/
suppressStaleTimeWarning?: boolean;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/state/internal/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ function getOrCreateAttachValue<T, S>(store: StoreApi<T>, selector: (state: T) =
}
return v;
}
const pathKey = fullPath ? `${fullPath}.${key.toString()}` : key.toString();
const keyString = typeof key === 'string' ? key : key.toString();
const pathKey = fullPath ? `${fullPath}.${keyString}` : keyString;
const cached = localCache.get(pathKey);
if (cached) {
if (ENABLE_LOGS) console.log('[🌀 AttachValue 🌀] Cache hit for:', pathKey);
Expand Down
8 changes: 4 additions & 4 deletions src/state/internal/tests/QueryStoreTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const UserAssetsTest = memo(function UserAssetsTest() {
)
)}
</View>
<Text color="label" size="17pt" weight="heavy">
<Text align="center" color="label" size="17pt" weight="heavy">
{data
? `Number of assets: ${Object.values(data).reduce((acc, chainAssets) => acc + Object.keys(chainAssets).length, 0)}`
: 'Loading…'}
Expand All @@ -119,7 +119,7 @@ export const UserAssetsTest = memo(function UserAssetsTest() {
}}
style={styles.button}
>
<Text color="label" size="17pt" weight="heavy">
<Text align="center" color="label" size="17pt" weight="heavy">
Shuffle Address
</Text>
</ButtonPressAnimation>
Expand All @@ -129,7 +129,7 @@ export const UserAssetsTest = memo(function UserAssetsTest() {
}}
style={styles.button}
>
<Text color="label" size="17pt" weight="heavy">
<Text align="center" color="label" size="17pt" weight="heavy">
{useUserAssetsTestStore.getState().enabled ? 'Disable Fetching' : 'Enable Fetching'}
</Text>
</ButtonPressAnimation>
Expand Down Expand Up @@ -194,7 +194,7 @@ export async function simpleUserAssetsQuery({ address, currency }: FetchUserAsse
}
return {};
} catch (e) {
logger.error(new RainbowError('[userAssetsQueryFunction]: Failed to fetch user assets'), {
logger.error(new RainbowError('[simpleUserAssetsQuery]: Failed to fetch user assets'), {
message: (e as Error)?.message,
});
return {};
Expand Down

0 comments on commit c677ad2

Please sign in to comment.