Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove direct inspector wirings #758

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-kangaroos-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Remove direct wirings for Segment Inspector
1 change: 0 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@internal/config": "0.0.0",
"@segment/analytics.js-integration": "^3.3.3",
"@segment/analytics.js-integration-amplitude": "^3.3.3",
"@segment/inspector-webext": "^2.0.3",
"@size-limit/preset-big-lib": "^7.0.8",
"@types/flat": "^5.0.1",
"@types/fs-extra": "^9.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,52 @@ jest
const writeKey = 'foo'

describe('Inspector', () => {
const triggeredSpy = jest.fn()
const attachedSpy = jest.fn()
const deliveredSpy = jest.fn()
beforeEach(() => {
Object.assign((window.__SEGMENT_INSPECTOR__ ??= {}), {
triggered: triggeredSpy,
attach: attachedSpy,
delivered: deliveredSpy,
Object.assign(((window as any)['__SEGMENT_INSPECTOR__'] ??= {}), {
attach: jest.fn(),
})
})

it('attaches to inspector', async () => {
await AnalyticsBrowser.load({
const [analytics] = await AnalyticsBrowser.load({
writeKey,
})
expect(attachedSpy).toBeCalledTimes(1)

expect(
((window as any)['__SEGMENT_INSPECTOR__'] as any).attach
).toHaveBeenCalledWith(analytics)
})

it('calls triggered and delivered when an event is sent', async () => {
it('emits essential message lifecycle events', async () => {
const [analytics] = await AnalyticsBrowser.load({
writeKey,
})
expect(attachedSpy).toBeCalledTimes(1)
expect(triggeredSpy).toBeCalledTimes(0)
expect(deliveredSpy).toBeCalledTimes(0)

await analytics.track('foo', {})
const triggeredFn = jest.fn()
const enrichedFn = jest.fn()
const deliveredFn = jest.fn()

analytics.on('dispatch_start', triggeredFn)
analytics.queue.on('message_enriched', enrichedFn)
analytics.queue.on('message_delivered', deliveredFn)

const deliveryPromise = analytics.track('Test event').catch(() => {})

expect(triggeredFn).toHaveBeenCalledTimes(1)

expect(triggeredFn).toHaveBeenCalledWith(
expect.objectContaining({
id: expect.any(String),
event: expect.objectContaining({
event: 'Test event',
type: 'track',
}),
})
)

await deliveryPromise

expect(triggeredSpy.mock.lastCall[0].event.type).toBe('track')
expect(triggeredSpy).toBeCalledTimes(1)
expect(deliveredSpy).toBeCalledTimes(1)
expect(enrichedFn).toHaveBeenCalledTimes(1)
expect(deliveredFn).toHaveBeenCalledTimes(1)
})
})
40 changes: 0 additions & 40 deletions packages/browser/src/core/inspector/__tests__/index.test.ts

This file was deleted.

27 changes: 4 additions & 23 deletions packages/browser/src/core/inspector/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import type { InspectBroker } from '@segment/inspector-webext'
import { getGlobal } from '../../lib/get-global'
import type { Analytics } from '../analytics'

declare global {
interface Window {
__SEGMENT_INSPECTOR__: Partial<InspectBroker>
}
}

const env = getGlobal()

// The code below assumes the inspector extension will use Object.assign
// to add the inspect interface on to this object reference (unless the
// extension code ran first and has already set up the variable)
const inspectorHost: Partial<InspectBroker> = ((env as any)[
'__SEGMENT_INSPECTOR__'
] ??= {})
const inspectorHost: {
attach: (analytics: Analytics) => void
} = ((env as any)['__SEGMENT_INSPECTOR__'] ??= {})

export const attachInspector = (analytics: Analytics) => {
export const attachInspector = (analytics: Analytics) =>
inspectorHost.attach?.(analytics as any)

analytics.on('dispatch_start', (ctx) => inspectorHost.triggered?.(ctx as any))

analytics.queue.on('message_enriched', (ctx) =>
inspectorHost.enriched?.(ctx as any)
)

analytics.queue.on('message_delivered', (ctx) =>
// FIXME: Resolve browsers destinations that the event was sent to
inspectorHost.delivered?.(ctx as any, ['segment.io'])
)
}
Loading