Skip to content

feat: add an onFeatureFlags listener you can unsubscribe - #670

Open
turnipdabeets wants to merge 8 commits into
chore/v6-adopt-posthog-ios-4from
feat/v6-on-feature-flags-listener
Open

turnipdabeets wants to merge 8 commits into
chore/v6-adopt-posthog-ios-4from
feat/v6-on-feature-flags-listener

Conversation

@turnipdabeets

@turnipdabeets turnipdabeets commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Brings Flutter to parity with posthog-ios 4.0 (PostHogSDK.shared.onFeatureFlags(_:)) and posthog-android 4.0 (PostHog.onFeatureFlags(callback), PostHog/posthog-android#878). Until now Flutter only had PostHogConfig.onFeatureFlags, which is fixed at setup() and can't be removed.

final subscription = Posthog().onFeatureFlags((loaded) {
  if (loaded.variants['new-checkout'] == 'test') { /* ... */ }
});
subscription.unsubscribe();

Same semantics as iOS and Android:

  • Runs after every load (startup, bootstrap, reloadFeatureFlags(), identify()/group()/reset()), with errorsLoading and the last known flags on failure.
  • If flags already loaded, a new listener gets the current values once, shortly after registering. Skipped if a newer load reaches it first.
  • Can register before setup() and survives close()/setup(). reset() and close() drop the cached load, so a new listener never gets the previous user's flags.

Implementation: a Dart-side registry (FeatureFlagsListeners, @internal). Each platform delivers to it next to the existing config callback: method channel (iOS/Android/macOS), desktop client (Windows/Linux), and posthog-js on web. No native changes, since native already forwards every load.

Web now registers its posthog-js onFeatureFlags listener even without config.onFeatureFlags, and unsubscribes it on close() and before re-registering on repeated setup(). Before, every setup() added another JS listener, so the config callback ran once per setup() call.

A throwing listener is logged and doesn't stop the others. Web skips registration when the page's posthog object has no onFeatureFlags (test stubs), instead of throwing during setup().

Stacked on #664 (needs PostHogFeatureFlagsLoaded).

💚 How did you test it?

  • test/feature_flags_listeners_test.dart: delivery until unsubscribe, one replay, replay skipped when a newer load wins, no replay after clear, unsubscribe during delivery, a throwing listener.
  • One wiring test per platform: method channel (posthog_flutter_io_test), desktop (posthog_flutter_desktop_test), web (posthog_flutter_web_setup_test, also covers repeated setup delivering once).
  • posthog_test: no replay of flags loaded before reset() / close().
  • Mutation-checked: removing the reset clear, the newer-load check, the desktop delivery, or the web re-setup unsubscribe each fails its test.
  • flutter analyze clean, flutter test and flutter test --platform chrome test/posthog_flutter_web_setup_test.dart pass.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code wrote this while aligning the flags API across iOS 4, Android 4 and Flutter 6. Kept the listener in Dart instead of adding a native subscribe/unsubscribe channel method: native already pushes every load to Dart, so a Dart registry covers all five platforms with one implementation.

🤖 Generated with Claude Code

@turnipdabeets
turnipdabeets requested a review from a team as a code owner October 25, 2026 16:45
@turnipdabeets turnipdabeets self-assigned this Oct 25, 2026

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed 34d79f4: 2 Should fix.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthog_flutter/lib/src/posthog.dart Outdated
Comment thread posthog_flutter/lib/src/feature_flags_listeners.dart Outdated
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

posthog-flutter Compliance Report

Date: 2026-10-30 16:30:49 UTC
Duration: 96883ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

✅ 29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields ✅ 144ms
Format Validation.Event Has Uuid ✅ 119ms
Format Validation.Event Has Lib Properties ✅ 116ms
Format Validation.Distinct Id Is String ✅ 116ms
Format Validation.Token Is Present ✅ 115ms
Format Validation.Custom Properties Preserved ✅ 118ms
Format Validation.Event Has Timestamp ✅ 117ms
Retry Behavior.Retries On 503 ✅ 5329ms
Retry Behavior.Does Not Retry On 400 ✅ 2119ms
Retry Behavior.Does Not Retry On 401 ✅ 2118ms
Retry Behavior.Respects Retry After Header ✅ 8126ms
Retry Behavior.Implements Backoff ✅ 15449ms
Retry Behavior.Retries On 500 ✅ 5226ms
Retry Behavior.Retries On 502 ✅ 5228ms
Retry Behavior.Retries On 504 ✅ 5227ms
Retry Behavior.Max Retries Respected ✅ 15448ms
Deduplication.Generates Unique Uuids ✅ 126ms
Deduplication.Preserves Uuid On Retry ✅ 5225ms
Deduplication.Preserves Uuid And Timestamp On Retry ✅ 10337ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5231ms
Deduplication.No Duplicate Events In Batch ✅ 125ms
Deduplication.Different Events Have Different Uuids ✅ 117ms
Compression.Sends Gzip When Enabled ✅ 117ms
Batch Format.Uses Proper Batch Structure ✅ 114ms
Batch Format.Flush With No Events Sends Nothing ✅ 108ms
Batch Format.Multiple Events Batched Together ✅ 124ms
Error Handling.Does Not Retry On 403 ✅ 2116ms
Error Handling.Does Not Retry On 413 ✅ 2118ms
Error Handling.Retries On 408 ✅ 5225ms

Feature_Flags Tests

✅ 16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 13ms
Request Payload.Flags Request Uses V2 Query Param ✅ 10ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 10ms
Request Payload.Flags Request Omits Authorization Header ✅ 10ms
Request Payload.Token In Flags Body Matches Init ✅ 10ms
Request Payload.Groups Round Trip ✅ 10ms
Request Payload.Groups Default To Empty Object ✅ 10ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It ✅ 11ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 10ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 9ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 11ms
Request Lifecycle.No Flags Request On Init Alone ✅ 4ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 113ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 19ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 10ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 116ms

@turnipdabeets
turnipdabeets force-pushed the feat/v6-on-feature-flags-listener branch from 5a85f1b to 2f7955e Compare October 26, 2026 16:28
@turnipdabeets
turnipdabeets force-pushed the chore/v6-flags-loaded-result branch from de3a190 to 0a7ab2a Compare October 26, 2026 18:38
@turnipdabeets
turnipdabeets force-pushed the feat/v6-on-feature-flags-listener branch from 2f7955e to a194366 Compare October 26, 2026 18:39

@dustinbyrne dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subscription API and ordinary delivery/replay paths look sound. Two dispatch-boundary cases remain: a config callback can restore an invalidated snapshot after reset/close, and a synchronous config-callback exception prevents runtime subscribers from receiving the load. Both reproduce through the public Dart API and IO channel harness.

AI-assisted review.

Comment thread posthog_flutter/lib/src/posthog_flutter_io.dart
Comment thread posthog_flutter/lib/src/posthog_flutter_io.dart Outdated
@dustinbyrne
dustinbyrne requested a review from a team October 26, 2026 20:22

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed d4105d9: 3 Should fix.

Comment thread posthog_flutter/lib/posthog_flutter_web.dart Outdated
Comment thread posthog_flutter/lib/src/posthog_flutter_io.dart
Comment thread posthog_flutter/lib/posthog_flutter_web.dart Outdated
Base automatically changed from chore/v6-flags-loaded-result to chore/v6-adopt-posthog-android-4 October 28, 2026 14:55

@dustinbyrne dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous web-loader, stale-native-notification and synchronous-replay findings are resolved in the focused regressions. Two runtime subscription routes remain incomplete: native auto-init does not activate the bridge when subscription is the first Dart operation, and Android manual reloads update neither subscribers nor their replay snapshot. Both reproduce independently; native 4.0 build availability remains a separate prerequisite.

AI-assisted review.

Comment thread posthog_flutter/lib/src/posthog.dart Outdated
Comment thread posthog_flutter/lib/src/posthog_flutter_io.dart
@dustinbyrne
dustinbyrne requested a review from a team October 28, 2026 19:27
Base automatically changed from chore/v6-adopt-posthog-android-4 to chore/v6-adopt-posthog-ios-4 October 29, 2026 16:01
@turnipdabeets
turnipdabeets force-pushed the chore/v6-adopt-posthog-ios-4 branch from fd88c7e to 7eebbca Compare October 29, 2026 19:10
turnipdabeets and others added 7 commits October 29, 2026 15:10
… callback

A config callback that threw kept listeners from getting the load, and a
reset() or close() it started was undone when the load was cached after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oader

A registration whose unsubscribe isn't stored yet (close() during the synchronous replay) or never arrives (the loader snippet's stub) now goes inert once setup() or close() runs again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dart bumps a generation when reset() or close() clears the listener registry and sends it with setup, reset and close. Android and iOS tag each onFeatureFlags notification with the generation they had when it fired, and Dart drops ones from an earlier generation, so the previous user's flags can't repopulate the replay cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With native auto-init, Posthog().onFeatureFlags() can be the app's only
Dart SDK call, and nothing created the platform that installs the channel
handler, so native loads never reached the listener.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…teners

posthog-android passes a manual reload only to its completion, so no
onFeatureFlagsCallback arrives and listeners missed the load while new
ones replayed the older snapshot. A reload that finishes after a reset or
close is still dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@turnipdabeets
turnipdabeets force-pushed the feat/v6-on-feature-flags-listener branch from 26f2755 to 4d81d9e Compare October 29, 2026 19:12

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants