world.dataHistory(name)
Returns the recent values of data[name] and the time at which each one arrived.
dataHistory(name: string): Promise<Array<{ value: unknown; time: number }>> | Array<{ value: unknown; time: number }>
js
const points = await world.dataHistory('score');Parameters
| Parameter | What it is |
|---|---|
name | A name of data such as 'score'. |
Returns
An array sorted from the oldest value to the newest. Each item is an object with two fields:
| Field | What it is |
|---|---|
value | The value in the same form as in data (a number, an array, a data URI, ...). |
time | The arrival time in Unix milliseconds. |
For a name absent from data the array is empty.
In the webapp the call returns a Promise that resolves to the array. With a fake world it returns the array directly. Always await the call so that the same code runs in both cases.
Usage
Draw a chart of a number
js
const points = await world.dataHistory('temperature');
const first = points[0]?.time ?? 0;
for (const { value, time } of points) {
plot((time - first) / 1000, value); // seconds since the first value
}Keep the chart going
dataHistory reads the history only once. Render it first and then append each new value that onData delivers:
js
const points = await world.dataHistory('temperature');
drawChart(points);
world.onData('temperature', (value) => {
points.push({ value, time: Date.now() });
drawChart(points);
});Caveats
- A copy taken at the time of the call. The array does not grow. Call
dataHistoryagain or follow the new values withonData. - Recent values only. The array holds at most the last 200 values of that name received by the webapp since the person entered the world.
- It sends nothing on behalf of the person. It only reads values already received by the webapp.
Troubleshooting
| Code | Why | What to do |
|---|---|---|
'failed' | The webapp could not run the call (rare). | Render from data alone without the history. |
| Problem | Why | What to do |
|---|---|---|
points.map is not a function | In the webapp the call returns a Promise. | await the call. |
| The array is empty. | The name is not in data or the world has sent no value yet. | Check the name in Object.keys(world.get().data). |
See also
world.onData(name, listener): each new value.- The recipe A chart of a number over time.