diff --git a/editor/src/templates/editor-entry-point.tsx b/editor/src/templates/editor-entry-point.tsx index 5221fd7dc6cb..a851d8d972f5 100644 --- a/editor/src/templates/editor-entry-point.tsx +++ b/editor/src/templates/editor-entry-point.tsx @@ -7,7 +7,8 @@ import '../vite-shims' // import feature switches so they are loaded before anything else can read them -import '../utils/feature-switches' +import { loadFeatureSwitches } from '../utils/feature-switches' +await loadFeatureSwitches() // Fire off server requests that later block, to improve initial load on slower connections. These will still block, // but this gives us a chance to cache the result first diff --git a/editor/src/utils/feature-switches.ts b/editor/src/utils/feature-switches.ts index 070839031018..3968b9cd71b6 100644 --- a/editor/src/utils/feature-switches.ts +++ b/editor/src/utils/feature-switches.ts @@ -47,6 +47,8 @@ let FeatureSwitches: { [feature in FeatureName]: boolean } = { 'Debug - Print UIDs': false, } +let FeatureSwitchLoaded: { [feature in FeatureName]?: boolean } = {} + function settingKeyForName(featureName: FeatureName): string { return `Feature-Switch-${featureName}` } @@ -54,18 +56,36 @@ function settingKeyForName(featureName: FeatureName): string { async function loadStoredValue(featureName: FeatureName) { if (isBrowserEnvironment && !IS_TEST_ENVIRONMENT) { const existing = await localforage.getItem(settingKeyForName(featureName)) + FeatureSwitchLoaded[featureName] = true if (existing != null) { FeatureSwitches[featureName] = existing } + return true + } else { + return true } } // Load stored settings -fastForEach(AllFeatureNames, (name) => { - void loadStoredValue(name) -}) +export async function loadFeatureSwitches(): Promise { + if (IS_TEST_ENVIRONMENT) { + // in test environments, we actually don't want to load from localforage, ever + return + } + await Promise.all( + AllFeatureNames.map((name) => { + return loadStoredValue(name) + }), + ) +} export function isFeatureEnabled(featureName: FeatureName): boolean { + if (FeatureSwitchLoaded[featureName] !== true) { + if (!IS_TEST_ENVIRONMENT) { + // in test environments, we actually don't want to load from localforage, ever + throw new Error(`Reading FS before it was loaded! ${featureName}`) + } + } return FeatureSwitches[featureName] ?? false }