$sessionStorage
Store temporary state for this instrument instance during the current trip, such as observed peak speed, a counter or a previous calculation. The store is cleared when the trip ends.
Storage scope
Here, “session” means the current trip. The same instrument can retrieve saved values across repeated calls to main() during that trip, making the store useful for peaks, counters and other state that spans drawing updates.
Each instrument instance has its own store. Two copies of an instrument on the dashboard maintain independent state.
When to keep and when to clear
| Situation | Storage behavior |
|---|---|
| Same trip and the same continuously running instrument instance | Values are retained between calls to main(). |
| The trip ends, or the app leaves the recording state | The store is cleared. With no active trip, get returns null and set does not retain writes. |
| A new trip starts | The store starts empty and does not inherit the previous trip’s values. |
| The script reloads, its drawing instance is destroyed, or the app restarts | Temporary values are lost and are not restored, even if the same trip continues. |
| Editor preview | Preview has its own temporary store. Its values are not carried into live operation. |
The store contains only state observed or calculated while that instrument was running. Opening an instrument partway through a trip does not recover earlier peak values. Use another storage mechanism for data that must be retained permanently.
Reading and writing objects and arrays
Both get and set use data copies. For example, if you take out an array and add elements to it, the original array in storage will not change, and you need to call set again to save the changes.
Key must be a string. Can hold finite numbers, strings, Boolean values, null, and ordinary objects and arrays composed of these values. Functions, View, circular references, and special objects cannot be saved. After setting a key to null, read the key to get null; the current interface provides two methods, get and set.
Example: Top speed observed during this trip
Two Basic data inputs are defined in Parameter configuration: the speed with identifier speed, and the recording status with identifier recording. A horizontal line is displayed when there is no active trip; each run compares the current speed with the saved peak value, and then saves the larger value. Peak values are saved in km/h and converted according to the current unit when displayed to avoid mixing units.
Missing readings preserve the observed peak. A valid zero participates normally; the next trip starts with no stored value.
function main() {
if ($data.current.recording !== true) {
return (
<Text fontSize={48} color={$env.foreground}>
—
</Text>
);
}
const speed = $data.current.speed;
const saved = $sessionStorage.get('peakKmh');
let peak = typeof saved === 'number' ? saved : null;
if (typeof speed?.value === 'number') {
const kmh = speed.unit === 'mph' ? speed.value * 1.609344 : speed.value;
peak = peak == null ? kmh : Math.max(peak, kmh);
$sessionStorage.set('peakKmh', peak);
}
const unit = speed?.unit ?? $env.speedUnit;
const value =
peak == null ? '—' : Math.round(unit === 'mph' ? peak / 1.609344 : peak);
return (
<VStack spacing={6}>
<Text fontSize={48} color={$env.foreground}>
{value}
</Text>
<Text color={$env.accent}>{unit}</Text>
</VStack>
);
}
Capacity limits
Each store has a maximum of 256 keys, with a total data size of approximately 1 MiB; a single key has a maximum of 256 UTF-8 bytes. There are also size and nesting limits on copying of values. If the limit is exceeded or an unsupported value is passed in, an error will be reported, and failed writing will not overwrite the original valid value.
Suitable for saving a small amount of statistical status. Appending the array frame by frame for a long time will continue to grow. The curve should use $data.history first, or limit the array length by yourself.
Method
get(key: string)unknown | nullRead the value saved by this instrument instance during the current trip. Returns null if the key does not exist; objects and arrays are returned as independent copies.
set(key: string, value: unknown)voidSave or overwrite the specified key with a copy of the value. Writes are not retained when there is no active trip. Returns undefined.