Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/linking/LinkingHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,41 @@ describe('LinkingHandler', () => {
uut.configure(baseConfig);
expect(remove).toHaveBeenCalled();
});

it('ignores an initial URL resolved by a previous configuration', async () => {
let resolveInitialURL!: (url: string | null) => void;
mockLinking.getInitialURL
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveInitialURL = resolve;
})
)
.mockResolvedValueOnce(null);
const firstOnLink = jest.fn();
const secondOnLink = jest.fn();

uut.configure({ ...baseConfig, onLink: firstOnLink });
uut.setRootReady();
uut.configure({ ...baseConfig, onLink: secondOnLink });
resolveInitialURL('myapp://home');
await Promise.resolve();

expect(firstOnLink).not.toHaveBeenCalled();
expect(secondOnLink).not.toHaveBeenCalled();
});

it('continues handling URL events when the initial URL lookup fails', async () => {
mockLinking.getInitialURL.mockRejectedValueOnce(new Error('lookup failed'));
uut.configure(baseConfig);
uut.setRootReady();

await Promise.resolve();
await Promise.resolve();
urlListener?.({ url: 'myapp://home' });

expect(mockShowModal).toHaveBeenCalledTimes(1);
});
});

describe('setRootReady', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/linking/LinkingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class LinkingHandler {

private config: LinkingConfig | null = null;
private linkingSubscription: { remove: () => void } | null = null;
private subscriptionGeneration = 0;
private rootReady = false;
private userReadyOverride: boolean | null = null;

Expand Down Expand Up @@ -114,6 +115,7 @@ export class LinkingHandler {
}

public teardown(): void {
this.subscriptionGeneration++;
if (this.linkingSubscription) {
this.linkingSubscription.remove();
this.linkingSubscription = null;
Expand All @@ -124,13 +126,19 @@ export class LinkingHandler {
}

private subscribe(): void {
const subscriptionGeneration = this.subscriptionGeneration;
this.linkingSubscription = this.linkingAPI.addEventListener('url', (event) => {
this.handleURL(event.url);
});

this.linkingAPI.getInitialURL().then((url) => {
if (url) this.handleURL(url);
});
this.linkingAPI
.getInitialURL()
.then((url) => {
if (url && subscriptionGeneration === this.subscriptionGeneration) {
this.handleURL(url);
}
})
.catch(() => {});
}

private processURL(url: string): void {
Expand Down