Skip to content

Commit

Permalink
Exposing Universal Storage on the Analytics object rather than passin…
Browse files Browse the repository at this point in the history
…g it directly to plugins (#721)
  • Loading branch information
pooyaj authored Dec 9, 2022
1 parent 7fcf762 commit 779e66b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 87 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-mails-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Expose universal storage directly on analytics object
9 changes: 2 additions & 7 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import * as SegmentPlugin from '../../plugins/segmentio'
import jar from 'js-cookie'
import { PriorityQueue } from '../../lib/priority-queue'
import { getCDN, setGlobalCDNUrl } from '../../lib/parse-cdn'
import { UniversalStorage } from '../../core/user'
import { clearAjsBrowserStorage } from '../../test-helpers/browser-storage'
import { ActionDestination } from '@/plugins/remote-loader'

const storage = {} as UniversalStorage

let fetchCalls: Array<any>[] = []

jest.mock('unfetch', () => {
Expand Down Expand Up @@ -882,8 +879,7 @@ describe('retries', () => {
throw new Error('aaay')
},
},
ajs,
storage
ajs
)

// Dispatching an event will push it into the priority queue.
Expand Down Expand Up @@ -911,8 +907,7 @@ describe('retries', () => {
ready: () => Promise.resolve(true),
track: (ctx) => ctx,
},
ajs,
storage
ajs
)

// @ts-ignore ignore reassining function
Expand Down
10 changes: 6 additions & 4 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ export class Analytics
queue ?? createDefaultQueue(options?.retryQueue, disablePersistance)

this._universalStorage = new UniversalStorage(
disablePersistance !== false
? ['localStorage', 'cookie', 'memory']
: ['memory'],
disablePersistance ? ['memory'] : ['localStorage', 'cookie', 'memory'],
getAvailableStorageOptions(cookieOptions)
)

Expand Down Expand Up @@ -167,6 +165,10 @@ export class Analytics
return this._user
}

get storage(): UniversalStorage {
return this._universalStorage
}

async track(...args: EventParams): Promise<DispatchedEvent> {
const [name, data, opts, cb] = resolveArguments(...args)

Expand Down Expand Up @@ -322,7 +324,7 @@ export class Analytics
const ctx = Context.system()

const registrations = plugins.map((xt) =>
this.queue.register(ctx, xt, this, this._universalStorage)
this.queue.register(ctx, xt, this)
)
await Promise.all(registrations)

Expand Down
2 changes: 0 additions & 2 deletions packages/browser/src/core/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Analytics } from '../analytics'
import { Context } from '../context'
import { UniversalStorage } from '../user'

interface PluginConfig {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: any
priority?: 'critical' | 'non-critical' // whether AJS should expect this plugin to be loaded before starting event delivery
storage?: UniversalStorage
}

// enrichment - modifies the event. Enrichment can happen in parallel, by reducing all changes in the final event. Failures in this stage could halt event delivery.
Expand Down
79 changes: 32 additions & 47 deletions packages/browser/src/core/queue/__tests__/event-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import { Plugin } from '../../plugin'
import { EventQueue } from '../event-queue'
import { pTimeout } from '../../callback'
import { ActionDestination } from '../../../plugins/remote-loader'
import { UniversalStorage } from '../../user'

const storage = {} as UniversalStorage

async function flushAll(eq: EventQueue): Promise<Context[]> {
const flushSpy = jest.spyOn(eq, 'flush')
Expand Down Expand Up @@ -152,8 +149,7 @@ describe('Flushing', () => {
return Promise.resolve(ctx)
},
},
ajs,
storage
ajs
)

eq.dispatch(fruitBasket)
Expand Down Expand Up @@ -223,8 +219,7 @@ describe('Flushing', () => {
return Promise.resolve(ctx)
},
},
ajs,
storage
ajs
)

eq.dispatch(fruitBasket)
Expand Down Expand Up @@ -262,8 +257,7 @@ describe('Flushing', () => {
return ctx
},
},
ajs,
storage
ajs
)

const dispatches = [
Expand Down Expand Up @@ -300,8 +294,7 @@ describe('Flushing', () => {
return ctx
},
},
ajs,
storage
ajs
)

const context = await eq.dispatchSingle(fruitBasket)
Expand All @@ -328,8 +321,7 @@ describe('Flushing', () => {
return Promise.resolve(ctx)
},
},
ajs,
storage
ajs
)

eq.dispatch(fruitBasket)
Expand Down Expand Up @@ -370,8 +362,7 @@ describe('Flushing', () => {
return Promise.resolve(ctx)
},
},
ajs,
storage
ajs
)

const fruitBasketDelivery = eq.dispatch(fruitBasket)
Expand Down Expand Up @@ -438,9 +429,9 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -471,9 +462,9 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -505,9 +496,9 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -539,9 +530,9 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -572,9 +563,9 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -607,8 +598,8 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -641,8 +632,8 @@ describe('Flushing', () => {

const ctx = new Context(evt)

await eq.register(Context.system(), fullstory, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), fullstory, ajs)
await eq.register(Context.system(), segmentio, ajs)

eq.dispatch(ctx)

Expand Down Expand Up @@ -672,9 +663,9 @@ describe('Flushing', () => {
}

const ctx = new Context(evt)
await eq.register(Context.system(), amplitude, ajs, storage)
await eq.register(Context.system(), mixPanel, ajs, storage)
await eq.register(Context.system(), segmentio, ajs, storage)
await eq.register(Context.system(), amplitude, ajs)
await eq.register(Context.system(), mixPanel, ajs)
await eq.register(Context.system(), segmentio, ajs)
await eq.dispatch(ctx)

const skipAmplitudeAndSegment: MiddlewareFunction = ({
Expand All @@ -693,8 +684,7 @@ describe('Flushing', () => {
await eq.register(
Context.system(),
sourceMiddlewarePlugin(skipAmplitudeAndSegment, {}),
ajs,
storage
ajs
)

await eq.dispatch(ctx)
Expand All @@ -712,9 +702,7 @@ describe('deregister', () => {
const toBeRemoved = { ...testPlugin, name: 'remove-me' }
const plugins = [testPlugin, toBeRemoved]

const promises = plugins.map((p) =>
eq.register(Context.system(), p, ajs, storage)
)
const promises = plugins.map((p) => eq.register(Context.system(), p, ajs))
await Promise.all(promises)

await eq.deregister(Context.system(), toBeRemoved, ajs)
Expand All @@ -727,9 +715,7 @@ describe('deregister', () => {
const toBeRemoved = { ...testPlugin, name: 'remove-me', unload: jest.fn() }
const plugins = [testPlugin, toBeRemoved]

const promises = plugins.map((p) =>
eq.register(Context.system(), p, ajs, storage)
)
const promises = plugins.map((p) => eq.register(Context.system(), p, ajs))
await Promise.all(promises)

await eq.deregister(Context.system(), toBeRemoved, ajs)
Expand Down Expand Up @@ -792,8 +778,7 @@ describe('dispatchSingle', () => {
return Promise.resolve(ctx)
},
},
ajs,
storage
ajs
)

expect(eq.queue.length).toBe(0)
Expand Down
Loading

0 comments on commit 779e66b

Please sign in to comment.