Skip to content

Commit 72c366e

Browse files
committed
Added AuthCompletedContext to DataSync
1 parent b7c949e commit 72c366e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/IHP/DataSync/react.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useContext } from 'react';
22
import { DataSubscription } from './ihp-datasync.js';
33

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+
48
/**
59
* Returns the result of the current query in real-time. Returns `null` while the data is still being fetched from the server.
610
* @example
711
* const messages = useQuery(query('messages').orderBy('createdAt'));
812
*/
913
export function useQuery(queryBuilder) {
1014
const [records, setRecords] = useState(null);
15+
const isAuthCompleted = useContext(AuthCompletedContext);
1116

1217
useEffect(() => {
18+
if (!isAuthCompleted) {
19+
return;
20+
}
21+
1322
const dataSubscription = new DataSubscription(queryBuilder.query);
1423
dataSubscription.createOnServer();
1524

1625
// The dataSubscription is automatically closed when the last subscriber on
1726
// the DataSubscription object has been unsubscribed
1827
return dataSubscription.subscribe(setRecords);
1928
}, [
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
2131
])
2232

2333
return records;

0 commit comments

Comments
 (0)