Skip to content

Mostra una schermata di caricamento ​

Prima di avere qualcosa da mostrare una vista può aspettare tre cose: (1) il mondo dato dalla webapp, (2) la connessione di un agente e (3) i primi dati. Mostra per tutte e tre una sola schermata di caricamento con una riga di testo che dice che cosa la vista sta aspettando. Un agente finto si connette quasi subito e per rivedere la schermata devi premere Restart sull'esempio.

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`;
});
Live, on a fake world
Open

Come funziona ​

  1. Prima che arrivi il mondo in React useWorld restituisce world: null e in JavaScript semplice await connectToHost() non si è ancora risolto. Mostra la schermata di caricamento fin dall'inizio.
  2. Un agente si connette poco dopo che la webapp lo passa alla vista e il suo status passa allora da 'connecting' a 'online'. Un mondo è 'online' fin dall'inizio e non passa mai per 'connecting'.
  3. I dati arrivano dopo e ogni nome di data è undefined finché non arriva il suo primo valore. Aspetta solo i nomi di cui la tua vista non può fare a meno e mostra gli altri man mano che arrivano.

Un elemento <progress> senza value è indeterminato e si anima da solo senza nessun CSS.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.