Skip to content

Commit 50e3359

Browse files
committed
PR feedback
1 parent 6e8f508 commit 50e3359

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

packages-exp/auth-exp/src/platform_cordova/popup_redirect/popup_redirect.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ describe('platform_cordova/popup_redirect/popup_redirect', () => {
118118

119119
afterEach(() => {
120120
universalLinksCb = null;
121+
const win = window as unknown as Record<string, unknown>;
122+
delete win.universalLinks;
123+
delete win.BuildInfo;
121124
});
122125

123126
function event(manager: EventManager): Promise<AuthEvent> {
@@ -264,6 +267,17 @@ describe('platform_cordova/popup_redirect/popup_redirect', () => {
264267
`${PACKAGE_NAME}://foo`
265268
);
266269
});
270+
271+
it('calls the dev existing handleOpenUrl function for other package', async () => {
272+
const oldHandleOpenUrl = sinon.stub();
273+
window.handleOpenUrl = oldHandleOpenUrl;
274+
275+
await resolver._initialize(auth);
276+
handleOpenUrl(`${NOT_PACKAGE_NAME}://foo`);
277+
expect(oldHandleOpenUrl).to.have.been.calledWith(
278+
`${NOT_PACKAGE_NAME}://foo`
279+
);
280+
});
267281
});
268282
});
269283

packages-exp/auth-exp/src/platform_cordova/popup_redirect/popup_redirect.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const INITIAL_EVENT_TIMEOUT_MS = 500;
4747

4848
/** Custom AuthEventManager that adds passive listeners to events */
4949
export class CordovaAuthEventManager extends AuthEventManager {
50-
private readonly passiveListeners: Set<(e: AuthEvent) => void> = new Set();
50+
private readonly passiveListeners = new Set<(e: AuthEvent) => void>();
5151

5252
addPassiveListener(cb: (e: AuthEvent) => void): void {
5353
this.passiveListeners.add(cb);
@@ -73,18 +73,19 @@ export class CordovaAuthEventManager extends AuthEventManager {
7373

7474
class CordovaPopupRedirectResolver implements PopupRedirectResolver {
7575
readonly _redirectPersistence = browserSessionPersistence;
76-
private readonly eventManagers: Record<string, CordovaAuthEventManager> = {};
76+
private readonly eventManagers = new Map<string, CordovaAuthEventManager>();
7777

7878
_completeRedirectFn: () => Promise<null> = async () => null;
7979

8080
async _initialize(auth: Auth): Promise<CordovaAuthEventManager> {
8181
const key = auth._key();
82-
if (!this.eventManagers[key]) {
83-
const manager = new CordovaAuthEventManager(auth);
84-
this.eventManagers[key] = manager;
82+
let manager = this.eventManagers.get(key);
83+
if (!manager) {
84+
manager = new CordovaAuthEventManager(auth);
85+
this.eventManagers.set(key, manager);
8586
this.attachCallbackListeners(auth, manager);
8687
}
87-
return this.eventManagers[key];
88+
return manager;
8889
}
8990

9091
_openPopup(auth: Auth): Promise<AuthPopup> {
@@ -111,21 +112,11 @@ class CordovaPopupRedirectResolver implements PopupRedirectResolver {
111112
}
112113

113114
private attachCallbackListeners(auth: Auth, manager: AuthEventManager): void {
114-
const noEvent: AuthEvent = {
115-
type: AuthEventType.UNKNOWN,
116-
eventId: null,
117-
sessionId: null,
118-
urlResponse: null,
119-
postBody: null,
120-
tenantId: null,
121-
error: _createError(AuthErrorCode.NO_AUTH_EVENT)
122-
};
123-
124115
const noEventTimeout = setTimeout(async () => {
125116
// We didn't see that initial event. Clear any pending object and
126117
// dispatch no event
127118
await _getAndRemoveEvent(auth);
128-
manager.onEvent(noEvent);
119+
manager.onEvent(generateNoEvent());
129120
}, INITIAL_EVENT_TIMEOUT_MS);
130121

131122
const universalLinksCb = async (
@@ -135,14 +126,20 @@ class CordovaPopupRedirectResolver implements PopupRedirectResolver {
135126
clearTimeout(noEventTimeout);
136127

137128
const partialEvent = await _getAndRemoveEvent(auth);
138-
let finalEvent: AuthEvent | null = noEvent;
139-
// Start with the noEvent
129+
let finalEvent: AuthEvent | null = null;
140130
if (partialEvent && eventData?.['url']) {
141131
finalEvent = _eventFromPartialAndUrl(partialEvent, eventData['url']);
142132
}
143-
manager.onEvent(finalEvent || noEvent);
133+
134+
// If finalEvent is never filled, trigger with no event
135+
manager.onEvent(finalEvent || generateNoEvent());
144136
};
145137

138+
// Universal links subscriber doesn't exist for iOS, so we need to check
139+
if (typeof universalLinks.subscribe === 'function') {
140+
universalLinks.subscribe(null, universalLinksCb);
141+
}
142+
146143
// iOS 7 or 8 custom URL schemes.
147144
// This is also the current default behavior for iOS 9+.
148145
// For this to work, cordova-plugin-customurlscheme needs to be installed.
@@ -169,11 +166,6 @@ class CordovaPopupRedirectResolver implements PopupRedirectResolver {
169166
}
170167
}
171168
};
172-
173-
// Universal links subscriber doesn't exist for iOS, so we need to check
174-
if (typeof universalLinks.subscribe === 'function') {
175-
universalLinks.subscribe(null, universalLinksCb);
176-
}
177169
}
178170
}
179171

@@ -184,3 +176,15 @@ class CordovaPopupRedirectResolver implements PopupRedirectResolver {
184176
* @public
185177
*/
186178
export const cordovaPopupRedirectResolver: externs.PopupRedirectResolver = CordovaPopupRedirectResolver;
179+
180+
function generateNoEvent(): AuthEvent {
181+
return {
182+
type: AuthEventType.UNKNOWN,
183+
eventId: null,
184+
sessionId: null,
185+
urlResponse: null,
186+
postBody: null,
187+
tenantId: null,
188+
error: _createError(AuthErrorCode.NO_AUTH_EVENT)
189+
};
190+
}

0 commit comments

Comments
 (0)