Skip to content
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

fix: Return this if already loaded #1092

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/__tests__/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* currently not supported in the browser lib).
*/

import posthog from '../loader-module'
import posthog, { PostHog } from '../loader-module'
import sinon from 'sinon'
import { window } from '../utils/globals'

Expand Down Expand Up @@ -48,4 +48,22 @@ describe(`Module-based loader in Node env`, () => {
it(`supports capture()`, () => {
expect(() => posthog.capture(`Pat`)).not.toThrow()
})

it(`always returns posthog from init`, () => {
console.error = jest.fn()
console.warn = jest.fn()
expect(posthog.init(`my-test`, undefined, 'sdk-1')).toBeInstanceOf(PostHog)
expect(posthog.init(``, undefined, 'sdk-2')).toBeInstanceOf(PostHog)
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenCalledWith(
'[PostHog.js]',
'PostHog was initialized without a token. This likely indicates a misconfiguration. Please check the first argument passed to posthog.init()'
)
// Already loaded
expect(posthog.init(`my-test`, undefined, 'sdk-1')).toBeInstanceOf(PostHog)
expect(console.warn).toHaveBeenCalledWith(
'[PostHog.js]',
'You have already initialized PostHog! Re-initializing is a no-op'
)
})
})
10 changes: 5 additions & 5 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class PostHog {
*/
init(token: string, config?: Partial<PostHogConfig>, name?: string): PostHog | void {
if (!name || name === PRIMARY_INSTANCE_NAME) {
// This means we are initialising the primary instance (i.e. this)
// This means we are initializing the primary instance (i.e. this)
return this._init(token, config, name)
} else {
const namedPosthog = instances[name] ?? new PostHog()
Expand All @@ -299,17 +299,17 @@ export class PostHog {
// IE11 compatible. We could use polyfills, which would make the
// code a bit cleaner, but will add some overhead.
//
_init(token: string, config: Partial<PostHogConfig> = {}, name?: string): PostHog | void {
_init(token: string, config: Partial<PostHogConfig> = {}, name?: string): PostHog {
if (_isUndefined(token) || _isEmptyString(token)) {
logger.critical(
'PostHog was initialized without a token. This likely indicates a misconfiguration. Please check the first argument passed to posthog.init()'
)
return
return this
}

if (this.__loaded) {
logger.warn('You have already initialized PostHog! Re-initialising is a no-op')
return
logger.warn('You have already initialized PostHog! Re-initializing is a no-op')
return this
}

this.__loaded = true
Expand Down
Loading