@@ -29,13 +29,13 @@ function isDuckDBBundle(bundle: unknown): bundle is DuckDBBundle {
29
29
30
30
type ConnectionPool = Record < number , AsyncDuckDBConnection > ;
31
31
32
- type ConnectionContextValue = {
32
+ type DuckDBContextValue = {
33
33
database : AsyncDuckDB | null ;
34
34
connectionPool : ConnectionPool ;
35
35
establishConnection : ( id ?: number ) => Promise < void > ;
36
36
} ;
37
37
38
- const ConnectionContext = createContext < ConnectionContextValue | null > ( null ) ;
38
+ const DuckDBContext = createContext < DuckDBContextValue | null > ( null ) ;
39
39
40
40
// TODO: consider adding support for passing in an existing AsyncDuckDB instance
41
41
export type DuckDBProviderProps = {
@@ -124,15 +124,15 @@ export function DuckDBProvider({
124
124
) ;
125
125
126
126
return (
127
- < ConnectionContext . Provider
127
+ < DuckDBContext . Provider
128
128
value = { {
129
129
database : database . value ,
130
130
connectionPool,
131
131
establishConnection,
132
132
} }
133
133
>
134
134
{ children }
135
- </ ConnectionContext . Provider >
135
+ </ DuckDBContext . Provider >
136
136
) ;
137
137
}
138
138
@@ -155,25 +155,30 @@ export function DuckDBProvider({
155
155
* // Use the AsyncDuckDB instance
156
156
* }
157
157
*/
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 } ;
179
184
}
0 commit comments