Prova gli errori con un mondo finto
Per vedere lo stato di errore della tua vista e provare il suo pulsante Retry mentre sviluppi, fai fallire apposta un mondo finto. failNext: true fa fallire il prossimo messaggio della persona come un messaggio perso su una rete instabile. Premi Send hello per vedere il messaggio fallire e poi Retry per vederlo passare e il mondo rispondere.
import { useWorld } from './useWorld.js';
// A fake world to build with, made to fail. The next message the person sends
// fails, as on a bad network, and the ones after it go through.
const arena = {
name: 'Arena',
failNext: true,
onMessage: (message) => `Got it: ${message.text}`,
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: arena } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// Drawn again at every change, including when the delivery of a message changes.
return (
<>
<button onClick={() => world.send('hello')}>Send hello</button>
{world.messages.map((message) => (
<p key={message.id}>
{message.fromMe ? `You: ${message.text} (${message.status})` : `${message.from}: ${message.text}`}
{/* A failed message from the person can be sent again. */}
{message.status === 'failed' && <> <button onClick={() => message.retry()}>Retry</button></>}
</p>
))}
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// A fake world to build with, made to fail. The next message the person sends
// fails, as on a bad network, and the ones after it go through.
const arena = {
name: 'Arena',
failNext: true,
onMessage: (message) => `Got it: ${message.text}`,
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });
const send = document.createElement('button');
send.textContent = 'Send hello';
send.onclick = () => world.send('hello');
const list = document.createElement('div');
document.body.append(send, list);
// Called at once, then after every change, including when the delivery of a message changes.
world.subscribe((node) => {
list.replaceChildren(...node.messages.map((message) => {
const line = document.createElement('p');
line.textContent = message.fromMe ? `You: ${message.text} (${message.status})` : `${message.from}: ${message.text}`;
// A failed message from the person can be sent again.
if (message.status === 'failed') {
const retry = document.createElement('button');
retry.textContent = 'Retry';
retry.onclick = () => message.retry();
line.append(' ', retry);
}
return line;
}));
});Come funziona
Quando il prossimo messaggio fallisce, send si risolve con 'failed', lo status del messaggio diventa 'failed' e message.retry() lo invia di nuovo. I messaggi successivi passano. Per rivedere l'errore ricarica la pagina (qui con Restart).
Altri due errori sono facili da provocare mentre sviluppi:
'not-allowed'arriva da un messaggio vuoto o da una domanda a cui si risponde due volte.connectToHost()senzafakeviene rifiutata subito con'no-host'in una pagina aperta da sola in una scheda.
Un mondo finto dice subito di sì e gli errori 'declined' e 'unavailable' di askFor arrivano perciò solo dalla webapp. Scrivi il codice che li gestisce con le ricette qui sotto e provalo nella webapp dicendo di no quando la webapp chiede. Per questa prova scrivi l'indirizzo della tua vista (anche http://localhost) nel campo Vista propria di un tuo mondo (Andare online).