Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.0 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers } from 'kea'

/**
 * Upper bound on the "turning it on" state after opt-in. Ingestion normally catches up within
 * seconds and a successful setup detection ends the wait early; the cap only exists so a slow
 * pipeline can never lock the user on the enabling state.
 */
const ENROLLMENT_SETTLE_TIMEOUT_MS = 15000

export interface FeaturePreviewGateSettlingLogicProps {
    flag: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featurePreviewGateSettlingLogicValues {
    settling: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featurePreviewGateSettlingLogicActions {
    markServerCaughtUp: () => {
        value: true
    }
    startSettling: () => {
        value: true
    }
    stopSettling: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featurePreviewGateSettlingLogicMeta {
    key: string
}

export type featurePreviewGateSettlingLogicType = MakeLogicType<
    featurePreviewGateSettlingLogicValues,
    featurePreviewGateSettlingLogicActions,
    FeaturePreviewGateSettlingLogicProps,
    featurePreviewGateSettlingLogicMeta
>

/**
 * Tracks the window between a user opting into a feature preview and the API agreeing. The
 * browser evaluates the flag as on the moment enrollment is stored locally (it is a locally
 * stored person property), while the API keeps denying until that property is ingested. A scene
 * mounted in that window 403s on every request, which reads as a broken product until a reload.
 * The scene gate holds an enabling state while `settling` is true; a successful setup detection
 * (`markServerCaughtUp`, fired by the gate) or the timeout ends the wait.
 */
export const featurePreviewGateSettlingLogic = kea<featurePreviewGateSettlingLogicType>([
    props({} as FeaturePreviewGateSettlingLogicProps),
    key((props) => props.flag),
    path((key) => ['layout', 'scenes', 'components', 'featurePreviewGateSettlingLogic', key]),
    actions({
        startSettling: true,
        stopSettling: true,
        markServerCaughtUp: true,
    }),
    reducers({
        settling: [
            false,
            {
                startSettling: () => true,
                stopSettling: () => false,
                markServerCaughtUp: () => false,
            },
        ],
    }),
    listeners(({ actions, cache }) => ({
        startSettling: () => {
            cache.disposables.add(() => {
                const id = window.setTimeout(() => actions.stopSettling(), ENROLLMENT_SETTLE_TIMEOUT_MS)
                return () => clearTimeout(id)
            }, 'settle-timeout')
        },
        markServerCaughtUp: () => {
            // Ending the wait early makes the timeout redundant; dispose it so it cannot fire
            // after the gate has already mounted the scene.
            cache.disposables.dispose('settle-timeout')
        },
    })),
])