Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 25, 2023
1 parent 2dd3d95 commit bab80f5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
37 changes: 21 additions & 16 deletions packages/browser/src/core/buffer/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { getBufferedPageCtxFixture } from '../../../test-helpers/fixtures'
import * as GlobalAnalytics from '../../../lib/global-analytics-helper'

describe(PreInitMethodCallBuffer, () => {
beforeEach(() => {
GlobalAnalytics.setGlobalAnalytics(undefined as any)
})
describe('page context', () => {
it('should append page context to arguments', () => {
const call = new PreInitMethodCall('identify', ['foo'], jest.fn())
Expand All @@ -31,16 +34,18 @@ describe(PreInitMethodCallBuffer, () => {

it('should also read from global analytics buffer', () => {
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
;(window as any).analytics = [['track', 'foo']]
;(window as any).analytics = [['track', 'snippet']]

const buffer = new PreInitMethodCallBuffer(call1)
expect(buffer.toArray()).toEqual([
call1,
const calls = buffer.toArray()
expect(calls.length).toBe(2)
expect(calls[0]).toEqual(
expect.objectContaining<Partial<PreInitMethodCall>>({
method: 'track',
args: ['foo', getBufferedPageCtxFixture()],
}),
])
args: ['snippet', getBufferedPageCtxFixture()],
})
)
expect(calls[1]).toEqual(call1)
})
})

Expand Down Expand Up @@ -76,18 +81,18 @@ describe(PreInitMethodCallBuffer, () => {
})
it('should also read from global analytics buffer', () => {
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
GlobalAnalytics.setGlobalAnalytics([['identify', 'bar']] as any)
GlobalAnalytics.setGlobalAnalytics([['identify', 'snippet']] as any)

const buffer = new PreInitMethodCallBuffer(call1)
expect(buffer.getCalls('identify')).toEqual(
expect.arrayContaining([
call1,
expect.objectContaining<Partial<PreInitMethodCall>>({
method: 'identify',
args: ['bar', getBufferedPageCtxFixture()],
}),
])
const calls = buffer.getCalls('identify')
expect(calls.length).toBe(2)
expect(calls[0]).toEqual(
expect.objectContaining<Partial<PreInitMethodCall>>({
method: 'identify',
args: ['snippet', getBufferedPageCtxFixture()],
})
)
expect(calls[1]).toEqual(call1)
})
})
describe('clear', () => {
Expand All @@ -104,7 +109,7 @@ describe(PreInitMethodCallBuffer, () => {
})

describe('snippet buffer', () => {
it('should read from a dynamic analytics key', () => {
it('should read from the global analytics instance', () => {
const getGlobalAnalyticsSpy = jest.spyOn(
GlobalAnalytics,
'getGlobalAnalytics'
Expand Down
26 changes: 17 additions & 9 deletions packages/browser/src/core/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,31 @@ type SnippetBuffer = SnippetWindowBufferedMethodCall[]
* Represents any and all the buffered method calls that occurred before initialization.
*/
export class PreInitMethodCallBuffer {
private _calls: MethodCallMap = {}
private _callMap: MethodCallMap = {}

constructor(...calls: PreInitMethodCall[]) {
this.push(...calls)
}

getCalls<T extends PreInitMethodName>(methodName: T): PreInitMethodCall<T>[] {
private get calls() {
this._pushSnippetWindowBuffer()
return (this._calls[methodName] ?? []) as PreInitMethodCall<T>[]
return this._callMap
}

private set calls(calls: MethodCallMap) {
this._callMap = calls
}

getCalls<T extends PreInitMethodName>(methodName: T): PreInitMethodCall<T>[] {
return (this.calls[methodName] ?? []) as PreInitMethodCall<T>[]
}

push(...calls: PreInitMethodCall[]): void {
calls.forEach((call) => {
if (this._calls[call.method]) {
this._calls[call.method]!.push(call)
if (this.calls[call.method]) {
this.calls[call.method]!.push(call)
} else {
this._calls[call.method] = [call]
this.calls[call.method] = [call]
}
})
}
Expand All @@ -181,12 +190,11 @@ export class PreInitMethodCallBuffer {
// clear calls in the global snippet buffered array.
this._pushSnippetWindowBuffer()
// clear calls in this instance
this._calls = {}
this.calls = {}
}

toArray(): PreInitMethodCall[] {
this._pushSnippetWindowBuffer()
return ([] as PreInitMethodCall[]).concat(...Object.values(this._calls))
return ([] as PreInitMethodCall[]).concat(...Object.values(this.calls))
}

/**
Expand Down

0 comments on commit bab80f5

Please sign in to comment.