Skip to content

Conversation

@wlsnmrk
Copy link
Contributor

@wlsnmrk wlsnmrk commented Dec 14, 2025

Added a general-purpose broadcast for all modifications to auto collections.

In some cases, a user will only want to know that a collection has been modified, without regard to what the modification was. (An example use case: saving a list of high scores to disk whenever the list is modified, whether that's adding a new score or replacing an old one.)

In such cases it is cumbersome to subscribe to all notifications, as the callbacks have different signatures. Currently, the user must do:

public void SubscribeToHighScoreChanges()
{
  _scoresBinding = HighScores.Bind();
  _scoresBinding.OnAdd(OnHighScoreAdded);
  _scoresBinding.OnRemove(OnHighScoreRemoved);
  _scoresBinding.OnUpdate(OnHighScoreUpdated);
  _scoresBinding.OnClear(OnHighScoresCleared);
}

public void OnHighScoreAdded(HighScore _) => SaveHighScores();
public void OnHighScoreRemoved(HighScore _) => SaveHighScores();
public void OnHighScoreUpdated(HighScore _, HighScore __) => SaveHighScores();
public void OnHighScoresCleared() => SaveHighScores();

public void SaveHighScores()
{
  // ...
}

(Granted, the OnClear() callback could be a direct call to SaveHighScores since the signature matches, and perhaps the Added/Removed callbacks could be condensed into one, but the structure of the code becomes less clear as these elisions are made.)

This PR adds a general-purpose modification callback to all auto-collections:

public void SubscribeToHighScoreChanges()
{
  _scoresBinding = HighScores.Bind();
  _scoresBinding.OnModify(SaveHighScores);
}

public void SaveHighScores()
{
  // ...
}

I've also updated some xmldocs comments to match comparable comments on other collections, updated a couple test names to be consistent, and updated the clear tests for AutoMap and AutoSet to verify that they only broadcast when the collection is non-empty (similar to the tests for AutoList).

Added a general-purpose broadcast for all modifications to auto
collections.
@wlsnmrk wlsnmrk requested a review from jolexxa December 14, 2025 00:10
@wlsnmrk wlsnmrk added the enhancement New feature or request label Dec 14, 2025
@wlsnmrk wlsnmrk requested review from Bastani and xodial December 14, 2025 00:12
Copy link
Member

@jolexxa jolexxa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@wlsnmrk wlsnmrk removed request for Bastani and xodial December 14, 2025 01:10
@wlsnmrk wlsnmrk merged commit 8f2f1c8 into chickensoft-games:main Dec 14, 2025
2 checks passed
@wlsnmrk wlsnmrk deleted the feat/collection-modification-broadcast branch December 14, 2025 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants