Skip to content

Commit

Permalink
fix: remove abortIndexer option for React hook usage of indexer:
Browse files Browse the repository at this point in the history
    - it gets too complicated
    - these hooks should not be used for the purpose of queries
      where we expect the request to end. that is much better suited
      to the fetchDataFromIndexer abstraction (which can be used in
      a hook like useQuery or useSWR if desired)
  • Loading branch information
dib542 committed Nov 21, 2023
1 parent 2af13e3 commit 4d5802a
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/lib/web3/hooks/useIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,14 @@ function useIndexerStream<
DataSet = BaseDataSet<DataRow>
>(
url: URL | string | undefined,
IndexerClass: typeof IndexerStreamAccumulateSingleDataSet,
opts?: StreamOptions
IndexerClass: typeof IndexerStreamAccumulateSingleDataSet
): StaleWhileRevalidateState<DataSet>;
function useIndexerStream<
DataRow extends BaseDataRow,
DataSet = BaseDataSet<DataRow>
>(
url: URL | string | undefined,
IndexerClass: typeof IndexerStreamAccumulateDualDataSet,
opts?: StreamOptions
IndexerClass: typeof IndexerStreamAccumulateDualDataSet
): StaleWhileRevalidateState<DataSet[]>;
function useIndexerStream<
DataRow extends BaseDataRow,
Expand All @@ -293,8 +291,7 @@ function useIndexerStream<
url: URL | string | undefined,
IndexerClass:
| typeof IndexerStreamAccumulateSingleDataSet
| typeof IndexerStreamAccumulateDualDataSet,
opts?: StreamOptions
| typeof IndexerStreamAccumulateDualDataSet
): StaleWhileRevalidateState<DataSet | DataSet[]> {
const [dataset, setDataSet] = useState<DataSet | DataSet[]>();
const [isValidating, setIsValidating] = useState<boolean>(false);
Expand All @@ -304,23 +301,19 @@ function useIndexerStream<
useEffect(() => {
if (url) {
setIsValidating(true);
const stream = new IndexerClass<DataRow>(
url,
{
onAccumulated: (dataSet) => {
// note: the TypeScript here is a bit hacky but this should be ok
setDataSet(dataSet as unknown as DataSet | DataSet[]);
},
onCompleted: () => setIsValidating(false),
onError: (error) => setError(error),
const stream = new IndexerClass<DataRow>(url, {
onAccumulated: (dataSet) => {
// note: the TypeScript here is a bit hacky but this should be ok
setDataSet(dataSet as unknown as DataSet | DataSet[]);
},
opts
);
onCompleted: () => setIsValidating(false),
onError: (error) => setError(error),
});
return () => {
stream.unsubscribe();
};
}
}, [IndexerClass, opts, url]);
}, [IndexerClass, url]);

return { data: dataset, isValidating, error };
}
Expand All @@ -329,11 +322,10 @@ function useIndexerStream<
export function useIndexerStreamOfSingleDataSet<
DataRow extends BaseDataRow,
DataSet = BaseDataSet<DataRow>
>(url: URL | string | undefined, opts?: StreamOptions) {
>(url: URL | string | undefined) {
return useIndexerStream<DataRow, DataSet>(
url,
IndexerStreamAccumulateSingleDataSet,
opts
IndexerStreamAccumulateSingleDataSet
);
}

Expand All @@ -344,8 +336,7 @@ export function useIndexerStreamOfDualDataSet<
>(url: URL | string | undefined, opts?: StreamOptions) {
return useIndexerStream<DataRow, DataSet>(
url,
IndexerStreamAccumulateDualDataSet,
opts
IndexerStreamAccumulateDualDataSet
);
}

Expand Down

0 comments on commit 4d5802a

Please sign in to comment.