Skip to content

Commit d643c92

Browse files
committed
wip
1 parent b7e37a0 commit d643c92

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

frontend/src/ts/components/pages/AboutPage.tsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,14 @@ export function AboutPage(): JSXElement {
4444
<Button onClick={() => connections.reload()} text="reload" />
4545
<Button onClick={() => connections.reset()} text="reset" />
4646
</Show>
47-
<Show when={connections.shouldLoad}>
48-
<AsyncContent
49-
resource={connections.resource}
50-
errorMessage="error loading connections"
51-
>
52-
{(data) => (
53-
<For each={data}>
54-
{(connection) => (
55-
<p>
56-
{connection.initiatorName} to {connection.receiverName}
57-
</p>
58-
)}
59-
</For>
47+
<Show when={connections.shouldLoad() && !connections.loading()}>
48+
<For each={connections.store}>
49+
{(connection) => (
50+
<p>
51+
{connection.initiatorName} to {connection.receiverName}
52+
</p>
6053
)}
61-
</AsyncContent>
54+
</For>
6255
</Show>
6356

6457
<div class="created">

frontend/src/ts/signals/connections.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ createEffect(() => {
2424
}
2525
});
2626

27+
createEffect(() => {
28+
const loading = connections.loading();
29+
const error = connections.error();
30+
31+
console.log("#### change in resource: ", { loading, error });
32+
});
33+
2734
//from legacy code
2835
//await connections.ready;

frontend/src/ts/signals/util/resourceBackedStore.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createSignal, createResource, createEffect } from "solid-js";
22
import { createStore, Store } from "solid-js/store";
3-
import type { Accessor, Resource } from "solid-js";
3+
import type { Accessor } from "solid-js";
44

55
export type ResourceBackedStore<T> = {
66
/**
@@ -24,9 +24,14 @@ export type ResourceBackedStore<T> = {
2424
reset: () => void;
2525

2626
/**
27-
* resource to be used to get loading and error states
27+
* store is loading
2828
*/
29-
resource: Resource<T>;
29+
loading: Accessor<boolean>;
30+
31+
/**
32+
* loading error
33+
*/
34+
error: Accessor<unknown | undefined>;
3035

3136
/**
3237
* the data store
@@ -51,40 +56,39 @@ export function createResourceBackedStore<T extends object>(
5156
initialValue: () => T,
5257
): ResourceBackedStore<T> {
5358
const [shouldLoad, setShouldLoad] = createSignal(false);
59+
const [loading, setLoading] = createSignal(false);
60+
const [error, setError] = createSignal<unknown>(undefined);
5461

55-
const [resource, { refetch, mutate }] = createResource(
56-
shouldLoad,
57-
async (load) => {
58-
if (!load) {
59-
throw new Error("Load not requested");
60-
}
61-
const result = await fetcher();
62-
63-
//@ts-expect-error TODO
64-
mutate(result);
65-
66-
return result;
62+
const [resource, { refetch }] = createResource(
63+
() => (shouldLoad() ? true : null),
64+
async () => {
65+
return fetcher();
6766
},
6867
);
6968

7069
const [store, setStore] = createStore<T>(initialValue());
7170
let ready = createReadyPromise();
7271

7372
createEffect(() => {
74-
console.log("watch resource", resource.state);
75-
if (resource.state === "pending") return;
73+
if (!shouldLoad()) {
74+
setLoading(false);
75+
setError(undefined);
76+
return;
77+
}
78+
79+
setLoading(resource.loading);
7680

7781
if (resource.error !== undefined) {
78-
//TODO figure out why this is causes logout
79-
//ready.reject(resource.error);
80-
//reset for next attempt
82+
setError(resource.error);
83+
//reset store?
8184
ready = createReadyPromise();
8285
return;
8386
}
8487

8588
const value = resource();
8689
if (value !== undefined) {
8790
setStore(value);
91+
setError(undefined);
8892
ready.resolve();
8993
}
9094
});
@@ -104,17 +108,18 @@ export function createResourceBackedStore<T extends object>(
104108
reset: () => {
105109
console.log("### reset");
106110
setShouldLoad(false);
111+
setLoading(false);
112+
setError(undefined);
107113

108-
// reset resource + store
109-
mutate(undefined);
110114
setStore(initialValue());
111115

112116
// reject any waiters
113117
//TODO figure out why this is uncaught
114118
//ready.reject(new Error("Reset"));
115119
ready = createReadyPromise();
116120
},
117-
resource,
121+
loading,
122+
error,
118123
store,
119124
ready: async () => ready.promise,
120125
};

0 commit comments

Comments
 (0)