Fix websocket stays subscribed after widget edit due to multiple instances #2753
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.
Summary
When a widget is added or edited, the system will send a broadcast to a new instance of the widget class (this is easy to spot when debugging or adding additional logging). Because there is a websocket connection to keep widgets updated, we should use a singleton variable to only start a subscription in one of these instances + subscribe for screen off broadcasts in that instance. This will stop the connection from staying active after the screen is turned off due to multiple instances of the class listening and only one receiving screen off events.
When the screen is turned on, only one instance of the widget provider is needed to update all existing widgets and created by the initial broadcast receiver in the app class.
Fixes #2750
Screenshots
n/a
Link to pull request in Documentation repository
n/a
Any other notes
Discussion