Skip to content

Commit

Permalink
Merge pull request #651 from microsoft/malaref/metadata-cb-fix
Browse files Browse the repository at this point in the history
Fix metadata callbacks for long/multiple sessions
  • Loading branch information
malaref authored Sep 9, 2024
2 parents 58c8601 + f570c1f commit df59a83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
30 changes: 21 additions & 9 deletions packages/clarity-js/src/data/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,22 @@ function userAgentData(): void {
export function stop(): void {
rootDomain = null;
data = null;
callbacks.forEach(cb => { cb.called = false; });
}

export function metadata(cb: MetadataCallback, wait: boolean = true): void {
export function metadata(cb: MetadataCallback, wait: boolean = true, recall: boolean = false): void {
let upgraded = config.lean ? BooleanFlag.False : BooleanFlag.True;
let called = false;
// if caller hasn't specified that they want to skip waiting for upgrade but we've already upgraded, we need to
// directly execute the callback rather than adding to our list as we only process callbacks at the moment
// directly execute the callback in addition to adding to our list as we only process callbacks at the moment
// we go through the upgrading flow.
if (data && (upgraded || wait === false)) {
// Immediately invoke the callback if the caller explicitly doesn't want to wait for the upgrade confirmation
cb(data, !config.lean);
} else {
callbacks.push({ callback: cb, wait: wait });
called = true;
}
if (recall || !called) {
callbacks.push({ callback: cb, wait, recall, called });
}
}

Expand Down Expand Up @@ -154,9 +158,17 @@ export function save(): void {

function processCallback(upgrade: BooleanFlag) {
if (callbacks.length > 0) {
callbacks.forEach(x => {
if (x.callback && (!x.wait || upgrade)) { x.callback(data, !config.lean); }
})
for (let i = 0; i < callbacks.length; i++) {
const cb = callbacks[i];
if (cb.callback && !cb.called && (!cb.wait || upgrade)) {
cb.callback(data, !config.lean);
cb.called = true;
if (!cb.recall) {
callbacks.splice(i, 1);
i--;
}
}
}
}
}

Expand Down Expand Up @@ -257,11 +269,11 @@ function getCookie(key: string): string {
// * Cookie was previously not encoded by Clarity and browser encoded it once or more
// * Cookie was previously encoded by Clarity and browser did not encode it
let [isEncoded, decodedValue] = decodeCookieValue(pair[1]);

while (isEncoded) {
[isEncoded, decodedValue] = decodeCookieValue(decodedValue);
}

return decodedValue;
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/clarity-js/types/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export type DecodedToken = (any | any[]);
export type MetadataCallback = (data: Metadata, playback: boolean) => void;
export interface MetadataCallbackOptions {
callback: MetadataCallback,
wait: boolean
wait: boolean,
recall: boolean,
called: boolean
}
export type SignalCallback = (data: ClaritySignal) => void

Expand Down

0 comments on commit df59a83

Please sign in to comment.