From 409cae4b9ac404277aa44bab7428186129b42a35 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:32:05 -0600 Subject: [PATCH] refactor/fix and clean core (#658) --- .changeset/selfish-pots-give.md | 5 + packages/core-integration-tests/README.md | 7 +- packages/core-integration-tests/package.json | 3 +- .../src/plugins/middleware/index.test.ts | 236 ------------------ .../src/public-api.test.ts | 6 + packages/core-integration-tests/tsconfig.json | 2 + .../arguments-resolver/__tests__/page.test.ts | 166 ------------ packages/core/src/arguments-resolver/index.ts | 1 - packages/core/src/arguments-resolver/page.ts | 57 ----- packages/core/src/index.ts | 3 - packages/core/src/plugins/middleware/index.ts | 127 ---------- packages/core/src/priority-queue/persisted.ts | 136 ---------- .../src/queue/__tests__/event-queue.test.ts | 52 ---- packages/core/src/utils/to-facade.ts | 53 ---- yarn.lock | 1 - 15 files changed, 19 insertions(+), 836 deletions(-) create mode 100644 .changeset/selfish-pots-give.md delete mode 100644 packages/core-integration-tests/src/plugins/middleware/index.test.ts create mode 100644 packages/core-integration-tests/src/public-api.test.ts delete mode 100644 packages/core/src/arguments-resolver/__tests__/page.test.ts delete mode 100644 packages/core/src/arguments-resolver/index.ts delete mode 100644 packages/core/src/arguments-resolver/page.ts delete mode 100644 packages/core/src/plugins/middleware/index.ts delete mode 100644 packages/core/src/priority-queue/persisted.ts delete mode 100644 packages/core/src/utils/to-facade.ts diff --git a/.changeset/selfish-pots-give.md b/.changeset/selfish-pots-give.md new file mode 100644 index 000000000..604288dd1 --- /dev/null +++ b/.changeset/selfish-pots-give.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-core': patch +--- + +Clean up dead code. diff --git a/packages/core-integration-tests/README.md b/packages/core-integration-tests/README.md index 08ec468c6..2173d3154 100644 --- a/packages/core-integration-tests/README.md +++ b/packages/core-integration-tests/README.md @@ -1,2 +1,5 @@ -Core tests that require AnalyticsBrowser, etc. -This exists because we can't create circular dependencies -- so, for example, installing AnalyticsBrowser as a dev dependency on core. \ No newline at end of file +# Core Integration Tests +This can contain a mix of tests which cover the public API of the package. This can range anywhere from typical integration tests that might stub out the API (which may or may not also be in the package itself), to tests around the specific npm packaged artifact. Examples include: +- Is a license included in npm pack? +- can you import a module (e.g. is the package.json path correctly to allow consumers to import)? +- are there missing depenndencies in package.json? diff --git a/packages/core-integration-tests/package.json b/packages/core-integration-tests/package.json index ad0d5f317..0783c9385 100644 --- a/packages/core-integration-tests/package.json +++ b/packages/core-integration-tests/package.json @@ -14,7 +14,6 @@ "packageManager": "yarn@3.2.1", "devDependencies": { "@internal/config": "workspace:^", - "@segment/analytics-core": "workspace:^", - "@segment/analytics-next": "workspace:^" + "@segment/analytics-core": "workspace:^" } } diff --git a/packages/core-integration-tests/src/plugins/middleware/index.test.ts b/packages/core-integration-tests/src/plugins/middleware/index.test.ts deleted file mode 100644 index 38e674d2a..000000000 --- a/packages/core-integration-tests/src/plugins/middleware/index.test.ts +++ /dev/null @@ -1,236 +0,0 @@ -/** - * @jest-environment jsdom - */ -import { - MiddlewareFunction, - sourceMiddlewarePlugin, - CoreContext, - CorePlugin, -} from '@segment/analytics-core' -import { Analytics } from '@segment/analytics-next' - -describe(sourceMiddlewarePlugin, () => { - const simpleMiddleware: MiddlewareFunction = ({ payload, next }) => { - if (!payload.obj.context) { - payload.obj.context = {} - } - - payload.obj.context.hello = 'from the other side' - - next(payload) - } - - const xt = sourceMiddlewarePlugin(simpleMiddleware, {}) - - it('creates a source middleware', () => { - expect(xt.name).toEqual('Source Middleware simpleMiddleware') - expect(xt.version).toEqual('0.1.0') - }) - - it('is loaded automatically', async () => { - // @ts-expect-error - expect(await xt.load(CoreContext.system())).toBeTruthy() - expect(xt.isLoaded()).toBe(true) - }) - - describe('Middleware', () => { - it('allows for changing the event', async () => { - const changeProperties: MiddlewareFunction = ({ payload, next }) => { - if (!payload.obj.properties) { - payload.obj.properties = {} - } - payload.obj.properties.hello = 'from the other side' - next(payload) - } - - const xt = sourceMiddlewarePlugin(changeProperties, {}) - - expect( - ( - await xt.track!( - new CoreContext({ - type: 'track', - }) - ) - ).event.properties - ).toEqual({ - hello: 'from the other side', - }) - }) - - it('uses a segment facade object', async () => { - let type = '' - const facadeMiddleware: MiddlewareFunction = ({ payload, next }) => { - type = payload.type() - next(payload) - } - - const xt = sourceMiddlewarePlugin(facadeMiddleware, {}) - - await xt.track!( - new CoreContext({ - type: 'track', - }) - ) - - expect(type).toEqual(type) - }) - - it('cancels the event if `next` is not called', async () => { - const hangs: MiddlewareFunction = () => {} - const hangsXT = sourceMiddlewarePlugin(hangs, {}) - - const doesNotHang: MiddlewareFunction = ({ next, payload }) => { - next(payload) - } - - const doesNotHangXT = sourceMiddlewarePlugin(doesNotHang, {}) - const toReturn = new CoreContext({ type: 'track' }) - const returnedCtx = await doesNotHangXT.track!(toReturn) - - expect(returnedCtx).toBe(toReturn) - - const toCancel = new CoreContext({ type: 'track' }) - await Promise.resolve(hangsXT.track!(toCancel)).catch((err) => { - expect(err).toMatchInlineSnapshot(` - ContextCancelation { - "reason": "Middleware \`next\` function skipped", - "retry": false, - "type": "middleware_cancellation", - } - `) - }) - }) - }) - - describe('Common use cases', () => { - it('can be used to cancel an event altogether', async () => { - const blowUp: MiddlewareFunction = () => { - // do nothing - // do not invoke next - } - - const ajs = new Analytics({ - writeKey: 'abc', - }) - - const ctx = await ajs.page('hello') - expect(ctx.logs().map((l) => l.message)).toContain('Delivered') - - // TODO: make browser use CoreContext - // @ts-ignore - await ajs.addSourceMiddleware(blowUp) - const notDelivered = await ajs.page('hello') - expect(notDelivered.logs().map((l) => l.message)).not.toContain( - 'Delivered' - ) - }) - - it('can be used to re-route/cancel destinations', async () => { - let middlewareInvoked = false - const pageMock = jest.fn() - - const skipGA: MiddlewareFunction = ({ payload, next }) => { - if (!payload.obj.integrations) { - payload.obj.integrations = {} - } - - payload.obj.integrations['Google Analytics'] = false - middlewareInvoked = true - next(payload) - } - - const gaDestination: CorePlugin = { - name: 'Google Analytics', - isLoaded: () => true, - load: async () => {}, - type: 'destination', - version: '1.0', - page: async (ctx) => { - pageMock() - return ctx - }, - } - - const ajs = new Analytics({ - writeKey: 'abc', - }) - - // TODO: make browser use CoreContext - // @ts-ignore - await ajs.register(gaDestination) - - await ajs.page('hello') - expect(pageMock).toHaveBeenCalled() - - // TODO: make browser use CoreContext - // @ts-ignore - await ajs.addSourceMiddleware(skipGA) - await ajs.page('hello') - expect(middlewareInvoked).toBe(true) - expect(pageMock).toHaveBeenCalledTimes(1) - }) - }) - - describe('Event API', () => { - it('wraps track', async () => { - const evt = new CoreContext({ - type: 'track', - }) - - expect((await xt.track!(evt)).event.context).toMatchInlineSnapshot(` - Object { - "hello": "from the other side", - } - `) - }) - - it('wraps identify', async () => { - const evt = new CoreContext({ - type: 'identify', - }) - - expect((await xt.identify!(evt)).event.context).toMatchInlineSnapshot(` - Object { - "hello": "from the other side", - } - `) - }) - - it('wraps page', async () => { - const evt = new CoreContext({ - type: 'page', - }) - - expect((await xt.page!(evt)).event.context).toMatchInlineSnapshot(` - Object { - "hello": "from the other side", - } - `) - }) - - it('wraps group', async () => { - const evt = new CoreContext({ - type: 'group', - }) - - expect((await xt.group!(evt)).event.context).toMatchInlineSnapshot(` - Object { - "hello": "from the other side", - } - `) - }) - - it('wraps alias', async () => { - const evt = new CoreContext({ - type: 'alias', - }) - - expect((await xt.alias!(evt)).event.context).toMatchInlineSnapshot(` - Object { - "hello": "from the other side", - } - `) - }) - }) -}) diff --git a/packages/core-integration-tests/src/public-api.test.ts b/packages/core-integration-tests/src/public-api.test.ts new file mode 100644 index 000000000..1d6f3f5c0 --- /dev/null +++ b/packages/core-integration-tests/src/public-api.test.ts @@ -0,0 +1,6 @@ +import { CoreContext } from '@segment/analytics-core' + +it('should be able to import and instantiate some module from core', () => { + // Test the ability to do basic imports + expect(typeof new CoreContext({ type: 'alias' })).toBe('object') +}) diff --git a/packages/core-integration-tests/tsconfig.json b/packages/core-integration-tests/tsconfig.json index 9936980ac..7bf286986 100644 --- a/packages/core-integration-tests/tsconfig.json +++ b/packages/core-integration-tests/tsconfig.json @@ -2,6 +2,8 @@ "extends": "../../tsconfig.json", "exclude": ["node_modules", "dist"], "compilerOptions": { + "noUnusedLocals": false, + "noUnusedParameters": false, "module": "esnext", "target": "ES5", "moduleResolution": "node", diff --git a/packages/core/src/arguments-resolver/__tests__/page.test.ts b/packages/core/src/arguments-resolver/__tests__/page.test.ts deleted file mode 100644 index 0ddd87abb..000000000 --- a/packages/core/src/arguments-resolver/__tests__/page.test.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { resolvePageArguments } from '../page' - -const bananaPhone = { - banana: 'phone', -} - -const baseOptions = { - integrations: { - amplitude: false, - }, -} - -describe(resolvePageArguments, () => { - test('should accept (category, name, properties, options, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'category', - 'name', - bananaPhone, - baseOptions, - fn - ) - - expect(category).toEqual('category') - expect(name).toEqual('name') - expect(properties).toEqual(bananaPhone) - expect(options).toEqual(baseOptions) - expect(cb).toEqual(fn) - }) - - test('empty strings ("", "", "", { integrations })', () => { - const [category, name, properties, options] = resolvePageArguments( - '', - '', - null, - { - integrations: { - Amplitude: { - sessionId: '123', - }, - }, - } - ) - - expect(category).toEqual('') - expect(name).toEqual('') - expect(properties).toEqual({}) - expect(options).toEqual({ - integrations: { - Amplitude: { - sessionId: '123', - }, - }, - }) - }) - - test('should accept (category, name, properties, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'category', - 'name', - bananaPhone, - fn - ) - - expect(category).toEqual('category') - expect(name).toEqual('name') - expect(properties).toEqual(bananaPhone) - expect(cb).toEqual(fn) - - expect(options).toEqual({}) - }) - - it('should accept (category, name, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'category', - 'name', - fn - ) - - expect(category).toEqual('category') - expect(name).toEqual('name') - expect(properties).toEqual({}) - expect(cb).toEqual(fn) - - expect(options).toEqual({}) - }) - - it('should accept (name, properties, options, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'name', - bananaPhone, - baseOptions, - fn - ) - - expect(category).toEqual(null) - expect(name).toEqual('name') - expect(properties).toEqual(bananaPhone) - expect(options).toEqual(baseOptions) - expect(cb).toEqual(fn) - }) - - it('should accept (name, properties, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'name', - bananaPhone, - fn - ) - - expect(category).toEqual(null) - expect(name).toEqual('name') - expect(properties).toEqual(bananaPhone) - expect(cb).toEqual(fn) - expect(options).toEqual({}) - }) - - it('should accept (name, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - 'name', - fn - ) - - expect(name).toEqual('name') - expect(cb).toEqual(fn) - - expect(category).toEqual(null) - expect(properties).toEqual({}) - expect(options).toEqual({}) - }) - - it('should accept (properties, options, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - bananaPhone, - baseOptions, - fn - ) - - expect(cb).toEqual(fn) - expect(properties).toEqual(bananaPhone) - expect(options).toEqual(baseOptions) - - expect(name).toEqual(null) - expect(category).toEqual(null) - }) - - it('should accept (properties, callback)', () => { - const fn = jest.fn() - const [category, name, properties, options, cb] = resolvePageArguments( - bananaPhone, - fn - ) - - expect(properties).toEqual(bananaPhone) - expect(cb).toEqual(fn) - - expect(options).toEqual({}) - expect(name).toEqual(null) - expect(category).toEqual(null) - }) -}) diff --git a/packages/core/src/arguments-resolver/index.ts b/packages/core/src/arguments-resolver/index.ts deleted file mode 100644 index feed53c7e..000000000 --- a/packages/core/src/arguments-resolver/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './page' diff --git a/packages/core/src/arguments-resolver/page.ts b/packages/core/src/arguments-resolver/page.ts deleted file mode 100644 index b5960ac3b..000000000 --- a/packages/core/src/arguments-resolver/page.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { isFunction, isPlainObject, isString } from '../validation/helpers' -import { JSONObject, EventProperties, CoreOptions } from '../events' -import { Callback } from '../events/interfaces' - -/** - * Helper for page, screen methods - */ -export function resolvePageArguments( - category?: string | object, - name?: string | object | Callback, - properties?: EventProperties | CoreOptions | Callback | null, - options?: CoreOptions | Callback, - callback?: Callback -): [ - string | null, - string | null, - EventProperties, - CoreOptions, - Callback | undefined -] { - let resolvedCategory: string | undefined | null = null - let resolvedName: string | undefined | null = null - const args = [category, name, properties, options, callback] - - const strings = args.filter(isString) - if (strings[0] !== undefined && strings[1] !== undefined) { - resolvedCategory = strings[0] - resolvedName = strings[1] - } - - if (strings.length === 1) { - resolvedCategory = null - resolvedName = strings[0] - } - - const resolvedCallback = args.find(isFunction) as Callback | undefined - - const objects = args.filter((obj) => { - if (resolvedName === null) { - return isPlainObject(obj) - } - return isPlainObject(obj) || obj === null - }) as Array - - const resolvedProperties = (objects[0] ?? {}) as EventProperties - const resolvedOptions = (objects[1] ?? {}) as CoreOptions - - return [ - resolvedCategory, - resolvedName, - resolvedProperties, - resolvedOptions, - resolvedCallback, - ] -} - -export type PageParams = Parameters diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index ea528e9b9..d91feb256 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,7 +1,6 @@ export * from './emitter' export * from './emitter/interface' export * from './plugins' -export * from './plugins/middleware' export * from './events/interfaces' export * from './events' export * from './callback' @@ -12,9 +11,7 @@ export * from './queue/event-queue' export * from './analytics' export * from './analytics/dispatch' export * from './analytics/dispatch-emit' -export * from './arguments-resolver' export * from './validation/helpers' export * from './validation/assertions' -export * from './utils/to-facade' export * from './utils/bind-all' export * from './stats' diff --git a/packages/core/src/plugins/middleware/index.ts b/packages/core/src/plugins/middleware/index.ts deleted file mode 100644 index c9ca33f0c..000000000 --- a/packages/core/src/plugins/middleware/index.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { CoreContext, ContextCancelation } from '../../context' -import { CoreSegmentEvent as SegmentEvent } from '../../events' -import { CorePlugin } from '../' -import { SegmentFacade, toFacade } from '../../utils/to-facade' - -export interface MiddlewareParams { - payload: SegmentFacade - - integrations?: SegmentEvent['integrations'] - next: (payload: MiddlewareParams['payload'] | null) => void -} - -export interface DestinationMiddlewareParams { - payload: SegmentFacade - integration: string - next: (payload: MiddlewareParams['payload'] | null) => void -} - -export type MiddlewareFunction = (middleware: MiddlewareParams) => void -export type DestinationMiddlewareFunction = ( - middleware: DestinationMiddlewareParams -) => void - -export async function applyDestinationMiddleware( - destination: string, - evt: SegmentEvent, - middleware: DestinationMiddlewareFunction[] -): Promise { - async function applyMiddleware( - event: SegmentEvent, - fn: DestinationMiddlewareFunction - ): Promise { - let nextCalled = false - let returnedEvent: SegmentEvent | null = null - - await Promise.resolve( - fn({ - payload: toFacade(event, { - clone: true, - traverse: false, - }), - integration: destination, - next(evt) { - nextCalled = true - - if (evt === null) { - returnedEvent = null - } - - if (evt) { - returnedEvent = evt.obj - } - }, - }) - ) - - if (!nextCalled && returnedEvent !== null) { - returnedEvent = returnedEvent as SegmentEvent - returnedEvent.integrations = { - ...event.integrations, - [destination]: false, - } - } - - return returnedEvent - } - - for (const md of middleware) { - const result = await applyMiddleware(evt, md) - if (result === null) { - return null - } - evt = result - } - - return evt -} - -export function sourceMiddlewarePlugin( - fn: MiddlewareFunction, - integrations: SegmentEvent['integrations'] -): CorePlugin { - async function apply(ctx: CoreContext): Promise { - let nextCalled = false - - await Promise.resolve( - fn({ - payload: toFacade(ctx.event, { - clone: true, - traverse: false, - }), - integrations: integrations ?? {}, - next(evt) { - nextCalled = true - if (evt) { - ctx.event = evt.obj - } - }, - }) - ) - - if (!nextCalled) { - throw new ContextCancelation({ - retry: false, - type: 'middleware_cancellation', - reason: 'Middleware `next` function skipped', - }) - } - - return ctx - } - - return { - name: `Source Middleware ${fn.name}`, - type: 'before', - version: '0.1.0', - - isLoaded: (): boolean => true, - load: (ctx) => Promise.resolve(ctx), - - track: apply, - page: apply, - identify: apply, - alias: apply, - group: apply, - } -} diff --git a/packages/core/src/priority-queue/persisted.ts b/packages/core/src/priority-queue/persisted.ts deleted file mode 100644 index 18e69d241..000000000 --- a/packages/core/src/priority-queue/persisted.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { PriorityQueue } from '.' -import { CoreContext, SerializedContext } from '../context' - -// TODO: Move this back into browser -const nullStorage = (): Storage => ({ - getItem: () => null, - setItem: () => null, - removeItem: () => null, - length: 0, - clear: () => null, - key: () => null, -}) - -function persisted(loc: Storage, key: string): CoreContext[] { - const items = loc.getItem(key) - return (items ? JSON.parse(items) : []).map( - (p: SerializedContext) => new CoreContext(p.event, p.id) - ) -} - -function persistItems(loc: Storage, key: string, items: CoreContext[]): void { - const existing = persisted(loc, key) - const all = [...items, ...existing] - - const merged = all.reduce((acc, item) => { - return { - ...acc, - [item.id]: item, - } - }, {} as Record) - - loc.setItem(key, JSON.stringify(Object.values(merged))) -} - -function seen(loc: Storage, key: string): Record { - const stored = loc.getItem(key) - return stored ? JSON.parse(stored) : {} -} - -function persistSeen( - loc: Storage, - key: string, - memory: Record -): void { - const stored = seen(loc, key) - - loc.setItem( - key, - JSON.stringify({ - ...stored, - ...memory, - }) - ) -} - -function remove(loc: Storage, key: string): void { - loc.removeItem(key) -} - -const now = (): number => new Date().getTime() - -function mutex( - loc: Storage, - key: string, - onUnlock: Function, - attempt = 0 -): void { - const lockTimeout = 50 - const lockKey = `persisted-queue:v1:${key}:lock` - - const expired = (lock: number): boolean => new Date().getTime() > lock - const rawLock = loc.getItem(lockKey) - const lock = rawLock ? (JSON.parse(rawLock) as number) : null - - const allowed = lock === null || expired(lock) - if (allowed) { - loc.setItem(lockKey, JSON.stringify(now() + lockTimeout)) - onUnlock() - loc.removeItem(lockKey) - return - } - - if (!allowed && attempt < 3) { - setTimeout(() => { - mutex(loc, key, onUnlock, attempt + 1) - }, lockTimeout) - } else { - console.error('Unable to retrieve lock') - } -} - -export class PersistedPriorityQueue extends PriorityQueue { - loc: Storage - constructor(maxAttempts: number, key: string) { - super(maxAttempts, []) - - if (typeof window === undefined) { - throw new Error('must be run in browser.') - } - - this.loc = window.localStorage ? window.localStorage : nullStorage() - - const itemsKey = `persisted-queue:v1:${key}:items` - const seenKey = `persisted-queue:v1:${key}:seen` - - let saved: CoreContext[] = [] - let lastSeen: Record = {} - - mutex(this.loc, key, () => { - try { - saved = persisted(this.loc, itemsKey) - lastSeen = seen(this.loc, seenKey) - remove(this.loc, itemsKey) - remove(this.loc, seenKey) - - this.queue = [...saved, ...this.queue] - this.seen = { ...lastSeen, ...this.seen } - } catch (err) { - console.error(err) - } - }) - window.addEventListener('pagehide', () => { - if (this.todo > 0) { - const items = [...this.queue, ...this.future] - try { - mutex(this.loc, key, () => { - persistItems(this.loc, itemsKey, items) - persistSeen(this.loc, seenKey, this.seen) - }) - } catch (err) { - console.error(err) - } - } - }) - } -} diff --git a/packages/core/src/queue/__tests__/event-queue.test.ts b/packages/core/src/queue/__tests__/event-queue.test.ts index 68d58c2f7..920bb110c 100644 --- a/packages/core/src/queue/__tests__/event-queue.test.ts +++ b/packages/core/src/queue/__tests__/event-queue.test.ts @@ -3,10 +3,6 @@ import { noop } from 'lodash' import { CoreAnalytics } from '../../analytics' import { pWhile } from '../../utils/p-while' import * as timer from '../../priority-queue/backoff' -import { - MiddlewareFunction, - sourceMiddlewarePlugin, -} from '../../plugins/middleware' import { CoreContext, ContextCancelation } from '../../context' import { CorePlugin } from '../../plugins' import { pTimeout } from '../../callback' @@ -617,54 +613,6 @@ describe('Flushing', () => { expect(amplitude.track).toHaveBeenCalled() expect(segmentio.track).toHaveBeenCalled() }) - - test('respect deny lists generated by other plugin', async () => { - const eq = new EventQueue() - - jest.spyOn(amplitude, 'track') - jest.spyOn(mixPanel, 'track') - jest.spyOn(segmentio, 'track') - - const evt = { - type: 'track' as const, - integrations: { - Amplitude: true, - MixPanel: true, - 'Segment.io': true, - }, - } - - const ctx = new CoreContext(evt) - await eq.register(CoreContext.system(), amplitude, ajs) - await eq.register(CoreContext.system(), mixPanel, ajs) - await eq.register(CoreContext.system(), segmentio, ajs) - await eq.dispatch(ctx) - - const skipAmplitudeAndSegment: MiddlewareFunction = ({ - payload, - next, - }) => { - if (!payload.obj.integrations) { - payload.obj.integrations = {} - } - - payload.obj.integrations['Amplitude'] = false - payload.obj.integrations['Segment.io'] = false - next(payload) - } - - await eq.register( - CoreContext.system(), - sourceMiddlewarePlugin(skipAmplitudeAndSegment, {}), - ajs - ) - - await eq.dispatch(ctx) - - expect(mixPanel.track).toHaveBeenCalledTimes(2) - expect(amplitude.track).toHaveBeenCalledTimes(1) - expect(segmentio.track).toHaveBeenCalledTimes(1) - }) }) }) diff --git a/packages/core/src/utils/to-facade.ts b/packages/core/src/utils/to-facade.ts deleted file mode 100644 index 97e7e2d31..000000000 --- a/packages/core/src/utils/to-facade.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { - Alias, - Facade, - Group, - Identify, - Options, - Page, - Screen, - Track, -} from '@segment/facade' -import { CoreSegmentEvent } from '../events' - -export type SegmentFacade = Facade & { - obj: CoreSegmentEvent -} - -export function toFacade( - evt: CoreSegmentEvent, - options?: Options -): SegmentFacade { - let fcd = new Facade(evt, options) - - if (evt.type === 'track') { - fcd = new Track(evt, options) - } - - if (evt.type === 'identify') { - fcd = new Identify(evt, options) - } - - if (evt.type === 'page') { - fcd = new Page(evt, options) - } - - if (evt.type === 'alias') { - fcd = new Alias(evt, options) - } - - if (evt.type === 'group') { - fcd = new Group(evt, options) - } - - if (evt.type === 'screen') { - fcd = new Screen(evt, options) - } - - Object.defineProperty(fcd, 'obj', { - value: evt, - writable: true, - }) - - return fcd as SegmentFacade -} diff --git a/yarn.lock b/yarn.lock index fd2e28ee3..5d22a9d05 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1197,7 +1197,6 @@ __metadata: dependencies: "@internal/config": "workspace:^" "@segment/analytics-core": "workspace:^" - "@segment/analytics-next": "workspace:^" languageName: unknown linkType: soft