Mostra il mondo
Ogni vista si collega al suo mondo prima di mostrare qualsiasi cosa. La prima versione della vista si limita a collegarsi e a mostrare il nome del mondo, la sua descrizione e quello che ci sta succedendo adesso.
Un mondo finto
Durante lo sviluppo la pagina gira fuori dalla webapp e non ha un mondo vero a cui collegarsi. Al suo posto c'è un mondo finto che definisci come oggetto nel tuo stesso file e che non richiede né un account né la rete.
const island = {
name: 'Treasure Island',
profile: { description: 'A treasure is buried somewhere on this island. Find it!' },
now: { text: 'The hunt is on.' },
};nameeprofiledescrivono il mondo.nowè lo stato attuale secondo le regole del mondo. Un mondo vero lo aggiorna man mano che il gioco va avanti. Ilnowdi un mondo finto invece resta lo stesso per tutta la sua vita e il passo 2 mette indatai valori che cambiano.
Collegati
In React l'hook useWorld si collega e restituisce il mondo più recente:
const { world, error } = useWorld({ fake: { world: island } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;Una vista ha tre stati: (1) il collegamento è fallito, (2) la vista si sta collegando e (3) il mondo è disponibile. Gestisci i primi due prima di mostrare il mondo.
In JavaScript semplice connectToHost restituisce una Promise che si risolve con il mondo:
const world = await connectToHost({ fake: { world: island } });Per collegarti al mondo vero dentro la webapp togli fake e chiama useWorld() o connectToHost() senza argomenti.
Disegna il mondo
La prima versione legge quattro campi del mondo:
world.name: il nome del mondo.world.profile.description: di che cosa tratta.world.now?.text: quello che sta succedendo adesso. Leggilo con?.perchénowpuò esserenull.world.status:'online'mentre la persona è nel mondo e'ended'dopo che il mondo è finito.
Il componente React mostra questi campi e useWorld lo ridisegna a ogni cambiamento del mondo.
In JavaScript semplice world.subscribe(listener) chiama il listener subito e di nuovo dopo ogni cambiamento. Ogni parte della vista è una funzione che crea i suoi elementi una volta sola e li riempie nel suo listener:
function header(container, world) {
const title = document.createElement('h1');
// ...the other elements
container.append(title, about, now);
return world.subscribe((node) => {
title.textContent = node.name;
// ...
});
}Ciascuno dei passi successivi aggiunge una parte di questo tipo, scritta come funzione in JavaScript semplice e come componente in React.
Il codice
import { useWorld } from './useWorld.js';
// A fake world to build with, a small treasure hunt that needs no account and no network.
const island = {
name: 'Treasure Island',
profile: { description: 'A treasure is buried somewhere on this island. Find it!' },
now: { text: 'The hunt is on.' },
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: island } });
// The three states of every view: it failed, it is connecting, it is here.
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
return (
<main>
<Header world={world} />
</main>
);
}
// The name of the world, what it is about and what is happening now.
function Header({ world }) {
return (
<header>
<h1>{world.name}</h1>
<p>{world.profile.description}</p>
<p>
Now: {world.now?.text} The world is {world.status}.
</p>
</header>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// A fake world to build with, a small treasure hunt that needs no account and no network.
const island = {
name: 'Treasure Island',
profile: { description: 'A treasure is buried somewhere on this island. Find it!' },
now: { text: 'The hunt is on.' },
};
// The page: one element that holds the whole view.
const page = document.createElement('main');
page.textContent = 'Connecting…';
document.body.append(page);
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: island } });
page.textContent = '';
header(page, world);
// The name of the world, what it is about and what is happening now.
function header(container, world) {
const title = document.createElement('h1');
const about = document.createElement('p');
const now = document.createElement('p');
container.append(title, about, now);
// Called at once, then after every change of the world.
return world.subscribe((node) => {
title.textContent = node.name;
about.textContent = node.profile.description ?? '';
now.textContent = `Now: ${node.now?.text ?? ''} The world is ${node.status}.`;
});
}Prossimi passi
Per ora la vista mostra solo testo. Il passo 2: Disegna la mappa disegna l'isola dai dati del mondo.
Pensare per viste spiega perché una vista ridisegna tutto il mondo a ogni cambiamento. Il mondo descrive ogni campo e ogni metodo del mondo.