Skip to content

Commit 662bf64

Browse files
tkruggEunjae Lee
authored andcommitted
fix(bug): persist function reference after library is loaded (#128)
* test: add test for checking function pointer persistance * fix: persist function reference after library is loaded * fix: use toBe to assert identity
1 parent 4fcdad0 commit 662bf64

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/__tests__/_processQueue.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,11 @@ describe("processQueue", () => {
6767
callback
6868
);
6969
});
70+
71+
it("should use the same `aa` function after library loaded", () => {
72+
const oldPointerFunction = globalObject.aa;
73+
insights.processQueue(globalObject);
74+
const newPointerFunction = globalObject.aa;
75+
expect(oldPointerFunction).toBe(newPointerFunction);
76+
});
7077
});

lib/_processQueue.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,34 @@ export function processQueue(globalObject) {
1010
// Set pointer which allows renaming of the script
1111
const pointer = globalObject["AlgoliaAnalyticsObject"] as string;
1212

13-
// Check if there is a queue
1413
if (pointer) {
15-
const queue: IArguments[] = globalObject[pointer].queue || [];
14+
const _aa = (functionName: string, ...functionArguments: any[]) => {
15+
if (functionName && isFunction((this as any)[functionName])) {
16+
this[functionName](...functionArguments);
17+
}
18+
};
19+
20+
// `aa` is the user facing function, which is defined in the install snippet.
21+
// - before library is initialized `aa` fills a queue
22+
// - after library is initialized `aa` calls `_aa`
23+
const aa = globalObject[pointer];
24+
aa.queue = aa.queue || [];
25+
26+
const queue: IArguments[] = aa.queue;
1627

1728
// Loop queue and execute functions in the queue
1829
queue.forEach((args: IArguments) => {
1930
const [functionName, ...functionArguments] = [].slice.call(args);
20-
if (functionName && isFunction((this as any)[functionName])) {
21-
this[functionName](...functionArguments);
22-
}
31+
_aa(functionName, ...functionArguments);
2332
});
2433

25-
// Reassign pointer
26-
globalObject[pointer] = (
27-
functionName: string,
28-
...functionArguments: any[]
29-
) => {
30-
(this as any)[functionName](...functionArguments);
34+
// FIXME: Reassigning the pointer is a bad idea (cf: https://github.com/algolia/search-insights.js/issues/127)
35+
// to remove this without any breaking change, we redefine the Array.prototype.push method on the queue array.
36+
// for next major version, use a custom method instead of push.
37+
// @ts-ignore (otherwise typescript won't let you change the signature)
38+
queue.push = (args: IArguments) => {
39+
const [functionName, ...functionArguments] = [].slice.call(args);
40+
_aa(functionName, ...functionArguments);
3141
};
3242
}
3343
}

0 commit comments

Comments
 (0)