Skip to content

Show which action is waiting ​

The rules may wait for the other players or for the turn of the person before they run an action the person asked for. Mark such an action as waiting and disable its button until the rules move on. An action is waiting when somebody asked for it (isRequested) or when this person has just asked (act resolved with 'requested'). Press one of the actions in the example to see it wait.

jsx
import { useEffect, useState } from 'react';
import { useWorld } from './useWorld.js';

// A fake kitchen to build with. Its rules offer two actions.
const kitchen = {
	name: 'Kitchen',
	now: {
		text: 'Dinner is being planned.',
		actions: [
			{ name: 'cook', text: 'Start cooking' },
			{ name: 'pizza', text: 'Order pizza' },
		],
	},
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: kitchen } });
	const [asked, setAsked] = useState([]); // the actions this person asked for, here and now
	const nowText = world?.now?.text;

	// When the rules move on, what was asked before is done. Start the list again.
	useEffect(() => setAsked([]), [nowText]);

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	async function choose(name) {
		// The rules answer 'requested' when they have the request and will do it when they can.
		if ((await world.act(name)) === 'requested') setAsked((names) => [...names, name]);
	}

	return (
		<>
			<p>{world.now?.text}</p>
			<ul>
				{(world.now?.actions ?? []).map((action) => {
					// An action waits when somebody asked for it (`isRequested`), or when this person just did.
					const isWaiting = action.isRequested || asked.includes(action.name);
					return (
						<li key={action.name}>
							<button onClick={() => choose(action.name)} disabled={isWaiting}>{action.text ?? action.name}</button>
							{isWaiting && <i> (waiting for the rules)</i>}
						</li>
					);
				})}
			</ul>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake kitchen to build with. Its rules offer two actions.
const kitchen = {
	name: 'Kitchen',
	now: {
		text: 'Dinner is being planned.',
		actions: [
			{ name: 'cook', text: 'Start cooking' },
			{ name: 'pizza', text: 'Order pizza' },
		],
	},
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: kitchen } });

const now = document.createElement('p');
const list = document.createElement('ul');
document.body.append(now, list);

let asked = []; // the actions this person asked for, here and now
let shownNow = null;

function draw(node) {
	// When the rules move on, what was asked before is done. Start the list again.
	if (node.now?.text !== shownNow) asked = [];
	shownNow = node.now?.text;
	now.textContent = node.now?.text ?? '';
	list.replaceChildren(...(node.now?.actions ?? []).map((action) => {
		// An action waits when somebody asked for it (`isRequested`), or when this person just did.
		const isWaiting = action.isRequested || asked.includes(action.name);
		const item = document.createElement('li');
		const button = document.createElement('button');
		button.textContent = action.text ?? action.name;
		button.disabled = isWaiting;
		button.onclick = async () => {
			// The rules answer 'requested' when they have the request and will do it when they can.
			if ((await world.act(action.name)) === 'requested') asked = [...asked, action.name];
			draw(world.get());
		};
		item.append(button);
		if (isWaiting) {
			const waiting = document.createElement('i');
			waiting.textContent = ' (waiting for the rules)';
			item.append(waiting);
		}
		return item;
	}));
}

world.subscribe(draw);
Live, on a fake world
Open

How it works ​

action.isRequested is true when somebody (this person or someone else) already asked for that action. From the moment act resolves with 'requested', the view keeps the name of the action in asked. The fake world of the example records no requests (it never has isRequested: true) and in the example only this list marks the action as waiting. The view empties asked when a new now.text shows that the rules moved on and the actions may have changed. Until then the button stays disabled because a second request would add nothing.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.