GuidesReact to changes
React to changes
Run code each time a provider rebuilds its collection, and make a provider rebuild on a signal of your own.
A provider signals its own change token after each rebuild. Subscribe to it to react, or give the provider another token to rebuild on. How the rebuild runs is on how a provider rebuilds.
Subscribe with ChangeToken.OnChange
Section titled “Subscribe with ChangeToken.OnChange”A token signals once, so ChangeToken.OnChange asks for a fresh one after each signal. Pass it the provider’s GetChangeToken and a callback. The provider signals after the rebuild, so the callback reads the rebuilt collection.
var rebuilds = 0;IReadOnlyList<string> latest = provider.Collection;
using var subscription = ChangeToken.OnChange( provider.GetChangeToken, () => { rebuilds++; latest = provider.Collection; });
observable.Add("alpha");observable.Add("beta");Each Add signals the observable source, and each signal rebuilds the collection once. Two adds give two callbacks. Dispose the subscription when you stop listening.
Rebuild on a signal of your own
Section titled “Rebuild on a signal of your own”A provider can rebuild for reasons other than its data source, such as a setting that changes how it filters. Implement ISupportChangeToken for the setting, and pass it to the provider’s base constructor as an additional change token producer.
private sealed class FilterSettings : ISupportChangeToken, IDisposable{ private CancellationTokenSource _changes = new();
public string Prefix { get; private set; } = "a";
public void Update(string prefix) { Prefix = prefix;
var next = new CancellationTokenSource(); var previous = Interlocked.Exchange(ref _changes, next); // Left undisposed on purpose: a reader on another thread may still hold its token, // and a cancelled source with no timer holds no resources. previous.Cancel(); }
public IChangeToken GetChangeToken() { var source = Volatile.Read(ref _changes); return new CancellationChangeToken(source.Token); }
public void Dispose() => _changes.Dispose();}
private sealed class FilteredProvider( ICollectionDataSource<string> dataSource, FilterSettings settings) : BaseCollectionProvider<string, IReadOnlyList<string>>(dataSource, [settings]){ protected override IReadOnlyList<string> CreateCollection(IEnumerable<string> items) => items.Where(item => item.StartsWith(settings.Prefix, StringComparison.Ordinal)).ToList();}FilterSettings stores the new prefix before it signals, and it signals by swapping in a new token and then cancelling the one it handed out. When the token signals, the provider calls CreateCollection again with the data source’s whole collection. The provider does not own additional producers, so it never disposes them.
Pitfalls
Section titled “Pitfalls”-
Holding one token.
- You write
var token = provider.GetChangeToken();and register one callback on it. - What happens: a token signals once, so you hear about the first rebuild and none after it.
- Write instead: use
ChangeToken.OnChange(provider.GetChangeToken, ...), which asks for a fresh token after each signal.
- You write
-
Doing heavy or blocking work in CreateCollection.
- You write a
CreateCollectionthat calls a service, waits on a task or sorts a large list. - What happens: it runs under the provider’s lock, on the thread that signalled, so it delays the code that changed the source and can deadlock when it waits on another thread. See reads and the lock.
- Write instead: keep
CreateCollectionshort and free of waits, and do slow work before the change reaches the source.
- You write a
-
Expecting one rebuild per burst.
- You write ten edits to an observable collection and a callback that counts rebuilds.
- What happens: each edit signals, and each signal is one full rebuild, so ten edits make ten rebuilds and ten callbacks.
- Write instead: make fewer signals, such as a source that replaces its whole list in one change.