Skip to content

Commit 2930c9b

Browse files
committed
encapsulate snippet-specific logic
1 parent 37cd27a commit 2930c9b

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

src/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
flushAddSourceMiddleware,
2121
flushSetAnonymousID,
2222
flushOn,
23-
getSnippetWindowBuffer,
2423
} from './core/buffer'
24+
import { getNormalizedSnippetWindowBuffer } from './core/buffer/snippet'
2525

2626
export interface LegacyIntegrationConfiguration {
2727
/* @deprecated - This does not indicate browser types anymore */
@@ -109,7 +109,7 @@ function flushPreBuffer(
109109
analytics: Analytics,
110110
buffer: PreInitMethodCallBuffer
111111
): void {
112-
buffer.push(...getSnippetWindowBuffer())
112+
buffer.push(...getNormalizedSnippetWindowBuffer())
113113
flushSetAnonymousID(analytics, buffer)
114114
flushOn(analytics, buffer)
115115
}

src/core/buffer/index.ts

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isThenable } from '../../lib/is-thenable'
66
* The names of any Analytics instance methods that can be called pre-initialization.
77
* These methods should exist statically on AnalyticsBrowser.
88
*/
9-
type PreInitMethodName =
9+
export type PreInitMethodName =
1010
| 'trackSubmit'
1111
| 'trackClick'
1212
| 'trackLink'
@@ -89,43 +89,8 @@ export interface PreInitMethodCall<
8989
reject: (reason: any) => void
9090
}
9191

92-
const normalizeSnippetBuffer = (buffer: SnippetBuffer): PreInitMethodCall[] => {
93-
return buffer.map(
94-
([methodName, ...args]) =>
95-
({
96-
method: methodName,
97-
resolve: () => {},
98-
reject: console.error,
99-
args,
100-
called: false,
101-
} as PreInitMethodCall)
102-
)
103-
}
104-
105-
type PreInitMethodParams<MethodName extends PreInitMethodName> = Parameters<
106-
Analytics[MethodName]
107-
>
108-
109-
type SnippetWindowBufferedMethodCall<
110-
MethodName extends PreInitMethodName = PreInitMethodName
111-
> = [MethodName, ...PreInitMethodParams<MethodName>]
112-
113-
/**
114-
* A list of the method calls before initialization for snippet users
115-
* For example, [["track", "foo", {bar: 123}], ["page"], ["on", "ready", function(){..}]
116-
*/
117-
type SnippetBuffer = SnippetWindowBufferedMethodCall[]
118-
119-
/**
120-
* Fetch the buffered method calls from the window object and normalize them.
121-
*/
122-
export const getSnippetWindowBuffer = (): PreInitMethodCall[] => {
123-
const wa = window.analytics
124-
const buffered =
125-
// @ts-expect-error
126-
(wa && wa[0] ? [...wa] : []) as SnippetBuffer
127-
return normalizeSnippetBuffer(buffered)
128-
}
92+
export type PreInitMethodParams<MethodName extends PreInitMethodName> =
93+
Parameters<Analytics[MethodName]>
12994

13095
/**
13196
* Infer return type; if return type is promise, unwrap it.

src/core/buffer/snippet.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { PreInitMethodCall, PreInitMethodName, PreInitMethodParams } from '.'
2+
3+
type SnippetWindowBufferedMethodCall<
4+
MethodName extends PreInitMethodName = PreInitMethodName
5+
> = [MethodName, ...PreInitMethodParams<MethodName>]
6+
7+
/**
8+
* A list of the method calls before initialization for snippet users
9+
* For example, [["track", "foo", {bar: 123}], ["page"], ["on", "ready", function(){..}]
10+
*/
11+
type SnippetBuffer = SnippetWindowBufferedMethodCall[]
12+
13+
/**
14+
* Fetch the buffered method calls from the window object
15+
*/
16+
const readSnippetWindowBuffer = (): SnippetBuffer => {
17+
const wa = window.analytics as unknown as any[]
18+
const buffer = wa && wa[0] ? [...wa] : []
19+
return buffer
20+
}
21+
22+
const normalizeSnippetWindowBuffer = (
23+
buffer: SnippetBuffer
24+
): PreInitMethodCall[] => {
25+
return buffer.map(
26+
([methodName, ...args]) =>
27+
({
28+
method: methodName,
29+
resolve: () => {},
30+
reject: console.error,
31+
args,
32+
called: false,
33+
} as PreInitMethodCall)
34+
)
35+
}
36+
37+
export const getNormalizedSnippetWindowBuffer = () =>
38+
normalizeSnippetWindowBuffer(readSnippetWindowBuffer())

0 commit comments

Comments
 (0)