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

How to fix Replacing an existing Haven instance. Are you sure this behaviour is intended? #142

Open
alveshelio opened this issue Jan 4, 2022 · 2 comments

Comments

@alveshelio
Copy link

Hi,

First of all, a big thank you for creating this package. It's really helpful.

I'd like to know if it would be possible for you to instead of console.warn("Replacing an existing Haven instance. Are you sure this behaviour is intended?");if you could simply return the existing instance.

create would be like this

  public static create(options: Partial<HavenOptions>): Haven {
    if (Haven.instance) {
      return Haven.instance
    } else {
      Haven.instance = new Haven(options);
      Haven.instance.init();
      return Haven.instance;
  }
}

If we have already an instance then there's no point in creating a new one right?

In my code I'm doing

const havenInstance = Haven.getInstance()
if (!havenInstance) {
    Haven.create(options)
}

But now I'm getting a console error because getInstance() has this console.error("No Haven instance found. Make sure to create a Haven instance before attempting to access it.")

Thank you

@chiiya
Copy link
Owner

chiiya commented Jan 6, 2022

Hey,

What exactly is your use case here, i.e. how are you getting to the point that you have multiple conflicting Haven.create() calls? The error message is there because usually that shouldn't be happening.

@alveshelio
Copy link
Author

alveshelio commented Jan 7, 2022

Hi,

Thank you for getting back.
I'm using Haven in a Next.js app and I'm instantiating it in _app.ts

  import { askUserConsent } from '@libs/askUserConcent'

  useEffect(() => {
    // Init analytics
    askUserConsent(locale)
  }, [])

// /libs/askUserConcent.ts
export const askUserConsent = (lang: string): void => {
  const havenInstance = Haven.getInstance()

  if (!havenInstance) {
    Haven.create({
      lang,
      services: [
        {
          name: 'PostHog',
          purposes: ['analytics'],
          inject: () => {
            analytics.enable()
          },
        },
      ],
      notification: {
        policyUrl: '/privacy-policy',
        position: 'bottom',
        styles: {
          background: '#000',
          textColor: '#A0AEC0',
          linkColor: '#07C969',
          buttonBackgroundColor: '#07C969',
          buttonBackgroundColorHover: '#0D9347',
          buttonTextColor: '#000',
        },
      },
      translations: {
        fr: {
          notification: {
            accept: 'Accepter',
            decline: 'Refuser',
            message: 'Message',
            policy: 'Politique',
          },
          preferences: {
            description: 'Description',
            save: 'Sauvegarder',
            saved: 'Sauvegardé',
          },
          purposes: {},
        },
      },
    })
  }
}

However if I don't check if an instance already exists, then it will create new ones.

I was wondering it this could be something that the library could handle since we aren't supposed to create multiple instances, instead of simply warn the user to actually return the existing instance.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants