Skip to content

Commit da9953a

Browse files
committed
experimenting
1 parent a0e8984 commit da9953a

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

localtest/shared/uid2SdkTag.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<script src="https://cdn.integ.uidapi.com/uid2-sdk-3.5.0.js"></script>
2-
<!-- <script src="../../dist/uid2-sdk-3.10.0.js"></script> -->
1+
<!-- <script src="https://cdn.integ.uidapi.com/uid2-sdk-3.5.0.js"></script> -->
2+
<script src="../../dist/uid2-sdk-3.15.0.js"></script>

src/callbackManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum EventType {
77
IdentityUpdated = 'IdentityUpdated',
88
SdkLoaded = 'SdkLoaded',
99
OptoutReceived = 'OptoutReceived',
10+
NoIdentityAvailable = 'NoIdentityAvailable',
1011
}
1112

1213
export type CallbackPayload = SdkLoadedPayload | PayloadWithIdentity;

src/integrationTests/basic.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ testCookieAndLocalStorage(() => {
142142

143143
describe('when initialised without identity', () => {
144144
describe('when uid2 value is not available', () => {
145+
let asyncCallback: jest.Mock<CallbackHandler>;
145146
beforeEach(() => {
146147
uid2.init({ callback: callback, useCookie: useCookie });
147148
});
@@ -166,6 +167,20 @@ testCookieAndLocalStorage(() => {
166167
test('should be in unavailable state', () => {
167168
(expect(uid2) as any).toBeInUnavailableState();
168169
});
170+
test.only('should invoke NoIdentityAvailable callback', () => {
171+
asyncCallback = jest.fn((event, payload) => {
172+
if (event === EventType.NoIdentityAvailable) {
173+
return { event: 'No Identity Available' };
174+
}
175+
});
176+
uid2!.callbacks!.push(asyncCallback);
177+
expect(asyncCallback).toBeCalledWith(EventType.NoIdentityAvailable, expect.anything());
178+
// expect(callback).toHaveBeenNthCalledWith(
179+
// 3,
180+
// expect.objectContaining({ test: EventType.NoIdentityAvailable })
181+
// );
182+
//expect(uid2.callbacks).toBeCalledWith(EventType.NoIdentityAvailable, expect.anything());
183+
});
169184
});
170185

171186
describe('when uid2 value with invalid JSON is available', () => {

src/sdkBase.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ export abstract class SdkBase {
158158
this.triggerRefreshOrSetTimer(validatedIdentity);
159159
}
160160
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {});
161+
} else {
162+
console.log('new event goes here');
163+
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
161164
}
162165
}
163166

@@ -412,8 +415,12 @@ export abstract class SdkBase {
412415

413416
private handleNewIdentity(identity: Identity | OptoutIdentity | null) {
414417
const validatedIdentity = this.validateAndSetIdentity(identity);
415-
if (validatedIdentity && !isOptoutIdentity(validatedIdentity))
418+
if (validatedIdentity && !isOptoutIdentity(validatedIdentity)) {
416419
this.triggerRefreshOrSetTimer(validatedIdentity);
420+
} else if (!validatedIdentity) {
421+
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
422+
console.log('new event goes here');
423+
}
417424
}
418425

419426
private _refreshTimerId: ReturnType<typeof setTimeout> | null = null;
@@ -424,12 +431,20 @@ export abstract class SdkBase {
424431
clearTimeout(this._refreshTimerId);
425432
}
426433
this._refreshTimerId = setTimeout(() => {
427-
if (this.isLoginRequired()) return;
434+
if (this.isLoginRequired()) {
435+
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
436+
console.log('new event goes here');
437+
return;
438+
}
428439
const validatedIdentity = this.validateAndSetIdentity(
429440
this._storageManager?.loadIdentity() ?? null
430441
);
431-
if (validatedIdentity && !isOptoutIdentity(validatedIdentity))
442+
if (validatedIdentity && !isOptoutIdentity(validatedIdentity)) {
432443
this.triggerRefreshOrSetTimer(validatedIdentity);
444+
} else if (!validatedIdentity) {
445+
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
446+
console.log('new event goes here');
447+
}
433448
this._refreshTimerId = null;
434449
}, timeout);
435450
}

0 commit comments

Comments
 (0)