Skip to content

GuidesDerive a data source

Derive a data source

Derive a live data source from any data source or provider with Transform, SelectItems, WhereItems or ConcatSources, without writing a provider subclass.

You can derive a live data source from any data source or provider without writing a provider subclass. Transform reshapes the whole collection, SelectItems maps each item, WhereItems keeps the matching items and ConcatSources joins several sources into one. Debounce is a derived source too, and it signals once after a burst, as rebuild once per burst shows. Each returns a data source you can read, feed to a provider or derive from again. What every source promises its readers, and who disposes what, is on data sources and ownership.

Every derived source works the same way. The first read builds its items from the source it derives from. Each time that source signals, the derived source rebuilds once, on the thread that signalled, and then signals its own change token. The result is an immutable snapshot, so a reader never sees half a change.

WhereItems keeps the items that match a predicate, and SelectItems maps each item with a selector. The names avoid a clash with LINQ’s Where and Select, which copy the items once and never update. Each link owns the one before it, so disposing names below also disposes enabled and features.

var search = new Feature("search", true);
var export = new Feature("export", false);
var features = dataSourceFactory.CreateMutable([search, export]);
var enabled = features.WhereItems(feature => feature.Enabled);
await using var names = enabled.SelectItems(feature => feature.Name);
var before = names.Collection;
var audit = new Feature("audit", true);
features.Add(audit);
var after = names.Collection;

The first read of names builds ["search"]. features.Add(audit) signals, so enabled rebuilds, signals, and names rebuilds in turn, and the next read returns ["search", "audit"].

Transform passes your function the whole collection, so it can sort, group, take or do anything else that needs every item at once. Use SelectItems or WhereItems when you only need one item at a time.

var scores = dataSourceFactory.CreateMutable([40, 95, 70, 88]);
await using var topThree = scores.Transform(items => items.OrderByDescending(score => score).Take(3));
var before = topThree.Collection;
scores.Add(99);
var after = topThree.Collection;

Adding 99 signals the source, so the derived source runs the function again over all five scores.

ConcatSources takes the receiver first, then the other sources, and concatenates their items in that order. The result signals when any of the sources signals. It owns every source it joins, so disposing it disposes all of them, unless you pass owns: false.

var builtIn = dataSourceFactory.CreateStatic(["search", "export"]);
var custom = dataSourceFactory.CreateMutable<string>();
await using var all = builtIn.ConcatSources([custom]);
var before = all.Collection;
custom.Add("audit");
var after = all.Collection;

Every provider is also a data source, so the operators reach a provider with no extra overload. Pass owns: false to keep the provider yours: disposing the derived source then leaves the provider, and the source under it, alive.

var flags = dataSourceFactory.CreateMutable(["search"]);
await using var provider = providerFactory.Create(items => items.ToList(), flags);
await using var upper = provider.SelectItems(flag => flag.ToUpperInvariant(), owns: false);
flags.Add("export");
var derived = upper.Collection;

The provider still reads after upper is disposed, because owns: false left it to the code that created it.

  • LINQ on Collection.

    • You write source.Collection.Where(...) or source.Collection.Select(...).
    • What happens: you get a one-time copy that never updates when the source changes.
    • Write instead: WhereItems or SelectItems, which rebuild each time the source signals.
  • One source shared by two chains.

    • You write two derived sources over the same source, both with the default ownership.
    • What happens: disposing the first chain disposes the shared source, and the second chain stops updating.
    • Write instead: pass owns: false to every chain except the one that should own the source.
  • A selector that throws on a rebuild.

    • You write a selector or predicate that fails for an item that arrives later.
    • What happens: a throw on the first read reaches the reader; a throw on a rebuild keeps the last items and says nothing, so readers see stale data.
    • Write instead: catch inside the delegate and return a fallback item, or filter the bad item out.
  • A slow transform.

    • You write a delegate that calls a service or sorts a very large list.
    • What happens: it runs on the thread that signalled the source, so it delays whoever changed the source.
    • Write instead: keep the delegate cheap, and fetch slow data with a polling source.
  • A derived source that is never disposed.

    • You write a derived source for a short task and let it go out of scope.
    • What happens: it stays subscribed to its source, so it keeps rebuilding and stays alive.
    • Write instead: dispose the last link of the chain, which disposes every link it owns.