Skip to content

Commit d9b53b0

Browse files
callback changes no need to init
1 parent f8b39dd commit d9b53b0

File tree

4 files changed

+127
-101
lines changed

4 files changed

+127
-101
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="shortcut icon" href="images/favicon.png" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<head>
9+
<script src="https://cdn.integ.uidapi.com/uid2-sdk-3.10.0.js"></script>
10+
<script
11+
type="text/javascript"
12+
src="https://imasdk.googleapis.com/js/sdkloader/ima3.js"
13+
></script>
14+
</head>
15+
<!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> -->
16+
17+
<title>UID2 Publisher Client-Side React Integration Example</title>
18+
</head>
19+
<body>
20+
<noscript>You need to enable JavaScript to run this app.</noscript>
21+
<div id="root"></div>
22+
</body>
23+
</html>

src/callbackManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export class CallbackManager {
5353
public runCallbacks(event: EventType, payload: CallbackPayload) {
5454
if (event === EventType.InitCompleted) this._sentInit = true;
5555
if (event === EventType.SdkLoaded) CallbackManager._sentSdkLoaded[this._productName] = true;
56-
if (!this._sentInit && event !== EventType.SdkLoaded) return;
5756

5857
const enrichedPayload = {
5958
...payload,

src/integrationTests/options.test.ts

Lines changed: 97 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -555,122 +555,123 @@ describe('Store config UID2', () => {
555555
expect(storageConfig).toBeNull();
556556
});
557557
});
558+
});
558559

559-
describe('calls the NoIdentityAvailable event', () => {
560-
let handler: ReturnType<typeof jest.fn>;
561-
beforeEach(() => {
562-
handler = jest.fn();
563-
uid2.callbacks.push(handler);
564-
});
560+
describe('calls the NoIdentityAvailable event', () => {
561+
let handler: ReturnType<typeof jest.fn>;
562+
beforeEach(() => {
563+
handler = jest.fn();
564+
uid2.callbacks.push(handler);
565+
});
565566

566-
test('when init is called for the first time with no identity', () => {
567-
uid2.init({});
568-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
567+
test('when init is called for the first time with no identity', () => {
568+
uid2.init({});
569+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
570+
});
571+
test('when init is already complete and called again with no identity', () => {
572+
uid2.init({});
573+
uid2.init({});
574+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
575+
});
576+
test('when init is already complete and called again with an expired identity', () => {
577+
uid2.init({});
578+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
579+
let expiredIdentity = makeIdentity({
580+
identity_expires: Date.now() - 100000,
581+
refresh_expires: Date.now() - 100000,
569582
});
570-
test('when init is already complete and called again with no identity', () => {
571-
uid2.init({});
572-
uid2.init({});
573-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
583+
uid2.init({
584+
identity: expiredIdentity,
574585
});
575-
test('when init is already complete and called again with an expired identity', () => {
576-
uid2.init({});
577-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
578-
let expiredIdentity = makeIdentity({
579-
identity_expires: Date.now() - 100000,
580-
refresh_expires: Date.now() - 100000,
581-
});
582-
uid2.init({
583-
identity: expiredIdentity,
584-
});
585-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
586-
identity: expiredIdentity,
587-
});
586+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
587+
identity: expiredIdentity,
588588
});
589-
test('when init is already complete but the existing identity is expired', () => {
590-
let expiredIdentity = makeIdentity({
591-
identity_expires: Date.now() - 100000,
592-
refresh_expires: Date.now() - 100000,
593-
});
594-
uid2.init({
595-
identity: expiredIdentity,
596-
});
597-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
598-
identity: expiredIdentity,
599-
});
600-
uid2.init({});
601-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
602-
identity: expiredIdentity,
603-
});
589+
});
590+
test('when init is already complete but the existing identity is expired', () => {
591+
let expiredIdentity = makeIdentity({
592+
identity_expires: Date.now() - 100000,
593+
refresh_expires: Date.now() - 100000,
604594
});
605-
test('when identity is expired but refreshable', () => {
606-
let expiredRefreshableIdentity = makeIdentity({
607-
identity_expires: Date.now() - 10000,
608-
refresh_expires: Date.now() + 10000,
609-
});
610-
uid2.setIdentity(expiredRefreshableIdentity);
611-
612-
// in this case, identity is temporarily unavailable but still unavailable
613-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
614-
identity: null,
615-
});
595+
uid2.init({
596+
identity: expiredIdentity,
616597
});
617-
test('when login is required', () => {
618-
uid2.isLoginRequired();
598+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
599+
identity: null,
600+
});
601+
uid2.init({});
602+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
603+
identity: expiredIdentity,
604+
});
605+
});
606+
test('when identity is expired but refreshable', () => {
607+
let expiredRefreshableIdentity = makeIdentity({
608+
identity_expires: Date.now() - 10000,
609+
refresh_expires: Date.now() + 10000,
610+
});
611+
uid2.init({ identity: expiredRefreshableIdentity });
619612

620-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
621-
identity: null,
622-
});
613+
// in this case, identity is temporarily unavailable but still unavailable
614+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
615+
identity: null,
623616
});
624-
test('when get identity returns null or get advertising token returns undefined', () => {
625-
const nullIdentity = uid2.getIdentity();
617+
});
618+
test('when login is required', () => {
619+
uid2.isLoginRequired();
626620

627-
expect(nullIdentity).toBeNull();
628-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
629-
identity: null,
630-
});
621+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
622+
identity: null,
631623
});
632-
test('when there is no advertising token', () => {
633-
const token = uid2.getAdvertisingToken();
624+
});
625+
test('when get identity returns null or get advertising token returns undefined', () => {
626+
const nullIdentity = uid2.getIdentity();
634627

635-
expect(token).toBeUndefined();
636-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
637-
identity: null,
638-
});
628+
expect(nullIdentity).toBeNull();
629+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
630+
identity: null,
639631
});
640632
});
633+
test('when there is no advertising token', () => {
634+
const token = uid2.getAdvertisingToken();
641635

642-
describe('does not call NoIdentityAvailable event', () => {
643-
let handler: ReturnType<typeof jest.fn>;
644-
beforeEach(() => {
645-
handler = jest.fn();
646-
uid2.callbacks.push(handler);
636+
expect(token).toBeUndefined();
637+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
638+
identity: null,
647639
});
640+
});
641+
});
648642

