Skip to content

Commit 5610fa2

Browse files
oliverdowlingdfahlander
authored andcommitted
Added loading and error properties to the query. Inspired by #2089
1 parent a946b1a commit 5610fa2

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { liveQuery } from "dexie";
22

3-
export function stateQuery<T>(
4-
querier: () => T | Promise<T>,
5-
dependencies?: () => unknown[]
6-
): { current?: T } {
7-
const query = $state<{ current?: T }>({ current: undefined });
3+
export function stateQuery<T>(querier: () => T | Promise<T>, dependencies?: () => unknown[]) {
4+
const query = $state<{ current?: T; isLoading: boolean; error?: any }>({
5+
current: undefined,
6+
isLoading: true,
7+
error: undefined,
8+
});
89
$effect(() => {
910
dependencies?.();
10-
return liveQuery(querier).subscribe((result) => {
11-
if (result !== undefined) {
12-
query.current = result;
11+
return liveQuery(querier).subscribe(
12+
(value) => {
13+
query.error = undefined;
14+
if (value !== undefined) {
15+
query.current = value;
16+
query.isLoading = false;
17+
} else {
18+
query.isLoading = true;
19+
}
20+
},
21+
(error) => {
22+
query.error = error;
23+
query.isLoading = false;
1324
}
14-
}).unsubscribe;
25+
).unsubscribe;
1526
});
1627
return query;
1728
}

0 commit comments

Comments
 (0)