Skip to content

Commit

Permalink
explicitly await loading of feature switches before loading the Editor (
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsbajorics authored Sep 20, 2023
1 parent aded463 commit 9747779
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion editor/src/templates/editor-entry-point.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions editor/src/utils/feature-switches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,45 @@ 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}`
}

async function loadStoredValue(featureName: FeatureName) {
if (isBrowserEnvironment && !IS_TEST_ENVIRONMENT) {
const existing = await localforage.getItem<boolean | null>(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<void> {
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
}

Expand Down

0 comments on commit 9747779

Please sign in to comment.