Skip to content

Commit 6728488

Browse files
committed
fix(store): cap an NgRx state whose small fields still don't fit
A state with many small top-level fields had no field larger than its marker, so nothing was replaced and the state was stored uncapped. When the fields still exceed the cap after the loop, store the marker for the whole state, under a [state] field for a signal store so it stays a record.
1 parent 431c339 commit 6728488

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎apps/docs/src/content/inspectors/ngrx-store.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ The log keeps the last 200 entries, and the page keeps the states of the same en
314314

315315
### State size
316316

317-
The server keeps at most 256 KB of JSON for each store's state: the classic Store's state, and the state and computed fields of each signal store. Past that, the largest top-level slices or fields show `[too large: N KB]` instead of their value, until the rest fits. The smaller slices stay readable. The **Store** tab, the `pangular:ngrx-inspect-store` tool and the `pangular:ngrx-store` resource all read the capped state.
317+
The server keeps at most 256 KB of JSON for each store's state: the classic Store's state, and the state and computed fields of each signal store. Past that, the largest top-level slices or fields show `[too large: N KB]` instead of their value, until the rest fits. The smaller slices stay readable. If the state still doesn't fit, such as one with thousands of small fields, the whole state shows the marker, under a `[state]` field for a signal store. The **Store** tab, the `pangular:ngrx-inspect-store` tool and the `pangular:ngrx-store` resource all read the capped state.
318318

319319
### Past states assume immutable state
320320

‎packages/devtools/src/__tests__/ngrx-size-cap.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
22
import type { NgrxPageReport } from '../ngrx-shared.ts';
33
import {
44
NGRX_STATE_MAX_BYTES,
5+
NGRX_TOO_LARGE_FIELD,
56
capNgrxFields,
67
capNgrxState,
78
tooLargeText,
@@ -54,7 +55,7 @@ describe('NgRx state size cap', () => {
5455
const fn = () => undefined;
5556
const capped = capNgrxState(
5657
{ a: 'x'.repeat(30), b: 1, c: fn, d: 'y'.repeat(30) },
57-
10,
58+
60,
5859
) as Record<string, unknown>;
5960
// The 30-character strings shrink to the marker; the number and the function stay.
6061
expect(capped['a']).toBe('[too large: 1 KB]');
@@ -63,6 +64,13 @@ describe('NgRx state size cap', () => {
6364
expect(capped['c']).toBe(fn);
6465
});
6566

67+
it('turns a state of many small fields that still does not fit into the marker', () => {
68+
const many = Object.fromEntries(Array.from({ length: 30_000 }, (_, i) => [`k${i}`, i]));
69+
const capped = capNgrxState(many);
70+
expect(capped).toMatch(/^\[too large: \d+ KB\]$/);
71+
expect(capNgrxFields(many)).toEqual({ [NGRX_TOO_LARGE_FIELD]: capped });
72+
});
73+
6674
it('caps the fields of a signal store as one state', () => {
6775
expect(capNgrxFields({ a: big(200), b: big(100) })).toEqual({
6876
a: '[too large: 201 KB]',

‎packages/devtools/src/rpc/ngrx-size-cap.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ const isPlainRecord = (value: unknown): value is Record<string, unknown> =>
2525
* Caps one stored NgRx state at `max` bytes of JSON. A state under the cap is
2626
* returned as it is. For an object, the largest top-level slices are replaced
2727
* with `[too large: N KB]` until the rest fits, so the small slices stay
28-
* readable. Any other value past the cap becomes the marker itself.
28+
* readable. Any other value past the cap becomes the marker itself, and so
29+
* does an object that still doesn't fit, such as one with many small fields.
2930
*/
3031
export function capNgrxState(value: unknown, max = NGRX_STATE_MAX_BYTES): unknown {
31-
let total = jsonBytes(value);
32+
const whole = jsonBytes(value);
33+
let total = whole;
3234
if (total <= max) return value;
33-
if (!isPlainRecord(value)) return tooLargeText(total);
35+
if (!isPlainRecord(value)) return tooLargeText(whole);
3436
const out: Record<string, unknown> = { ...value };
3537
const sizes = Object.keys(out)
3638
.map((key) => ({ key, bytes: jsonBytes(out[key]) }))
@@ -44,16 +46,23 @@ export function capNgrxState(value: unknown, max = NGRX_STATE_MAX_BYTES): unknow
4446
out[key] = marker;
4547
total -= saved;
4648
}
47-
return out;
49+
return total <= max ? out : tooLargeText(whole);
4850
}
4951

52+
/** The field a signal store's state holds when even its fields together don't fit. */
53+
export const NGRX_TOO_LARGE_FIELD = '[state]';
54+
5055
/**
5156
* Caps a signal store's fields as one state: the largest fields give way to
52-
* the marker until the whole record fits.
57+
* the marker until the whole record fits. When they never fit, the record
58+
* holds only `[state]` with the marker, so it stays a record.
5359
*/
5460
export function capNgrxFields(
5561
fields: Record<string, unknown>,
5662
max = NGRX_STATE_MAX_BYTES,
5763
): Record<string, unknown> {
58-
return capNgrxState(fields, max) as Record<string, unknown>;
64+
const capped = capNgrxState(fields, max);
65+
return typeof capped === 'string'
66+
? { [NGRX_TOO_LARGE_FIELD]: capped }
67+
: (capped as Record<string, unknown>);
5968
}

0 commit comments

Comments
 (0)