Skip to content

Commit cda56b4

Browse files
authored
Rename context, add return type for hook
1 parent 82374be commit cda56b4

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

packages/react-duckdb/src/duckdb_provider.tsx

+30-25
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ function isDuckDBBundle(bundle: unknown): bundle is DuckDBBundle {
2929

3030
type ConnectionPool = Record<number, AsyncDuckDBConnection>;
3131

32-
type ConnectionContextValue = {
32+
type DuckDBContextValue = {
3333
database: AsyncDuckDB | null;
3434
connectionPool: ConnectionPool;
3535
establishConnection: (id?: number) => Promise<void>;
3636
};
3737

38-
const ConnectionContext = createContext<ConnectionContextValue | null>(null);
38+
const DuckDBContext = createContext<DuckDBContextValue | null>(null);
3939

4040
// TODO: consider adding support for passing in an existing AsyncDuckDB instance
4141
export type DuckDBProviderProps = {
@@ -124,15 +124,15 @@ export function DuckDBProvider({
124124
);
125125

126126
return (
127-
<ConnectionContext.Provider
127+
<DuckDBContext.Provider
128128
value={{
129129
database: database.value,
130130
connectionPool,
131131
establishConnection,
132132
}}
133133
>
134134
{children}
135-
</ConnectionContext.Provider>
135+
</DuckDBContext.Provider>
136136
);
137137
}
138138

@@ -155,25 +155,30 @@ export function DuckDBProvider({
155155
* // Use the AsyncDuckDB instance
156156
* }
157157
*/
158-
export function useDuckDB(connectionId?: number) {
159-
const context = useContext(ConnectionContext);
160-
if (!context) {
161-
throw new Error('useDuckDB must be used within a DuckDBProvider');
162-
}
163-
164-
const { database, connectionPool, establishConnection } = context;
165-
166-
// Check if a connection exists in the pool for the given ID
167-
const connection = connectionPool[connectionId || 0] || null;
168-
169-
// Determine if a connection is currently being established
170-
const isConnecting = !connection && !connectionPool[connectionId || 0];
171-
172-
useEffect(() => {
173-
// If no connection exists and it's not currently being established,
174-
// trigger the establishConnection function to create a new connection
175-
if (isConnecting) establishConnection(connectionId);
176-
}, [connectionId, isConnecting, establishConnection]);
177-
178-
return { database, connection, isConnecting };
158+
export function useDuckDB(connectionId?: number): {
159+
database: AsyncDuckDB | null;
160+
connection: AsyncDuckDBConnection | null;
161+
isConnecting: boolean;
162+
} {
163+
const context = useContext(DuckDBContext);
164+
if (!context) {
165+
throw new Error("useDuckDB must be used within a DuckDBProvider");
166+
}
167+
168+
const { database, connectionPool, establishConnection } = context;
169+
170+
// Check if a connection exists in the pool for the given ID
171+
const connection: AsyncDuckDBConnection | null =
172+
connectionPool[connectionId || 0] || null;
173+
174+
// Determine if a connection is currently being established
175+
const isConnecting = !connection && !connectionPool[connectionId || 0];
176+
177+
useEffect(() => {
178+
// If no connection exists and it's not currently being established,
179+
// trigger the establishConnection function to create a new connection
180+
if (isConnecting) establishConnection(connectionId);
181+
}, [connectionId, isConnecting, establishConnection]);
182+
183+
return { database, connection, isConnecting };
179184
}

0 commit comments

Comments
 (0)