world.act(name, args?)
On behalf of the person, asks the rules of the world to run one of the actions they offer at this moment.
act(name: string, args?: Record<string, unknown>): Promise<'requested' | 'refused' | 'not-allowed'>
js
const result = await world.act('vote');Parameters
| Parameter | What it is |
|---|---|
name | The name of an action in now.actions. |
args | Optional arguments for an action that takes them. Each action in now.actions lists its arguments in its own args. |
Returns
A Promise that resolves to one of these results:
| Result | What it means |
|---|---|
'requested' | The rules accepted the request. Its effect arrives later in the world as a new now, a message or new data. |
'refused' | The rules turned the request down (the action takes no requests now) or did not answer within 10 seconds. |
'not-allowed' | Nothing was asked because the action is not among the current now.actions. |
Usage
Offer the actions as buttons
now.actions holds the actions currently offered by the rules. Render a button for each action whose canChoose is not false and call act when the button is clicked:
js
world.subscribe((node) => {
const actions = (node.now?.actions ?? []).filter((action) => action.canChoose !== false);
buttons.replaceChildren(...actions.map((action) => {
const button = document.createElement('button');
button.textContent = action.text ?? action.name;
button.disabled = action.isRequested;
button.onclick = () => world.act(action.name);
return button;
}));
});Try it with a fake world
A fake world with now.actions and an onAction callback answers every request:
js
const world = await connectToHost({
fake: {
world: {
name: 'Council',
now: { text: 'Voting is open', actions: [{ name: 'vote', text: 'Vote now' }] },
onAction: (name) => `You chose ${name}`,
},
},
});
await world.act('vote'); // 'requested'; then the message "You chose vote" arrivesCaveats
'requested'is not "done". The rules decide what happens and when. Render the effect only when it reaches yoursubscribelistener.- It can take up to 10 seconds to resolve because it waits for the answer of the rules.
- Offer only what the person may choose. An action with
canChoose: falsehappens on its own and one withisRequested: truehas already been asked for by somebody. - It acts on behalf of the person. The rules treat every request as a choice made by the person. Call
actonly when the person chooses the action. - A fake world never refuses.
actresolves'requested'for every action in itsnow.actionsand itsnownever changes from what you wrote. Keep the state that changes indata(Fake worlds).
Troubleshooting
| Code | Why | What to do |
|---|---|---|
'failed' | The webapp could not run the call (rare). | Show an error and let the person try again. |
'refused' and 'not-allowed' are not errors. The Promise resolves with them as results. Either one usually means that the world changed after your view rendered its buttons. Render them again from the newest now.actions.
See also
- The
nowof the world: what is happening and the actions on offer. - The actions the rules offer with the recipes Offer the actions of the rules as buttons and Show which action is waiting.