From bab80f5f2e755552be3128d561e41fa3d28a0cbb Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 25 Sep 2023 16:24:06 -0500 Subject: [PATCH] wip --- .../src/core/buffer/__tests__/index.test.ts | 37 +++++++++++-------- packages/browser/src/core/buffer/index.ts | 26 ++++++++----- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/packages/browser/src/core/buffer/__tests__/index.test.ts b/packages/browser/src/core/buffer/__tests__/index.test.ts index f18d2beba..3376a3f32 100644 --- a/packages/browser/src/core/buffer/__tests__/index.test.ts +++ b/packages/browser/src/core/buffer/__tests__/index.test.ts @@ -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()) @@ -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>({ method: 'track', - args: ['foo', getBufferedPageCtxFixture()], - }), - ]) + args: ['snippet', getBufferedPageCtxFixture()], + }) + ) + expect(calls[1]).toEqual(call1) }) }) @@ -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>({ - method: 'identify', - args: ['bar', getBufferedPageCtxFixture()], - }), - ]) + const calls = buffer.getCalls('identify') + expect(calls.length).toBe(2) + expect(calls[0]).toEqual( + expect.objectContaining>({ + method: 'identify', + args: ['snippet', getBufferedPageCtxFixture()], + }) ) + expect(calls[1]).toEqual(call1) }) }) describe('clear', () => { @@ -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' diff --git a/packages/browser/src/core/buffer/index.ts b/packages/browser/src/core/buffer/index.ts index ef228ba17..f06fb1ca4 100644 --- a/packages/browser/src/core/buffer/index.ts +++ b/packages/browser/src/core/buffer/index.ts @@ -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(methodName: T): PreInitMethodCall[] { + private get calls() { this._pushSnippetWindowBuffer() - return (this._calls[methodName] ?? []) as PreInitMethodCall[] + return this._callMap + } + + private set calls(calls: MethodCallMap) { + this._callMap = calls + } + + getCalls(methodName: T): PreInitMethodCall[] { + return (this.calls[methodName] ?? []) as PreInitMethodCall[] } 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] } }) } @@ -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)) } /**