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

prevent /config call on multiple initializations #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions demo/views/display-a-map.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
$('#map-warning').hide();
$('#map').show();

Radar.initialize(publishableKey, { debug: true });
Radar.initialize(publishableKey, { debug: true });
Radar.initialize(publishableKey, { debug: true });

// prevent duplicate maps from rendering over each other
Expand Down
16 changes: 12 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Radar {
throw new RadarPublishableKeyError('Secret keys are not allowed. Please use your Radar publishable key.');
}

if (Config.isInitialized()) {
Logger.error('Radar.initialize() called more than once.');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're handling this, should we Logger.warn instead?

Copy link
Collaborator Author

@kochis kochis Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's a "warn" and not an error, but I also sort of want this to fire on a production site. Maybe I will just change to console.warn instead of using the Logger class.

}

// store settings in global config
const live = isLiveKey(publishableKey);
const logLevel = live ? 'error' : 'info';
Expand All @@ -79,11 +83,15 @@ class Radar {
Logger.debug('using options', options);
}

// NOTE(jasonl): this allows us to run jest tests
// without having to mock the ConfigAPI.getConfig call
if (!(window as any)?.RADAR_TEST_ENV) {
ConfigAPI.getConfig();
if (!Config.isInitialized()) { // only call getConfig on first initialization
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want to || keyChanged to handle cases where someone would change the key (can't come up with anything off the top of my head where that would matter here, but we do allow the key to change in Waypoint for instance)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's the one scenario I was sort of thinking of.

Maybe it's worth just checking if the config options have changed or not.

// NOTE(jasonl): this allows us to run jest tests
// without having to mock the ConfigAPI.getConfig call
if (!(window as any)?.RADAR_TEST_ENV) {
ConfigAPI.getConfig();
}
}

Config.setInitialized();
}

public static clear() {
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RadarOptions } from './types';

class Config {
static options: RadarOptions;
static initialized: boolean = false;

static defaultOptions = {
live: false,
Expand All @@ -15,6 +16,14 @@ class Config {
Config.options = options;
}

public static setInitialized() {
Config.initialized = true;
}

public static isInitialized(): boolean {
return Config.initialized;
}

public static get(): RadarOptions {
return Config.options || {};
}
Expand Down
Loading