From 486e35ca0ce69b3cbb643b76fd8101aac7eb080a Mon Sep 17 00:00:00 2001 From: Craig Kochis Date: Mon, 4 Nov 2024 10:59:57 -0500 Subject: [PATCH] prevent /config call on multiple initializations --- demo/views/display-a-map.hbs | 2 ++ src/api.ts | 16 ++++++++++++---- src/config.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/demo/views/display-a-map.hbs b/demo/views/display-a-map.hbs index 3ac7fe1e..ca199a8c 100644 --- a/demo/views/display-a-map.hbs +++ b/demo/views/display-a-map.hbs @@ -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 diff --git a/src/api.ts b/src/api.ts index 93e3dc6c..93286e10 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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.'); + } + // store settings in global config const live = isLiveKey(publishableKey); const logLevel = live ? 'error' : 'info'; @@ -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 + // 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() { diff --git a/src/config.ts b/src/config.ts index 89a599f2..5db19d90 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,6 +2,7 @@ import type { RadarOptions } from './types'; class Config { static options: RadarOptions; + static initialized: boolean = false; static defaultOptions = { live: false, @@ -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 || {}; }