Skip to content

Commit 61688e2

Browse files
authored
Refactor node / browser to share Context, EventQueue, attempt(), Stats, dispatch(), Plugins, and update traits and context interface (#722)
1 parent ad76582 commit 61688e2

File tree

83 files changed

+810
-3094
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+810
-3094
lines changed

.changeset/sweet-rings-tie.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@segment/analytics-next': minor
3+
'@segment/analytics-core': minor
4+
---
5+
6+
Improve core interfaces. Refactor analytics-next to use shared EventQueue, dispatch, and other methods.
7+
Augment Browser interface with traits and context options type.

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = {
4949
'@typescript-eslint/ban-ts-ignore': 'off',
5050
'@typescript-eslint/ban-ts-comment': 'off',
5151
'@typescript-eslint/no-unused-vars': 'off',
52+
'@typescript-eslint/no-empty-interface': 'off',
5253
'@typescript-eslint/no-explicit-any': 'off',
5354
'@typescript-eslint/no-non-null-assertion': 'off',
5455
'@typescript-eslint/no-floating-promises': [

examples/standalone-playground/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
"hoistingLimits": "workspaces"
66
},
77
"scripts": {
8-
"dev": "yarn concurrently 'yarn run -T watch --filter=standalone-playground' 'sleep 10 && npx http-server .'"
8+
"dev": "yarn concurrently 'yarn run -T watch --filter=standalone-playground' 'sleep 10 && npx http-server .'",
9+
"concurrently": "yarn run -T concurrently"
910
},
1011
"dependencies": {
1112
"@segment/analytics-next": "workspace:^"
12-
},
13-
"devDependencies": {
14-
"concurrently": "^7.2.1"
1513
}
1614
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@
3636
"@changesets/cli": "^2.23.2",
3737
"@internal/config": "workspace:^",
3838
"@npmcli/promise-spawn": "^3.0.0",
39+
"@types/express": "4",
3940
"@types/jest": "^28.1.1",
4041
"@types/lodash": "^4",
4142
"@types/node-fetch": "^2.6.2",
4243
"@typescript-eslint/eslint-plugin": "^5.21.0",
4344
"@typescript-eslint/parser": "^5.21.0",
44-
"concurrently": "^7.2.1",
45+
"concurrently": "^7.6.0",
4546
"eslint": "^8.14.0",
4647
"eslint-config-prettier": "^8.5.0",
4748
"eslint-plugin-prettier": "^4.0.0",
49+
"express": "^4.18.2",
4850
"get-monorepo-packages": "^1.2.0",
4951
"husky": "^8.0.0",
5052
"jest": "^28.1.0",
@@ -60,6 +62,7 @@
6062
},
6163
"resolutions": {
6264
"@segment/analytics-next": "workspace:*",
63-
"@segment/analytics-node": "workspace:*"
65+
"@segment/analytics-node": "workspace:*",
66+
"@segment/analytics-core": "workspace:*"
6467
}
6568
}

packages/browser-integration-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"test": "playwright test",
1010
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
11+
"concurrently": "yarn run -T concurrently",
1112
"watch:test": "yarn test --watch",
1213
"tsc": "yarn run -T tsc",
1314
"eslint": "yarn run -T eslint",

packages/browser/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"tsc": "yarn run -T tsc",
3232
"jest": "yarn run -T jest",
3333
"concurrently": "yarn run -T concurrently",
34-
"watch": "yarn concurrently 'NODE_ENV=production yarn umd --watch' 'yarn pkg --watch --incremental'",
34+
"watch": "yarn concurrently 'NODE_ENV=production WATCH=true yarn umd --watch' 'yarn pkg --watch'",
3535
"build": "yarn clean && yarn build-prep && yarn concurrently 'NODE_ENV=production yarn umd' 'yarn pkg' 'yarn cjs'",
3636
"release:cdn": "yarn run -T browser+deps build && NODE_ENV=production bash scripts/release.sh",
3737
"pkg": "yarn tsc -p tsconfig.build.json",
@@ -79,7 +79,6 @@
7979
"circular-dependency-plugin": "^5.2.2",
8080
"compression-webpack-plugin": "^8.0.1",
8181
"execa": "^4.1.0",
82-
"express": "^4.17.3",
8382
"flat": "^5.0.2",
8483
"fs-extra": "^9.0.1",
8584
"jest-dev-server": "^6.0.3",

packages/browser/src/browser/__tests__/analytics-pre-init.integration.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const writeKey = 'foo'
2323

2424
const errMsg = 'errMsg'
2525

26+
jest.spyOn(console, 'error').mockImplementation(() => {}) // silence console spam
27+
2628
describe('Pre-initialization', () => {
2729
const trackSpy = jest.spyOn(Analytics.prototype, 'track')
2830
const identifySpy = jest.spyOn(Analytics.prototype, 'identify')

packages/browser/src/browser/__tests__/standalone-analytics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import unfetch from 'unfetch'
77
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
88
import { sleep } from '../../lib/sleep'
99
import * as Factory from '../../test-helpers/factories'
10-
import { EventQueue } from '@segment/analytics-core'
10+
import { EventQueue } from '../../core/queue/event-queue'
1111

1212
const track = jest.fn()
1313
const identify = jest.fn()

packages/browser/src/browser/__tests__/typedef-tests/analytics-browser.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { Analytics } from '@/core/analytics'
2-
import { Context } from '@/core/context'
3-
import { AnalyticsBrowser } from '@/browser'
4-
import { assertNotAny, assertIs } from '@/test-helpers/type-assertions'
5-
import { Group, User } from '../../../core/user'
1+
import { AnalyticsBrowser, Analytics, Context, User, Group } from '../../..'
2+
import { assertNotAny, assertIs } from '../../../test-helpers/type-assertions'
63

74
/**
85
* These are general typescript definition tests;
@@ -77,23 +74,23 @@ export default {
7774
'Identify should work with spread objects ': () => {
7875
const user = {
7976
name: 'john',
80-
id: 12345,
77+
id: 'abc123',
8178
}
8279
const { id, ...traits } = user
8380
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', traits)
8481
},
8582
'Track should work with spread objects': () => {
8683
const user = {
8784
name: 'john',
88-
id: 12345,
85+
id: 'abc123',
8986
}
9087
const { id, ...traits } = user
9188
void AnalyticsBrowser.load({ writeKey: 'foo' }).track('foo', traits)
9289
},
9390
'Identify should work with generic objects ': () => {
9491
const user = {
9592
name: 'john',
96-
id: 12345,
93+
id: 'abc123',
9794
}
9895
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', user)
9996
},
@@ -122,4 +119,12 @@ export default {
122119
assertNotAny(analytics)
123120
assertIs<AnalyticsBrowser>(analytics)
124121
},
122+
'Should error if there is a type conflict in Traits': () => {
123+
const analytics = new AnalyticsBrowser().load({ writeKey: 'foo' })
124+
assertNotAny(analytics)
125+
assertIs<AnalyticsBrowser>(analytics)
126+
127+
// @ts-expect-error - id should be a string
128+
void analytics.identify('foo', { id: 123 })
129+
},
125130
}

packages/browser/src/browser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ async function loadAnalytics(
279279

280280
const plugins = settings.plugins ?? []
281281
const classicIntegrations = settings.classicIntegrations ?? []
282-
Context.initMetrics(legacySettings.metrics)
282+
Context.initRemoteMetrics(legacySettings.metrics)
283283

284284
// needs to be flushed before plugins are registered
285285
flushPreBuffer(analytics, preInitBuffer)

0 commit comments

Comments
 (0)