Listen for changes to variable value in datasource

I have a datasource that populates a set of fields based upon a variable’s value. If the value of said variable changes, those populated fields should either be cleared out or re-populated. I’ve been searching the docs and other community posts and have been unable to find a clean way of doing this - is this possible?

Add support for variables in plugins | Grafana documentation - These are the docs that document how to add support for variables, but only cover how to listen for changes to query variables.

How to listen for when a variable changes? - This covers how it might be done for a plugin, but not a datasource.

Thanks!

I’m facing at same situation…
no have doc regarding this.
Some one could help?

I would suggest learning about the EventBus and looking at the Grafana source code. We are using it in the panels, but it should be supported in the data sources as well.

Here are events related to the variables:

  • variables-changed
  • variables-changed-in-url
  • variables-time-range-process-done

Here is the snippet I use.

const subscriber = eventBus.getStream(RefreshEvent).subscribe((event) => {
      doWhateverYouWant();
});
1 Like

@r34m1 The original question was about variable changed events.

Your code snippet is correct for the Refresh Event. That’s different event types and your plugins may react differently.

1 Like