Show a loading screen
A view can wait for three things before it has something to show: (1) the world given by the webapp, (2) the connection of an agent and (3) the first data. Show one loading screen for all three with a line of text that names what the view is waiting for. A fake agent connects almost at once and you can see the screen again only by pressing Restart on the example.
jsx
import { useWorld } from './useWorld.js';
// The fake agent is a weather station with one reading.
const station = {
name: 'Weather station',
profile: { description: 'Live readings from the roof.' },
data: { temperature: (time) => Math.round((20 + 3 * Math.sin(time / 10000)) * 10) / 10 },
};
// One calm loading screen for every wait, with words that say what the view waits for.
function Loading({ text }) {
return (
<div style={{ display: 'grid', placeItems: 'center', minHeight: 160, textAlign: 'center' }}>
<div>
<progress aria-label="Loading" />
<p>{text}</p>
</div>
</div>
);
}
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: station } });
if (error) return <p>Could not connect ({error.code}).</p>;
// 1. The webapp has not given the view its agent yet.
if (!agent) return <Loading text="Opening the view…" />;
// 2. The agent is still connecting.
if (agent.status === 'connecting') return <Loading text={`Connecting to ${agent.name}…`} />;
// 3. The agent is here, but its first reading is not.
if (agent.data.temperature === undefined) return <Loading text="Waiting for the first reading…" />;
return (
<>
<h2>{agent.name}</h2>
<p>{agent.profile.description}</p>
<p>Temperature: {agent.data.temperature} °C</p>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The fake agent is a weather station with one reading.
const station = {
name: 'Weather station',
profile: { description: 'Live readings from the roof.' },
data: { temperature: (time) => Math.round((20 + 3 * Math.sin(time / 10000)) * 10) / 10 },
};
// One calm loading screen for every wait, with words that say what the view waits for.
const loading = document.createElement('div');
loading.style.cssText = 'display: grid; place-items: center; min-height: 160px; text-align: center;';
const bar = document.createElement('progress');
bar.setAttribute('aria-label', 'Loading');
const loadingText = document.createElement('p');
const box = document.createElement('div');
box.append(bar, loadingText);
loading.append(box);
// What the view shows once everything is here.
const content = document.createElement('div');
const title = document.createElement('h2');
const description = document.createElement('p');
const temperature = document.createElement('p');
content.append(title, description, temperature);
document.body.append(loading, content);
function showLoading(text) {
loadingText.textContent = text;
loading.hidden = false;
content.hidden = true;
}
// 1. The webapp has not given the view its agent yet.
showLoading('Opening the view…');
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: station } });
agent.subscribe((node) => {
// 2. The agent is still connecting.
if (node.status === 'connecting') return showLoading(`Connecting to ${node.name}…`);
// 3. The agent is here, but its first reading is not.
if (node.data.temperature === undefined) return showLoading('Waiting for the first reading…');
loading.hidden = true;
content.hidden = false;
title.textContent = node.name;
description.textContent = node.profile.description ?? '';
temperature.textContent = `Temperature: ${node.data.temperature} °C`;
});How it works
- Before the world arrives
useWorldreturnsworld: nullin React andawait connectToHost()has not resolved yet in plain JavaScript. Show the loading screen from the start. - An agent connects shortly after the webapp passes it to the view and its
statusthen goes from'connecting'to'online'. A world is'online'from the start and never goes through'connecting'. - Data arrives later and each name of
dataisundefineduntil its first value arrives. Wait only for the names your view cannot do without and render the others as they arrive.
A <progress> element without a value is indeterminate and animates on its own without any CSS.
See also
- LoadingAndErrors: a component that renders the loading screen and the errors.
- Show a message outside the webapp: what to render when the view never gets a world.
- The data of the world.