Show the world
Every view connects to its world before it renders anything. The first version of the view only connects and renders the name of the world, its description and what is happening in it now.
A fake world
During development the page runs outside the webapp and has no real world to connect to. A fake world defined as an object in your own file takes its place and needs no account or network.
const island = {
name: 'Treasure Island',
profile: { description: 'A treasure is buried somewhere on this island. Find it!' },
now: { text: 'The hunt is on.' },
};nameandprofiledescribe the world.nowis the current state according to the rules of the world. A real world updates it as the game goes on. A fake world instead keeps the samenowfor its whole life. Step 2 puts the values that change indata.
Connect
In React the useWorld hook connects and returns the newest world:
const { world, error } = useWorld({ fake: { world: island } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;A view has three states: (1) the connection failed, (2) the view is connecting and (3) the world is available. Handle the first two before rendering the world.
In plain JavaScript connectToHost returns a Promise that resolves to the world:
const world = await connectToHost({ fake: { world: island } });To connect to the real world inside the webapp, remove fake and call useWorld() or connectToHost() without arguments.
Draw the world
The first version reads four fields of the world:
world.name: the name of the world.world.profile.description: what it is about.world.now?.text: what is happening now. Read it with?.becausenowcan benull.world.status:'online'while the person is in the world and'ended'after the world has finished.
In React the component renders these fields and useWorld re-renders it on every change of the world.
In plain JavaScript world.subscribe(listener) calls the listener immediately and again after every change. Each part of the view is a function that creates its elements once and fills them in its 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;
// ...
});
}Every following step adds one part of this kind, written as a function in plain JavaScript and as a component in React.
The code
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}.`;
});
}Next steps
The view renders only text so far. Step 2: Draw the map draws the island from the data of the world.
Thinking in views explains why a view renders the whole world again on every change. The world documents every field and method of the world.