File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change 1
- import React , { useState , useEffect } from 'react' ;
1
+ import React , { useState , useEffect , useContext } from 'react' ;
2
2
import { DataSubscription } from './ihp-datasync.js' ;
3
3
4
+ // Most IHP apps never use this context because they use session cookies for auth.
5
+ // Therefore the default value is true.
6
+ export const AuthCompletedContext = React . createContext ( true ) ;
7
+
4
8
/**
5
9
* Returns the result of the current query in real-time. Returns `null` while the data is still being fetched from the server.
6
10
* @example
7
11
* const messages = useQuery(query('messages').orderBy('createdAt'));
8
12
*/
9
13
export function useQuery ( queryBuilder ) {
10
14
const [ records , setRecords ] = useState ( null ) ;
15
+ const isAuthCompleted = useContext ( AuthCompletedContext ) ;
11
16
12
17
useEffect ( ( ) => {
18
+ if ( ! isAuthCompleted ) {
19
+ return ;
20
+ }
21
+
13
22
const dataSubscription = new DataSubscription ( queryBuilder . query ) ;
14
23
dataSubscription . createOnServer ( ) ;
15
24
16
25
// The dataSubscription is automatically closed when the last subscriber on
17
26
// the DataSubscription object has been unsubscribed
18
27
return dataSubscription . subscribe ( setRecords ) ;
19
28
} , [
20
- JSON . stringify ( queryBuilder . query ) /* <-- It's terrible - but it works, we should find a better for this */
29
+ JSON . stringify ( queryBuilder . query ) /* <-- It's terrible - but it works, we should find a better for this */ ,
30
+ isAuthCompleted
21
31
] )
22
32
23
33
return records ;
You can’t perform that action at this time.
0 commit comments