Skip to content

Subscribe to variable updates #346

@bencehornak

Description

@bencehornak

Feature request

When working with DevCycle I have come to notice that OpenFeature doesn't have a convenient way for subscribing to updates of a certain variable similar to DevCycle's variable updates:

var variable: Variable<String> = devcycleClient.variable("str_key", "default")
variable.onUpdate {
    // grab the variable value using it.value
}

Source code

The Events API provides generic callbacks only currently

What the Events spec provides is a global mechanism for listening to updates in general:

The data that providers supply in event payloads may include a list of flag keys changed, error messages, and possibly updated flag values. [See the Event details data type]

Kotlin example:

var myFlag = OpenFeatureAPI.getClient().getBooleanValue("myflag", ...)
OpenFeatureAPI.observe().onEach { event ->
  if(event.eventDetails?.flagsChanged.contains("myflag")) {
    myFlag = OpenFeatureAPI.getClient().getBooleanValue("myflag", ...)
}

So as you can see it's quite a bit of boilerplate for just tracking the values of one variable. And you have to ask fetch the new value again, it's not part of the event payload, making this idiom less convenient.

Motivation

As an application developer I would like to activate/deactivate features of my long-running client-side apps instantaneously. My app is not restarted for days or weeks, so I'd like to subscribe to variable changes in my application code.

Proposal

Some reactive API could be added to the Flag Evaluation API, which is convenient to use as an app developer, such as:

class MyClass {
  private var subscription: ...

  fun init() {
    subscription = OpenFeatureAPI.getClient().onBooleanValueChanges("myflag", "default", { oldValue, newValue ->
      // do some stuff with newValue
      updateUI(newValue)
    })
  }

  fun close() {
    subscription.close()
  }
}

P.S.: If you like the suggestion, feel free to pick up this topic, I won't have much time in the coming weeks for contribution.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions