Skip to content

Commit

Permalink
fix: Return this if already loaded (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Mar 20, 2024
1 parent 8e4eef2 commit 6fd350d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
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

0 comments on commit 6fd350d

Please sign in to comment.