Skip to content

Commit 5cb24db

Browse files
[POC] runbot: fix useQuery
1 parent fb46cae commit 5cb24db

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

runbot/static/src/frontend/hooks.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,32 @@ export const useQuery = (fn, depsFn) => {
4040

4141
useEffect(
4242
() => {
43-
const thisCounter = ++counter;
44-
state.loading = true;
45-
fn().catch(err => {
46-
if (counter !== thisCounter) {
47-
return;
43+
// Use effect's effect can't be async.
44+
const exec = async () => {
45+
const guard = (callback) => {
46+
if (counter !== thisCounter) {
47+
return;
48+
}
49+
callback();
4850
}
49-
state.error = err;
50-
}).then(data => {
51-
if (counter !== thisCounter) {
52-
return;
51+
const thisCounter = ++counter;
52+
state.loading = true;
53+
try {
54+
const data = await fn();
55+
guard(() => {
56+
state.loading = false;
57+
state.error = null;
58+
state.data = data;
59+
});
60+
} catch (err) {
61+
guard(() => {
62+
state.loading = false;
63+
state.error = err?.message || 'error';
64+
state.data = null;
65+
});
5366
}
54-
state.loading = false;
55-
state.data = data;
56-
});
67+
}
68+
exec();
5769
},
5870
depsFn,
5971
)

runbot/static/src/frontend/routes/bundles.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class Bundles extends Component {
9393
domain.splice(0, 0, '&');
9494
domain.push(...searchDomain);
9595
}
96-
return fetch(
96+
const res = await fetch(
9797
'/runbot/api/runbot.bundle/read', {
9898
method: 'POST',
9999
body: JSON.stringify({
@@ -178,7 +178,8 @@ export class Bundles extends Component {
178178
}
179179
})
180180
}
181-
).then(d => d.json())
181+
)
182+
return await res.json();
182183
}
183184

184185
sortedCommits(commit_links) {

0 commit comments

Comments
 (0)