feat: add context change listeners for flag subscriptions #1281
+692
−25
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.
Add Context Change Listeners for Flag Subscriptions - Proposal
Overview
Implements context change listeners for the web SDK, addressing open-feature/spec#346. Provides a type-safe way to subscribe to flag value updates when evaluation context changes.
Problem
The current Events API requires significant boilerplate to track individual flag values. This PR adds a convenient API allowing developers to subscribe directly to flag changes with minimal code.
Solution
Two approaches for subscribing to context changes are implemented as examples for discussion:
1. EvaluationDetails Subscriptions
Enhanced
EvaluationDetailsobjects returned from evaluation methods include anonContextChangedmethod. The callback is invoked whenever the context changes.2. Client-Level Subscriptions
Type-specific methods directly on the client for subscribing to context changes. Performs an initial flag evaluation and invokes the callback immediately, then again whenever the context changes.
Implementation
EvaluationDetailsWithSubscription- WrapsEvaluationDetailswith subscription capabilityContextChangeSubscriptionsinterface - Groups subscription methodsevaluateWithSubscriptionhelper - DRY wrapper for evaluation resultsImportant: Providers do NOT need to implement
ContextChangedevents. The SDK automatically emits these events after context changes viasetContext(), making this implementation reliable and provider-agnostic.Handler Management: Uses closure-based approach consistent with existing SDK patterns. A WeakMap-based subscription registry was considered for subscription introspection/bulk operations but would diverge from current patterns and add complexity.
Usage Example
Related Issues
Addresses open-feature/spec#346