Skip to content

Commit 51ecfc3

Browse files
committed
Use example
1 parent 2df1fa9 commit 51ecfc3

File tree

2 files changed

+12
-31
lines changed
  • demos/react-supabase-todolist/src/app/views/todo-lists/edit
  • packages/react/src/hooks/watched

2 files changed

+12
-31
lines changed

demos/react-supabase-todolist/src/app/views/todo-lists/edit/page.tsx

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
} from '@mui/material';
2020
import Fab from '@mui/material/Fab';
2121
import { usePowerSync, useQuery } from '@powersync/react';
22-
import { SyncStreamSubscription } from '@powersync/web';
23-
import React, { Suspense, useEffect } from 'react';
22+
import React, { Suspense } from 'react';
2423
import { useParams } from 'react-router-dom';
2524

2625
/**
@@ -33,29 +32,6 @@ const TodoEditSection = () => {
3332
const supabase = useSupabase();
3433
const { id: listID } = useParams();
3534

36-
if (import.meta.env.VITE_USE_SYNC_STREAMS == 'true') {
37-
useEffect(() => {
38-
let active = true;
39-
let subscription: SyncStreamSubscription | null = null;
40-
41-
powerSync
42-
.syncStream('todos', { list: listID })
43-
.subscribe()
44-
.then((sub) => {
45-
if (!active) {
46-
sub.unsubscribe();
47-
} else {
48-
subscription = sub;
49-
}
50-
});
51-
52-
return () => {
53-
active = false;
54-
subscription?.unsubscribe();
55-
};
56-
}, [listID]);
57-
}
58-
5935
const {
6036
data: [listRecord]
6137
} = useQuery<{ name: string }>(
@@ -70,7 +46,7 @@ const TodoEditSection = () => {
7046
[listID]
7147
);
7248

73-
const { data: todos } = useQuery<TodoRecord>(
49+
const secondQuery = useQuery<TodoRecord>(
7450
/* sql */ `
7551
SELECT
7652
*
@@ -82,8 +58,12 @@ const TodoEditSection = () => {
8258
created_at DESC,
8359
id
8460
`,
85-
[listID]
61+
[listID],
62+
{
63+
streams: import.meta.env.VITE_USE_SYNC_STREAMS == 'true' ? [{ name: 'todos', parameters: { list: listID } }] : []
64+
}
8665
);
66+
const { data: todos } = secondQuery;
8767

8868
const [showPrompt, setShowPrompt] = React.useState(false);
8969
const nameInputRef = React.createRef<HTMLInputElement>();

packages/react/src/hooks/watched/useQuery.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ export function useQuery<RowType = any>(
6060
const powerSync = usePowerSync();
6161
if (!powerSync) {
6262
return {
63+
..._loadingState,
6364
isLoading: false,
64-
isFetching: false,
65-
data: [],
6665
error: new Error('PowerSync not configured.')
6766
};
6867
}
@@ -90,8 +89,10 @@ export function useQuery<RowType = any>(
9089
});
9190

9291
if (!streamsHaveSynced) {
93-
return { isLoading: true, isFetching: false, data: [], error: undefined };
92+
return { ..._loadingState };
9493
}
9594

96-
return runOnce ? single : watched;
95+
return (runOnce ? single : watched) ?? _loadingState;
9796
}
97+
98+
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };

0 commit comments

Comments
 (0)