Snapshot
- Type
- With Generics
interface Snapshot {
getResponse(endpoint, ...args) => { data, expiryStatus, expiresAt };
getError(endpoint, ...args) => ErrorTypes | undefined;
fetchedAt: number;
}
import type { ErrorTypes } from './ErrorTypes.js';
import type { EndpointInterface } from './interface.js';
import type { DenormalizeNullable } from './normal.js';
export interface SnapshotInterface {
getResponse: <
E extends Pick<EndpointInterface, 'key' | 'schema' | 'invalidIfStale'>,
Args extends readonly [...Parameters<E['key']>],
>(
endpoint: E,
...args: Args
) => {
data: DenormalizeNullable<E['schema']>;
expiryStatus: ExpiryStatusInterface;
expiresAt: number;
};
getError: <
E extends Pick<EndpointInterface, 'key'>,
Args extends readonly [...Parameters<E['key']>],
>(
endpoint: E,
...args: Args
) => ErrorTypes | undefined;
readonly fetchedAt: number;
}
// looser version to allow for cross-package version compatibility
export type ExpiryStatusInterface = 1 | 2 | 3;
tip
Use Controller.snapshot() to construct a snapshot
Snapshots passed to user-defined function that are used to compute state updates. These allow safe and performant access to the denormalized data based on the current state.
getResponse(endpoint, ...args)
returns
{
data: DenormalizeNullable<E['schema']>;
expiryStatus: ExpiryStatus;
expiresAt: number;
}
Gets the (globally referentially stable) response for a given endpoint/args pair from state given.
data
The denormalize response data. Guarantees global referential stability for all members.
expiryStatus
export enum ExpiryStatus {
Invalid = 1,
InvalidIfStale,
Valid,
}
Valid
- Will never suspend.
- Might fetch if data is stale
InvalidIfStale
- Will suspend if data is stale.
- Might fetch if data is stale
Invalid
- Will always suspend
- Will always fetch
expiresAt
A number representing time when it expires. Compare to Date.now().
getError(endpoint, ...args)
Gets the error, if any, for a given endpoint. Returns undefined for no errors.
fetchedAt
When the fetch was called that resulted in this snapshot.