haven icon indicating copy to clipboard operation
haven copied to clipboard

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

Open alveshelio opened this issue 4 years ago • 2 comments

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

alveshelio avatar Jan 04 '22 17:01 alveshelio

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.

chiiya avatar Jan 06 '22 17:01 chiiya

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

alveshelio avatar Jan 07 '22 14:01 alveshelio