StatisticStateExtensionOptions <StateExtension, PersistedStateExtension>
Index
Properties
Properties
optionaldefaultState
The values the fields start with, and the ones Statistics.reset restores. A plain value is
deep-copied with structuredClone each time it is used; pass a factory for a state structuredClone cannot
rebuild.
Can be omitted when deserialize supplies its own defaults, which is then the single place the fields are
declared - see the example on deserialize.
optionaldeserialize
Rebuilds the custom fields from the persisted record, and the place to validate them before trusting them.
Receives the whole record, so it has to supply a value for every field - .default() in a schema, or
defaultState alongside a conversion that copes with
a missing field itself.
const statistics = new Statistics({
stateExtension: { deserialize: z.object({ productsFound: z.number().default(0) }) },
});
Without it, the declared fields are restored as they were persisted - which is a record off the key-value
store taken at its word, productsFound included in whatever type it happens to hold.
A conversion that rejects the record costs the custom fields their persisted values (they start from the defaults, with a warning) and nothing else.
optionalserialize
Converts the custom fields to the JSON-serializable form they are persisted in. Not needed for fields that
already are one - pair it with deserialize for the fields that are not.
How the custom fields of StatisticsOptions.stateExtension are initialized and converted to and from the persisted record - the same three things RecoverableStateOptions asks for, scoped to the custom half of the statistics state.