Skip to content

Commit 501b3b7

Browse files
committed
fix(overlay): keep the Angular Native web shims while any connection is pending
The temporary location and navigator were tracked per call, so when two connections overlapped the first to finish removed them while the second was still connecting. They are now shared: added when the first connection starts and removed only after the last one ends, and only if they are still the overlay's own objects.
1 parent 745516e commit 501b3b7

2 files changed

Lines changed: 50 additions & 21 deletions

File tree

‎packages/ng-devtools/src/__tests__/overlay-angular-native.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ describe('initAngularNativeOverlay', () => {
385385
log.mockRestore();
386386
});
387387

388+
it('keeps the stand-in location while another connection is still pending', async () => {
389+
const { withWebShims } = await import('../overlay-angular-native.ts');
390+
let finishFirst!: () => void;
391+
let finishSecond!: () => void;
392+
const first = withWebShims(
393+
'http://localhost:9999/',
394+
() => new Promise<void>((r) => (finishFirst = r)),
395+
);
396+
const second = withWebShims(
397+
'http://localhost:9999/',
398+
() => new Promise<void>((r) => (finishSecond = r)),
399+
);
400+
expect((g['location'] as { origin: string }).origin).toBe('http://localhost:9999');
401+
402+
finishFirst();
403+
await first;
404+
expect((g['location'] as { origin: string }).origin).toBe('http://localhost:9999');
405+
expect(typeof g['navigator']).not.toBe('undefined');
406+
407+
finishSecond();
408+
await second;
409+
expect(typeof g['location']).toBe('undefined');
410+
});
411+
388412
it('leaves a location the app already has untouched', async () => {
389413
const { root, ng } = fixture();
390414
g['ng'] = ng;

‎packages/ng-devtools/src/overlay-angular-native.ts‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -378,32 +378,37 @@ function withTextGlobal<T>(fn: () => T): T {
378378
* for the page's until `connectDevframe()` settles, and is removed again: Expo
379379
* loads split bundles from `location.origin` whenever a `location` exists.
380380
*/
381-
async function withWebShims<T>(baseURL: string, connect: () => Promise<T>): Promise<T> {
381+
let shimUsers = 0;
382+
let shims: Record<string, unknown> = {};
383+
384+
export async function withWebShims<T>(baseURL: string, connect: () => Promise<T>): Promise<T> {
382385
const g = globalThis as Record<string, unknown>;
383-
const added: string[] = [];
384-
if (typeof g['location'] === 'undefined') {
385-
const url = new URL(baseURL);
386-
added.push('location');
387-
g['location'] = {
388-
href: url.href,
389-
origin: url.origin,
390-
protocol: url.protocol,
391-
host: url.host,
392-
hostname: url.hostname,
393-
port: url.port,
394-
pathname: url.pathname,
395-
search: '',
396-
hash: '',
397-
};
398-
}
399-
if (typeof g['navigator'] === 'undefined') {
400-
added.push('navigator');
401-
g['navigator'] = { userAgent: TITLE };
386+
if (shimUsers++ === 0) {
387+
shims = {};
388+
if (typeof g['location'] === 'undefined') {
389+
const url = new URL(baseURL);
390+
shims['location'] = {
391+
href: url.href,
392+
origin: url.origin,
393+
protocol: url.protocol,
394+
host: url.host,
395+
hostname: url.hostname,
396+
port: url.port,
397+
pathname: url.pathname,
398+
search: '',
399+
hash: '',
400+
};
401+
}
402+
if (typeof g['navigator'] === 'undefined') shims['navigator'] = { userAgent: TITLE };
403+
for (const [key, value] of Object.entries(shims)) g[key] = value;
402404
}
403405
try {
404406
return await connect();
405407
} finally {
406-
for (const key of added) delete g[key];
408+
if (--shimUsers === 0) {
409+
for (const [key, value] of Object.entries(shims)) if (g[key] === value) delete g[key];
410+
shims = {};
411+
}
407412
}
408413
}
409414

0 commit comments

Comments
 (0)