Repository navigation
feat: add public onFeatureFlags listener with flags, variants and error state - #897
Merged
Merged
Conversation
…or state Adds `PostHogSDK.onFeatureFlags(_:)`, which calls back on the main thread whenever feature flags load or change, passing the enabled flags, their values and whether loading failed. A listener registered after flags have loaded is called with the current values. The returned subscription's `unsubscribe()` stops further calls. Listeners can be registered before `setup()`. The bootstrap flags-loaded notification now fires at the end of `setup()` instead of while the remote config is created, so listeners and integrations no longer miss it when `setup()` runs off the main thread. Closes #647 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
3 of 5 tasks
Contributor
|
[Medium risk] Adds public listener API for feature flag updates. The PR is not ready to merge because listeners can receive previous-user flags after reset or callbacks after unsubscribing. Reviews (1) · Last reviewed commit: "feat: add public onFeatureFlags listener..." |
Contributor
posthog-ios Compliance ReportDate: 2026-10-02 22:42:46 UTC
|
| Test | Status | Duration |
|---|---|---|
| Format Validation.Event Has Required Fields | ✅ | 2820ms |
| Format Validation.Event Has Uuid | ✅ | 2814ms |
| Format Validation.Event Has Lib Properties | ✅ | 2799ms |
| Format Validation.Distinct Id Is String | ✅ | 453ms |
| Format Validation.Token Is Present | ✅ | 2880ms |
| Format Validation.Custom Properties Preserved | ✅ | 2732ms |
| Format Validation.Event Has Timestamp | ✅ | 2697ms |
| Retry Behavior.Retries On 503 | ✅ | 11274ms |
| Retry Behavior.Does Not Retry On 400 | ✅ | 4799ms |
| Retry Behavior.Does Not Retry On 401 | ✅ | 4837ms |
| Retry Behavior.Respects Retry After Header | ✅ | 5396ms |
| Retry Behavior.Implements Backoff | ✅ | 15510ms |
| Retry Behavior.Retries On 500 | ✅ | 9277ms |
| Retry Behavior.Retries On 502 | ✅ | 9520ms |
| Retry Behavior.Retries On 504 | ✅ | 9390ms |
| Retry Behavior.Max Retries Respected | ❌ | 21277ms |
| Deduplication.Generates Unique Uuids | ✅ | 2876ms |
| Deduplication.Preserves Uuid On Retry | ✅ | 8840ms |
| Deduplication.Preserves Uuid And Timestamp On Retry | ✅ | 16214ms |
| Deduplication.Preserves Uuid And Timestamp On Batch Retry | ✅ | 7888ms |
| Deduplication.No Duplicate Events In Batch | ✅ | 2992ms |
| Deduplication.Different Events Have Different Uuids | ✅ | 2828ms |
| Compression.Sends Gzip When Enabled | ✅ | 2769ms |
| Batch Format.Uses Proper Batch Structure | ✅ | 2729ms |
| Batch Format.Flush With No Events Sends Nothing | ✅ | 211ms |
| Batch Format.Multiple Events Batched Together | ✅ | 2920ms |
| Error Handling.Does Not Retry On 403 | ✅ | 4746ms |
| Error Handling.Does Not Retry On 413 | ✅ | 4834ms |
| Error Handling.Retries On 408 | ✅ | 5414ms |
Failures
retry_behavior.max_retries_respected
Expected 4 requests, got 6
Feature_Flags Tests
✅ 16/16 tests passed
View Details
| Test | Status | Duration |
|---|---|---|
| Request Payload.Request With Person Properties Device Id | ✅ | 2888ms |
| Request Payload.Flags Request Uses V2 Query Param | ✅ | 2864ms |
| Request Payload.Flags Request Hits Flags Path Not Decide | ✅ | 2899ms |
| Request Payload.Flags Request Omits Authorization Header | ✅ | 2865ms |
| Request Payload.Token In Flags Body Matches Init | ✅ | 2892ms |
| Request Payload.Groups Round Trip | ✅ | 2824ms |
| Request Payload.Groups Default To Empty Object | ✅ | 2773ms |
| Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It | ✅ | 2807ms |
| Request Payload.Disable Geoip False Propagates As Geoip Disable False | ✅ | 2873ms |
| Request Payload.Disable Geoip Omitted Defaults To False | ✅ | 2860ms |
| Request Payload.Flag Keys To Evaluate Contains Only Requested Key | ✅ | 2730ms |
| Request Lifecycle.No Flags Request On Init Alone | ✅ | 108ms |
| Request Lifecycle.No Flags Request On Normal Capture | ✅ | 2997ms |
| Request Lifecycle.Two Flag Calls Produce Two Remote Requests | ✅ | 5533ms |
| Request Lifecycle.Mock Response Value Is Returned To Caller | ✅ | 2853ms |
| Side Effect Events.Get Feature Flag Captures Feature Flag Called Event | ✅ | 3045ms |
…elivery - Drop a flags notification whose values no longer match the cache, e.g. one queued before reset(), so listeners never get the previous user's flags and the replay cache can't hold them. - Skip a listener that another listener unsubscribed during the same delivery. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dustinbyrne
approved these changes
Oct 2, 2026
dustinbyrne
left a comment
Contributor
There was a problem hiding this comment.
Approved with the two inline follow-ups below. Source review included an independent validation pass; the proposed regression tests were not executed.
Human-driven, agent-assisted review.
…om a listener Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ication follows a failure errorsLoading now reports the outcome of the latest delivered load. The deferred bootstrap notification leaves it unchanged, so a startup request that fails before bootstrap readiness is delivered still reports errorsLoading: true, including to late subscribers. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2 of 5 tasks
This was referenced Oct 23, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Closes #647.
Today the only signal on iOS is the
PostHogSDK.didReceiveFeatureFlagsnotification. It carries no payload, so apps have to read each flag again. Web'sonFeatureFlagspasses the enabled flags, their variants and whether loading failed. This PR adds the same listener to iOS, following theon-feature-flagsspec.The callback:
reloadFeatureFlags(), and afteridentify(),group()orreset()reload flags.setup().onFeatureFlags:.Only enabled flags are included, as on Web. When a request fails,
errorsLoadingistrueand the payload holds the last known (cached) flags.Behaviour change for existing consumers
The bootstrap "flags loaded" notification used to fire while the remote config was created, part-way through
setup(). It now fires at the end ofsetup(). Nothing changes whensetup()runs on the main thread. Whensetup()runs off main, the notification used to be able to reach the main thread before anyone was listening, so session replay, surveys and the new listeners all missed it. They now receive it.didReceiveFeatureFlagsis still posted exactly once.Differences from the spec and Web
errorsLoading: false. Web currently doesn't call listeners at all in that case, which posthog-js#5045 fixes to match this behaviour.💚 How did you test it?
Unit tests: new tests in
PostHogFeatureFlagsTest→ "Test onFeatureFlags":unsubscribeerrorsLoadingwith the last known flagssetup()runs off the main threadsetup()andonFeatureFlagsare called together on mainreset(), including a delivery that was already queued whenreset()ranreset()orclose()stops the rest of that delivery, and later updates still arriveerrorsLoadingstaystruewhen the bootstrap notification arrives after a failed loadEach regression test was checked to fail without its fix.
On device: a scripted run on an iPhone 14 Pro Max against a real project, in five scenarios. The online ones covered no bootstrap, and bootstrap with
setup()on main and off main. The offline ones covered Airplane Mode with and without bootstrap. For every callback I checked that:getFeatureFlagidentify()with person properties changed a targeted flagunsubscribestopped further callsreset()never replayed the previous user's flagserrorsLoading: truewith the cached flagsOther: an Objective-C caller compiles against the generated header.
make build,make testandmake lintall pass.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
setup()runs off main.PostHogSDK, not on the remote config, so they survivesetup()/close().unsubscribe()rather than a closure, so it reads well from Objective-C.PostHogConfig.onFeatureFlagsfield, because registering beforesetup()covers that case.PostHogFeatureFlagsLoadedandPostHogFeatureFlagsSubscriptionare open to suggestions.🤖 Generated with Claude Code