Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly await types in serve #1242

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions commands/serve/web/components/Visualize/Visualize.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function Visualize() {
const [currentId, setCurrentId] = useState();
const [themeChooserAnchorEl, setThemeChooserAnchorEl] = React.useState(null);
const [languageChooserAnchorEl, setLanguageChooserAnchorEl] = React.useState(null);
const [initialized, setInit] = useState(false);
const [nebbie, setNebbie] = useState(null);
const customThemes = Array.isArray(info?.themes) && info.themes.length ? ['light', 'dark', ...info.themes] : [];

Expand All @@ -97,11 +98,16 @@ export default function Visualize() {
);

useEffect(() => {
if (info) initiateWatch(info);
(async () => {
if (info) {
await initiateWatch(info);
setInit(true);
}
})();
}, [info]);

useEffect(() => {
if (waiting) return;
if (waiting || !initialized) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add initialized to dependency list of the useEffect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do yes, good catch

const n = embed(app, {
context: {
theme: currentThemeName,
Expand Down Expand Up @@ -132,7 +138,7 @@ export default function Visualize() {
}

setNebbie(n);
}, [app, info, waiting]);
}, [app, info, waiting, initialized]);

useLayoutEffect(() => {
if (!nebbie) return;
Expand Down
4 changes: 2 additions & 2 deletions commands/serve/web/eRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const nuke = async ({ app, supernova: { name }, themes, theme, language }) => {

async function renderWithEngine() {
const info = await getConnectionInfo();
initiateWatch(info);
await initiateWatch(info);
if (!info.enigma.appId) {
location.href = location.origin; //eslint-disable-line
}
Expand Down Expand Up @@ -82,7 +82,7 @@ async function renderWithEngine() {
async function renderSnapshot() {
const info = await getConnectionInfo();
const { themes, supernova } = info;
initiateWatch(info);
await initiateWatch(info);
const element = document.querySelector('#chart-container');
element.classList.toggle('full', true);

Expand Down
16 changes: 9 additions & 7 deletions commands/serve/web/hot.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ export default function initiateWatch(info) {
window[info.supernova.name] = mo;
lightItUp(info.supernova.name);
});
if (info.types) {
info.types.forEach((t) => {
getModule(t.name, t.url).then((mo) => {
window[t.name] = mo;
});
});
}
};

if (info.sock.port) {
Expand All @@ -62,4 +55,13 @@ export default function initiateWatch(info) {
};
}
update();
if (info.types) {
const proms = info.types.map((t) => {
return getModule(t.name, t.url).then((mo) => {
window[t.name] = mo;
});
});
return Promise.all(proms);
}
return Promise.resolve();
Comment on lines +58 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional]
alternative you could set window[t.name] to a promise

window[t.name] = getModule(t.name, t.url);

then you would not need to wait here
and I think that would be closer to how it work in a real mashup

}