GuidesImplement a data source
Implement a data source
Write your own ICollectionDataSource that a provider can rebuild from, and signal its changes correctly.
The built-in sources cover static lists, observable collections, several sources at once and polling. Implement ICollectionDataSource<T> when your items come from somewhere else, such as a file watcher or a message queue. To use a provider as a source instead, see a provider as a source.
The contract
Section titled “The contract”ICollectionDataSource<T> has two members. Collection returns the current items. GetChangeToken(), from ISupportChangeToken, returns a token that signals when the items change. A token signals once, so the source hands out a fresh one for each change, and a provider asks again after each signal.
Make Collection safe to read from any thread, and treat a collection you have returned as immutable. A change replaces the whole collection, so a reader that is enumerating keeps a consistent snapshot.
Signal a change
Section titled “Signal a change”The sample keeps its items in a field and its current token in a CancellationTokenSource. Replace stores the new items, then swaps in a new source and cancels the old one. Cancelling signals every token handed out before the swap, and a callback that runs on that signal reads the new items.
private sealed class InMemoryDataSource : ICollectionDataSource<string>, IDisposable{ private IReadOnlyList<string> _items = ["a"]; private CancellationTokenSource _changes = new();
public IEnumerable<string> Collection => Volatile.Read(ref _items);
public void Replace(IEnumerable<string> items) { // Store the new items first, so a callback that runs on the signal reads them. var snapshot = items.ToArray(); Volatile.Write(ref _items, snapshot);
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();}The source implements IDisposable because it owns a CancellationTokenSource. A provider disposes a single source it is given unless you pass owns: false. See who disposes what.
Pitfalls
Section titled “Pitfalls”-
Returning a token that is already signalled, or never a new one.
- You write a
GetChangeToken()that returns the sameCancellationChangeTokenafter it has been cancelled. - What happens: a token signals once. A source that never replaces its token goes silent after the first change, and a token that is already signalled makes a subscriber react at once, again and again.
- Write instead: swap in a new
CancellationTokenSourceon each change, and return a token built from the current one.
- You write a
-
Mutating the list readers enumerate.
- You write a source that keeps one
List<string>and callsAddorClearon it before it signals. - What happens: a reader that is enumerating the list when it changes throws, and
CreateCollectioncan see a half-edited list. - Write instead: build a new array or list, store its reference, and then signal. Readers keep the old collection until they ask again.
- You write a source that keeps one
-
Signalling before storing the new items.
- You write a
Replacethat cancels the token first and assigns the new items after. - What happens: the callbacks run on the signal, so the provider rebuilds from the old items and no later signal arrives to correct it.
- Write instead: store the new items first, then cancel the old token.
- You write a