649-
test('when setIdentity is run with a valid identity, should not call NoIdentityAvailable on set or get', () => {
650-
uid2.init({});
651-
let validIdentity = makeIdentity();
652-
uid2.setIdentity(validIdentity);
653-
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
654-
identity: null,
655-
});
643+
describe('does not call NoIdentityAvailable event', () => {
644+
let handler: ReturnType<typeof jest.fn>;
645+
beforeEach(() => {
646+
handler = jest.fn();
647+
uid2.callbacks.push(handler);
648+
});
656649

657-
uid2.getIdentity();
658-
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
659-
identity: null,
660-
});
650+
test('when setIdentity is run with a valid identity, should not call NoIdentityAvailable on set or get', () => {
651+
uid2.init({});
652+
handler = jest.fn();
653+
let validIdentity = makeIdentity();
654+
uid2.setIdentity(validIdentity);
655+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
656+
identity: null,
657+
});
661658

662-
uid2.getAdvertisingToken();
663-
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
664-
identity: null,
665-
});
659+
uid2.getIdentity();
660+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
661+
identity: null,
666662
});
667-
test('when identity is set with opted out identity', () => {
668-
uid2.init({});
669-
let optedOutIdentity = makeIdentity({ status: 'optout' });
670-
uid2.setIdentity(optedOutIdentity);
671-
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
672-
identity: null,
673-
});
663+
664+
uid2.getAdvertisingToken();
665+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
666+
identity: null,
667+
});
668+
});
669+
test('when identity is set with opted out identity', () => {
670+
uid2.init({});
671+
let optedOutIdentity = makeIdentity({ status: 'optout' });
672+
uid2.setIdentity(optedOutIdentity);
673+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
674+
identity: null,
674675
});
675676
});
676677
});

src/sdkBase.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export abstract class SdkBase {
175175
}
176176
}
177177

178-
// When the SDK has been initialized, this vfunction should return the token
178+
// When the SDK has been initialized, this function should return the token
179179
// from the most recent refresh request, if there is a request, wait for the
180180
// new token. Otherwise, returns a promise which will be resolved after init.
181181
public getAdvertisingTokenAsync() {
@@ -192,7 +192,11 @@ export abstract class SdkBase {
192192
}
193193

194194
public isIdentityAvailable() {
195-
const identityAvailable = this.isIdentityValid() || this._apiClient?.hasActiveRequests();
195+
const identity = this._identity ?? this.getIdentityNoInit();
196+
const identityAvailable =
197+
(this.isIdentityValid(identity) && !this.temporarilyUnavailable(identity)) ||
198+
this._apiClient?.hasActiveRequests();
199+
196200
if (!identityAvailable) {
197201
if (this._callbackManager) {
198202
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
@@ -299,8 +303,7 @@ export abstract class SdkBase {
299303
if (this.hasOptedOut()) this._callbackManager.runCallbacks(EventType.OptoutReceived, {});
300304
}
301305

302-
private isIdentityValid() {
303-
const identity = this._identity ?? this.getIdentityNoInit();
306+
private isIdentityValid(identity: Identity | OptoutIdentity | null | undefined) {
304307
return identity && !hasExpired(identity.refresh_expires);
305308
}
306309

0 commit comments

Comments
 (0)