Skip to content

GuidesPush or reload items

Push or reload items

Push a whole list into a provider from your own code, or reload it whenever an IChangeToken signals, such as a configuration reload.

Two built-in sources take their items from your code instead of a fixed or observable collection. A mutable source holds a list you push into from any thread. A change-token source loads its items from a function and loads them again each time a change token signals. What every source promises its readers is on data sources and ownership.

CreateMutable returns an IMutableCollectionDataSource<T>. Replace, Update, Add, AddRange, Remove and Clear each swap the source’s items under its lock and signal once, so a provider over it rebuilds once per call, however many items the call changes.

var flags = dataSourceFactory.CreateMutable(["beta"]);
var rebuilds = 0;
await using var provider = providerFactory.Create(
items =>
{
rebuilds++;
return items.ToList();
},
flags
);
var before = provider.Collection;
flags.AddRange(["dark-mode", "search", "export"]);
flags.Update(items => items.Where(flag => flag != "beta"));
var after = provider.Collection;

AddRange adds three items in one call, and Update passes the current items to your function, which runs under the source’s lock, and keeps what it returns, so the provider rebuilds twice after its first build. A call that changes nothing, such as Remove of an item the source doesn’t hold, signals nothing. Every call is safe from any thread, and a reader never sees half a change.

The source never disposes its items, because a reader may still hold an older snapshot after an item leaves it. Dispose your items yourself once nothing reads them.

CreateChangeToken takes a function that returns a change token and a function that loads the items. The source loads on the first read, subscribes to the token, and loads again each time the token signals, then signals its own token. Any producer of fresh tokens works, such as configuration.GetReloadToken or () => fileProvider.Watch("flags.json").

using var settings = new ReloadingSettings(["beta"]);
var flags = dataSourceFactory.CreateChangeToken(settings.GetReloadToken, settings.LoadFlags);
await using var provider = providerFactory.Create(items => items.ToList(), flags);
var before = provider.Collection;
settings.Reload(["beta", "search"]);
var after = provider.Collection;
private sealed class ReloadingSettings(IReadOnlyList<string> flags) : IDisposable
{
private CancellationTokenSource _reload = new();
private IReadOnlyList<string>? Flags { get; set; } = flags;
public IChangeToken GetReloadToken()
{
var reload = Volatile.Read(ref _reload);
return new CancellationChangeToken(reload.Token);
}
public IReadOnlyList<string> LoadFlags() =>
Flags ?? throw new InvalidOperationException("The settings are not valid.");
public void Reload(IReadOnlyList<string>? flags)
{
Flags = flags;
var next = new CancellationTokenSource();
var previous = Interlocked.Exchange(ref _reload, 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 void Dispose() => _reload.Dispose();
}

The reload runs under the source’s lock on the thread that fired the token, so the provider has rebuilt before Reload returns.

When the first load throws, the read throws, and the next read loads again. When a later reload throws, the source keeps its last items, signals nothing and passes the exception to handleException. A handler that returns keeps the source listening, so the next signal reloads. Without a handler, or when the handler throws, the source stops reloading and keeps its last items.

using var settings = new ReloadingSettings(["beta"]);
var failures = new List<Exception>();
var flags = dataSourceFactory.CreateChangeToken(settings.GetReloadToken, settings.LoadFlags, failures.Add);
await using var provider = providerFactory.Create(items => items.ToList(), flags);
var first = provider.Collection;
settings.Reload(null);
var afterFailure = provider.Collection;
settings.Reload(["export"]);
var afterFix = provider.Collection;
  • Adding items one at a time.

    • You write a loop that calls Add for each item.
    • What happens: each call signals, so a provider rebuilds once per item.
    • Write instead: AddRange, Replace or Update, which change many items in one call and one rebuild.
  • A slow load.

    • You write a load function that calls a service or reads a large file.
    • What happens: it runs under the source’s lock on the thread that fired the token, so it delays that code, such as a configuration reload.
    • Write instead: keep the load short, and fetch slow data with a polling source.
  • Leaving out the handler.

    • You write CreateChangeToken(producer, load) over a file someone may save half-written.
    • What happens: the first reload that throws ends reloading, and the source keeps the items it last loaded for good.
    • Write instead: pass a handleException that logs the exception and returns.
  • Expecting a removed item to be disposed.

    • You write source.Remove(connection) and expect the connection to close.
    • What happens: the mutable source never disposes an item, because a reader may still hold a snapshot with it.
    • Write instead: dispose the item yourself once nothing reads it.