Skip to content

Commit 8f633cd

Browse files
committed
fix(overlay): gate the injector profiler after the router publishes ng
An app.config.ts imported above the Angular Native overlay runs provideRouter() first, which creates ng without getComponent. The gate returned early on any ng, so the platform was created without the injector profiler. It now gates on that ng too and waits for getComponent on the same object.
1 parent f5db025 commit 8f633cd

4 files changed

Lines changed: 50 additions & 19 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ Angular creates a `signalStore` the first time something injects it. Open a page
288288

289289
Use `provideStore()` or `StoreModule.forRoot()`. The overlay stops looking after a few tries, so if you provide the Store late, reload the page.
290290

291+
### State changed in place
292+
293+
The tab rereads a store only when its state object is a new one, as NgRx creates on every change. Code that changes a nested object in place, without `patchState` or a reducer, leaves the tab on the old values until the next real change.
294+
291295
### Redaction
292296

293297
The devtools replace state keys with secret-looking names with `[redacted]`, at any depth. A signal store method call that writes such a key shows every argument as `[redacted]` in the change log, and so do later calls of that method. An argument whose parameter name looks secret, such as `password` in `login(email, password)`, is masked too. See [what the devtools redact](../security.md#components-signals-ngrx-and-pipes).

‎packages/devtools/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ What remains: `/config` exports `DEFAULT_POPUP_HOTKEY`, `PANGULAR_ACTIONS`, `PAN
181181

182182
- Pages push deltas instead of full snapshots, at most about once a second, and idle pages send only a small heartbeat.
183183
- NgRx store updates no longer slow down as the store grows (#432).
184-
- The NgRx collector serializes a signal store or the classic `Store` only when its state, computed values or method stats changed, and the overlay compares a report version instead of the serialized report, so an unchanged store costs no serialization on each check (#368).
184+
- The NgRx collector serializes a signal store or the classic `Store` only when its state, computed values or method stats changed, and the overlay compares a report version instead of the serialized report, so an unchanged store costs no serialization on each check. A change made in place, without a new state object, shows at the next real change (#368).
185185
- Collectors schedule their work outside the Angular zone, so an app that writes a signal or logs during change detection settles again on zone.js.
186186
- Long lists and trees in the panel render only the visible rows.
187187
- The agent tool list is much smaller: shorter descriptions, and library tools only for apps that use them.

‎packages/devtools/src/__tests__/injector-profiler-gate.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ describe('gateInjectorProfiler', () => {
4848
expect((g['ng'] as Record<string, unknown>)['ɵgetLoadedRoutes']).toBeTypeOf('function');
4949
});
5050

51+
it('gates when the router published onto ng before the gate ran', () => {
52+
const ng: Record<string, unknown> = { ɵgetLoadedRoutes: () => [] };
53+
g['ng'] = ng;
54+
gateInjectorProfiler();
55+
expect(g['window']).toBe(globalThis);
56+
expect(bootAngular().profiler).toBe(true);
57+
expect('window' in g).toBe(false);
58+
expect(g['ng']).toBe(ng);
59+
expect(ng['getComponent']).toBeTypeOf('function');
60+
});
61+
5162
it('removes window when ng arrives with the utilities already on it', () => {
5263
gateInjectorProfiler();
5364
const getComponent = () => null;

‎packages/devtools/src/injector-profiler-gate.ts‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,41 @@
88
* than the `ng` object itself because the router publishes its own utilities
99
* onto that object earlier, while `provideRouter()` evaluates.
1010
*
11-
* Does nothing when `window` or `ng` already exists: the profiler is wired, or
12-
* the platform was created without it and it is too late.
11+
* Does nothing when `window` exists, or when `ng` already holds the core
12+
* utilities: the profiler is wired, or the platform was created without it and
13+
* it is too late. An `ng` that holds only the router's utilities still gates.
1314
*/
1415
export function gateInjectorProfiler(): void {
1516
const g = globalThis as Record<string, unknown>;
16-
if ('window' in g || 'ng' in g) return;
17+
if ('window' in g) return;
18+
const existing = g['ng'];
19+
// Core has published its utilities, or something else owns `ng`: too late.
20+
if (existing !== undefined && (!isObject(existing) || 'getComponent' in existing)) return;
1721
Object.defineProperty(g, 'window', { configurable: true, get: () => g });
1822
const removeWindow = () => {
1923
if (Object.getOwnPropertyDescriptor(g, 'window')?.get) delete g['window'];
2024
};
25+
const awaitCoreUtils = (ng: Record<string, unknown>) => {
26+
Object.defineProperty(ng, 'getComponent', {
27+
configurable: true,
28+
enumerable: true,
29+
get: () => undefined,
30+
set(fn: unknown) {
31+
Object.defineProperty(ng, 'getComponent', {
32+
configurable: true,
33+
enumerable: true,
34+
writable: true,
35+
value: fn,
36+
});
37+
removeWindow();
38+
},
39+
});
40+
};
41+
// The router ran first (an `app.config.ts` imported before this module).
42+
if (isObject(existing)) {
43+
awaitCoreUtils(existing);
44+
return;
45+
}
2146
Object.defineProperty(g, 'ng', {
2247
configurable: true,
2348
enumerable: true,
@@ -30,28 +55,19 @@ export function gateInjectorProfiler(): void {
3055
value,
3156
});
3257
// Not Angular's empty object, or one that already holds the core utilities.
33-
if (!value || typeof value !== 'object' || 'getComponent' in value) {
58+
if (!isObject(value) || 'getComponent' in value) {
3459
removeWindow();
3560
return;
3661
}
37-
Object.defineProperty(value, 'getComponent', {
38-
configurable: true,
39-
enumerable: true,
40-
get: () => undefined,
41-
set(fn: unknown) {
42-
Object.defineProperty(value, 'getComponent', {
43-
configurable: true,
44-
enumerable: true,
45-
writable: true,
46-
value: fn,
47-
});
48-
removeWindow();
49-
},
50-
});
62+
awaitCoreUtils(value);
5163
},
5264
});
5365
}
5466

67+
function isObject(value: unknown): value is Record<string, unknown> {
68+
return !!value && typeof value === 'object';
69+
}
70+
5571
/**
5672
* The gate for Angular Native, run as its overlay module loads. The overlay
5773
* itself starts after `mount()`, once the platform exists, but `src/main.ts`

0 commit comments

Comments
 (0)