tarkah / tickrs

Realtime ticker data in your terminal 📈
MIT License
1.17k stars 58 forks source link

Don't redraw unless new data received over API #15

Closed tarkah closed 3 years ago

tarkah commented 3 years ago

@daniel-j-h I've already merged the user update interval feature and as I mentioned, this doesn't affect redrawing, only when to fetch API data.

Right now I have the app re-draw on a fixed 1 second cycle. In addition, user input causing changes will trigger the app to re-draw. If I can add a mechanism that tells the app to redraw whenever new API data is received, I can ditch the fixed update interval and the entire app will be reactive to when changes occur. Which will be awesome because it'll even further reduce CPU usage, especially when a long interval is specified by the user.

I have an idea on how to implement this, I'll get to it sometime later today!

daniel-j-h commented 3 years ago

Awesome idea!

tarkah commented 3 years ago

I won't merge #18 until tomorrow so I can validate everything works once market is open.

So now 3 things can trigger the app to re-draw...

  1. On update-interval. Defaults to 1 second unless supplied via command line.

  2. When a new API connection is made (a new stock is added, time frame change, options pane opened...) it will initially request the data. When the data is initially received, it'll request the app to redraw to make those changes immediately viewable. After this, the API is queried on update-interval and any updates will be shown through re-draws from 1.

    1. It's worth noting that not ALL API requests are looped on update-interval... For example, graph data is only requested at most every minute (1D - 1min data points) and at least every 24 hours (1Y - 1day data points). In these scenarios, I take the max between those defaults and update-interval to determine how often to query the API.
  3. Input event is received that changes the state of the application or a resize event occurs.

So now, if you have an update-interval of 10 seconds and are just letting it sit there to display, API request + redraw will only occur on that 10 second interval. So CPU usage is basically nothing!