A fake world that changes over time
A real world changes on its own (a clock runs, a ball flies, a score goes up) and a fake world does the same through functions in its data. Three forms make it change: (1) a function of time that gives a new value every second, (2) an { every, value } object that gives one at the interval you choose and (3) a variable in your file that a callback changes when the person acts and that one of these functions reads. In the example, press Shoot to score.
jsx
import { useWorld } from './useWorld.js';
// The fake world reads this variable, and the person's messages change it.
let goals = 0;
// A fake world that changes over time. Since `now` stays fixed for its whole life, what changes goes in `data`.
const match = {
name: 'Match',
now: { text: 'Second half' },
data: {
// A function of time (in milliseconds) gives a new value every second.
minute: (time) => 45 + (Math.floor(time / 1000) % 45),
// { every, value } gives a new value as often as you say, here every 100 ms.
ball: { every: 100, value: (time) => Math.round(50 + 45 * Math.sin(time / 400)) },
// The world reads the variable every 200 ms and changes when the person acts.
goals: { every: 200, value: () => goals },
},
onMessage: () => {
goals += 1;
},
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: match } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// Drawn again at every new value.
return (
<>
<p>{world.now?.text}, minute {world.data.minute}</p>
<div style={{ position: 'relative', height: 24, background: '#3c8d5a', borderRadius: 4 }}>
<div
style={{
position: 'absolute', top: 4, left: `calc(${world.data.ball}% - 8px)`,
width: 16, height: 16, borderRadius: '50%', background: 'white',
}}
/>
</div>
<p>Goals: {world.data.goals}</p>
<button onClick={() => world.send('shoot')}>Shoot</button>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The fake world reads this variable, and the person's messages change it.
let goals = 0;
// A fake world that changes over time. Since `now` stays fixed for its whole life, what changes goes in `data`.
const match = {
name: 'Match',
now: { text: 'Second half' },
data: {
// A function of time (in milliseconds) gives a new value every second.
minute: (time) => 45 + (Math.floor(time / 1000) % 45),
// { every, value } gives a new value as often as you say, here every 100 ms.
ball: { every: 100, value: (time) => Math.round(50 + 45 * Math.sin(time / 400)) },
// The world reads the variable every 200 ms and changes when the person acts.
goals: { every: 200, value: () => goals },
},
onMessage: () => {
goals += 1;
},
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: match } });
const minute = document.createElement('p');
const field = document.createElement('div');
field.style.cssText = 'position: relative; height: 24px; background: #3c8d5a; border-radius: 4px';
const ball = document.createElement('div');
ball.style.cssText = 'position: absolute; top: 4px; width: 16px; height: 16px; border-radius: 50%; background: white';
field.append(ball);
const score = document.createElement('p');
const shoot = document.createElement('button');
shoot.textContent = 'Shoot';
shoot.onclick = () => world.send('shoot');
document.body.append(minute, field, score, shoot);
// Called at once, then at every new value.
world.subscribe((node) => {
minute.textContent = `${node.now?.text}, minute ${node.data.minute}`;
ball.style.left = `calc(${node.data.ball}% - 8px)`;
score.textContent = `Goals: ${node.data.goals}`;
});How it works
- Every second the fake world calls
minute: (time) => ...with the time in milliseconds since the epoch (the value ofDate.now()) and your view receives the result as the new value ofminute. ball: { every: 100, value: (time) => ... }does the same every 100 milliseconds. Data this fast shows whether your view keeps up (Follow fast data without slowing down).goalsis a plain variable in the file thatonMessageincrements on every message of the person.value: () => goalsreads it every 200 milliseconds and in this way the world answers the person through its data.
now, on the other hand, keeps the value you wrote for the whole life of the fake world. Put what changes (whose turn it is, the score, the time left) in data. A real world delivers its data in data as well and the same code of your view therefore works in the webapp.