@@ -10,24 +10,34 @@ export function processQueue(globalObject) {
10
10
// Set pointer which allows renaming of the script
11
11
const pointer = globalObject [ "AlgoliaAnalyticsObject" ] as string ;
12
12
13
- // Check if there is a queue
14
13
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 ;
16
27
17
28
// Loop queue and execute functions in the queue
18
29
queue . forEach ( ( args : IArguments ) => {
19
30
const [ functionName , ...functionArguments ] = [ ] . slice . call ( args ) ;
20
- if ( functionName && isFunction ( ( this as any ) [ functionName ] ) ) {
21
- this [ functionName ] ( ...functionArguments ) ;
22
- }
31
+ _aa ( functionName , ...functionArguments ) ;
23
32
} ) ;
24
33
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 ) ;
31
41
} ;
32
42
}
33
43
}
0 commit comments