Skip to content

Commit

Permalink
Re-arrange some code
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Mar 7, 2024
1 parent a52791b commit 4ffac38
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/posthog-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,39 @@ export const parseFeatureFlagDecideResponse = (
) => {
const flags = response['featureFlags']
const flagPayloads = response['featureFlagPayloads']
if (flags) {
// using the v1 api
if (_isArray(flags)) {
const $enabled_feature_flags: Record<string, boolean> = {}
if (flags) {
for (let i = 0; i < flags.length; i++) {
$enabled_feature_flags[flags[i]] = true
}
}
persistence &&
persistence.register({
[PERSISTENCE_ACTIVE_FEATURE_FLAGS]: flags,
[ENABLED_FEATURE_FLAGS]: $enabled_feature_flags,
})
} else {
// using the v2+ api
let newFeatureFlags = flags
let newFeatureFlagPayloads = flagPayloads
if (response.errorsWhileComputingFlags) {
// if not all flags were computed, we upsert flags instead of replacing them
newFeatureFlags = { ...currentFlags, ...newFeatureFlags }
newFeatureFlagPayloads = { ...currentFlagPayloads, ...newFeatureFlagPayloads }
if (!flags) {
return
}
// using the v1 api
if (_isArray(flags)) {
const $enabled_feature_flags: Record<string, boolean> = {}
if (flags) {
for (let i = 0; i < flags.length; i++) {
$enabled_feature_flags[flags[i]] = true
}
persistence &&
persistence.register({
[PERSISTENCE_ACTIVE_FEATURE_FLAGS]: Object.keys(filterActiveFeatureFlags(newFeatureFlags)),
[ENABLED_FEATURE_FLAGS]: newFeatureFlags || {},
[PERSISTENCE_FEATURE_FLAG_PAYLOADS]: newFeatureFlagPayloads || {},
})
}
persistence &&
persistence.register({
[PERSISTENCE_ACTIVE_FEATURE_FLAGS]: flags,
[ENABLED_FEATURE_FLAGS]: $enabled_feature_flags,
})
return
}

// using the v2+ api
let newFeatureFlags = flags
let newFeatureFlagPayloads = flagPayloads
if (response.errorsWhileComputingFlags) {
// if not all flags were computed, we upsert flags instead of replacing them
newFeatureFlags = { ...currentFlags, ...newFeatureFlags }
newFeatureFlagPayloads = { ...currentFlagPayloads, ...newFeatureFlagPayloads }
}
persistence &&
persistence.register({
[PERSISTENCE_ACTIVE_FEATURE_FLAGS]: Object.keys(filterActiveFeatureFlags(newFeatureFlags)),
[ENABLED_FEATURE_FLAGS]: newFeatureFlags || {},
[PERSISTENCE_FEATURE_FLAG_PAYLOADS]: newFeatureFlagPayloads || {},
})
}

export class PostHogFeatureFlags {
Expand Down

0 comments on commit 4ffac38

Please sign in to comment.