Skip to content

Commit

Permalink
delay init poc
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Oct 24, 2022
1 parent 525705f commit bb8b8ab
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-mails-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Add ability to delay initialization
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,16 @@ describe('Pre-initialization', () => {
await ajsBrowser2
})
})

describe('Delayed initialization', () => {
it('Should be able to delay initialization ', async () => {
const analytics = new AnalyticsBrowser({ writeKey: 'foo' })
const track = analytics.track('foo')
await sleep(100)
expect(trackSpy).not.toBeCalled()
analytics.load()
await track
expect(trackSpy).toBeCalledWith('foo')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { Group, User } from '../../../core/user'
* They aren't meant to be run by anything but the typescript compiler.
*/
export default {
'AnalyticsBrowser should accept settings': () => {
const analytics = new AnalyticsBrowser({ writeKey: 'foo' })
assertNotAny(analytics)
assertIs<AnalyticsBrowser>(analytics)
void analytics.track('foo')
},
'AnalyticsBrowser should return the correct type': () => {
const result = AnalyticsBrowser.load({ writeKey: 'abc' })
assertNotAny(result)
Expand Down
40 changes: 35 additions & 5 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
*/
cdnURL?: string

/**
* Wait for .load() call.
* Default is "false" for instantiation via AnalyticsBrowser.load (invokes .load immediately)
* and "true" for the AnalyticsBrowser constructor
*/
lazy?: boolean
}

export function loadLegacySettings(
Expand Down Expand Up @@ -308,13 +315,38 @@ async function loadAnalytics(
return [analytics, ctx]
}

const createDeferred = () => {
let resolve!: () => void
const promise = new Promise((_resolve) => {
resolve = () => _resolve(undefined)
})
return {
promise,
resolve,
}
}
/**
* The public browser interface for this package.
* Use AnalyticsBrowser.load to create an instance.
*/
export class AnalyticsBrowser extends AnalyticsBuffered {
private constructor(loader: AnalyticsLoader) {
super(loader)
_resolve: Function
load(): void {
// if user wants to invoke .load immediately after instantiation want to resolve immediately
this._resolve()
}

constructor(...args: Parameters<typeof AnalyticsBrowser['load']>) {
const [settings, options] = args
const lazy = settings.lazy ?? true
const { promise, resolve } = createDeferred()
super((buffer) =>
promise.then(() => loadAnalytics(settings, options, buffer))
)
if (!lazy) {
resolve()
}
this._resolve = resolve
}

/**
Expand All @@ -331,9 +363,7 @@ export class AnalyticsBrowser extends AnalyticsBuffered {
settings: AnalyticsBrowserSettings,
options: InitOptions = {}
): AnalyticsBrowser {
return new this((preInitBuffer) =>
loadAnalytics(settings, options, preInitBuffer)
)
return new this({ ...settings, lazy: false }, options)
}

static standalone(
Expand Down

0 comments on commit bb8b8ab

Please sign in to comment.