-
Notifications
You must be signed in to change notification settings - Fork 135
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
Delay initialization (lazy loading) #637
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
22de419
delay init poc
silesky 0e51bc5
polish
silesky 1266dfe
support passing settings in .load
silesky f24375e
pass settings into load
silesky 4bea175
clean up
silesky a67cfc2
add doc strings
silesky 2f931d6
tweak ts sig
silesky 9e20ac9
update readme
silesky c4dbf05
add test that verifies that extra load calls are ignored
silesky 0894ef2
update readme
silesky 0b5ce4d
update changeset
silesky de3360d
tweak test
silesky 44bc976
fix spelling mistake
silesky 754e74c
update docstring
silesky 4e39761
make type sig consistent
silesky b66c10f
sync README
silesky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-next': minor | ||
--- | ||
|
||
Add ability to delay initialization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/browser/src/browser/__tests__/analytics-lazy-init.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { sleep } from '@segment/analytics-core' | ||
import unfetch from 'unfetch' | ||
import { AnalyticsBrowser } from '..' | ||
import { Analytics } from '../../core/analytics' | ||
import { createSuccess } from '../../test-helpers/factories' | ||
|
||
jest.mock('unfetch') | ||
|
||
const mockFetchSettingsSuccessResponse = () => { | ||
return jest | ||
.mocked(unfetch) | ||
.mockImplementation(() => createSuccess({ integrations: {} })) | ||
} | ||
|
||
describe('Lazy initialization', () => { | ||
let trackSpy: jest.SpiedFunction<Analytics['track']> | ||
let fetched: jest.MockedFn<typeof unfetch> | ||
beforeEach(() => { | ||
fetched = mockFetchSettingsSuccessResponse() | ||
trackSpy = jest.spyOn(Analytics.prototype, 'track') | ||
}) | ||
|
||
it('Should be able to delay initialization ', async () => { | ||
const analytics = new AnalyticsBrowser() | ||
const track = analytics.track('foo') | ||
await sleep(100) | ||
expect(trackSpy).not.toBeCalled() | ||
analytics.load({ writeKey: 'abc' }) | ||
await track | ||
expect(trackSpy).toBeCalledWith('foo') | ||
}) | ||
|
||
it('.load method return an analytics instance', async () => { | ||
const analytics = new AnalyticsBrowser().load({ writeKey: 'foo' }) | ||
expect(analytics instanceof AnalyticsBrowser).toBeTruthy() | ||
}) | ||
|
||
it('should ignore subsequent .load calls', async () => { | ||
const analytics = new AnalyticsBrowser() | ||
await analytics.load({ writeKey: 'my-write-key' }) | ||
await analytics.load({ writeKey: 'def' }) | ||
expect(fetched).toBeCalledTimes(1) | ||
expect(fetched).toBeCalledWith( | ||
expect.stringContaining( | ||
'https://cdn.segment.com/v1/projects/my-write-key/settings' | ||
) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Return a promise that can be externally resolved | ||
*/ | ||
export const createDeferred = <T>() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a better name than |
||
let resolve!: (value: T | PromiseLike<T>) => void | ||
let reject!: (reason: any) => void | ||
const promise = new Promise<T>((_resolve, _reject) => { | ||
resolve = _resolve | ||
reject = _reject | ||
}) | ||
return { | ||
resolve, | ||
reject, | ||
promise, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this section!