Access global execution context from different CustomJS

Setup is inside a Jupyter notebook which is rendered to HTML every so often.

I have created CustomJS to load and update data from a rest API. Instead of using AjaxDataSource I create CustomJS that runs on document_ready, populates the historical data and then starts streaming new data by calling the API every x seconds using setInterval. (because ajax datasource supports only one of the two scenarios at a time, from what I could see)

This works fine now.

What I would like to do now is to change the API endpoint when selecting from a dropdown, or clicking a button widget. However, from what I understand, each widget is creating its own context to run the CustomJS. This means, I can clear the chart, load new data, but I would have two intervals running. I could clear the interval, by storing its ID, but this leads me to my main question:

What is the correct way to store state/variables among widgets, to accomplish the behavior I am looking for. I.e. modify a bunch of variables and re-create the chart.