Yonto Player plays your own library, and you can add several sources to it: a Jellyfin server, a Plex server, a folder on a NAS. Most screens show one source at a time, the active one. Search is the one place that asks all of them at once.
That sounds like a loop over a list. In practice a TV app has a few constraints that make the loop interesting: a remote for typing, and sources that answer at very different speeds. This post is about the choices that keep Search fast and honest, and about the Watch list that grew out of it.
Search stays live
The obvious way to search many sources is to build an index of all of them in the background and query that. I chose not to. An index needs a walker that keeps every source in step, and I did not want that running on a television. So Search asks the sources when you type, only more of them. The cost is that each search goes out to the sources again.
Which sources get asked is a small filter:
- the source is on;
- its config can search at all (a catalog saved as not searchable is left out);
- its last health check does not say it needs you, for example a login or a fixed address.
The active source is asked whatever its last check said.
Fan out, collect as they arrive
The core of it is a small class that takes a list of sources and an ask function. It starts one coroutine per source, lets at most three run at the same time, and hands every answer to the screen the moment it lands. Here is a simplified sketch, with generic names, of how it is shaped:
// Simplified sketch, not the exact source. suspend fun <R> fanOut( sources: List<Source>, concurrency: Int, ask: suspend (Source) -> Outcome<R>, onAnswer: (outcomes: Map<Source, Outcome<R>>, pending: Int) -> Unit, ) = coroutineScope { val permits = Semaphore(concurrency) val finished = Channel<Pair<Source, Outcome<R>>>(Channel.UNLIMITED) val outcomes = LinkedHashMap<Source, Outcome<R>>() for (source in sources) { // launch per source launch(start = CoroutineStart.ATOMIC) { // starts even if cancelled first var outcome: Outcome<R> = Outcome.Unanswered try { outcome = permits.withPermit { ask(source) } } finally { finished.trySend(source to outcome) // reports back } } } repeat(sources.size) { // collect as they arrive val (source, outcome) = finished.receive() outcomes[source] = outcome onAnswer(outcomes, sources.size - it - 1) // merge and show now } } Two details matter more than they look.
The first is the finally. Each coroutine starts atomically, so even a source cancelled before its turn reports back, so the collecting loop knows how many answers are still to come and does not wait for one that is gone. If you turn a source off or delete it while a search is running, only that source is dropped. The rest carry on.
The second is onAnswer. It gets everything that has landed so far plus the number still pending, and the screen re-ranks the whole list each time. So the first fast source puts results on screen right away, and a slow source adds its titles later instead of holding everything back.
What "slow" means
A search should not feel as slow as the slowest server you own. There are three limits, all constants in the code:
| Limit | Value |
|---|---|
| Sources asked at once | 3 |
| Time any non-active source has to answer | 8 seconds |
| Time all non-active sources share | 20 seconds |
The active source is not held to these two limits, because it is the one you are looking at. A source that fails or runs out of time is named in a note above the results, so a missing title is explained instead of silent. If every source failed, the error shown is the first real failure, because a failure has a Retry button and "this source has no search" does not.
A newer query cancels everything in flight, so an old answer cannot overwrite a fresher list when you type fast. Each source's lease is let go as soon as it has answered, so the source can be released instead of staying built for the rest of the search.
Merging and ordering
Each source's first 40 titles are ranked, and the combined list is cut at 200. The order is a chain of comparisons, strongest first:
- How the title reads against your query: an exact match, then the same show in another season, then a name that starts with it, contains it, or has all the words.
- Within the same match, a title the active source has comes before the rest.
- The name closest in length to your query.
- The place in the source's own answer.
- The order you keep your sources in.
The same title can live in more than one source, and a list with five copies of one film is noise. So copies are joined into one card. The rule is deliberately strict: a shared database id, or the same normalised name with the same type and the same year. A name alone does not merge two titles, and two copies from one source do not merge, since a source's two copies of a name are its own versions (a dub, a cut).
When several sources have a title, the card shows the active source's copy if it has one, and says how many more have it. The title page lists the other copies under Also on, and each row opens the title on its own source. Where a match is uncertain the rule keeps two cards, and Also on lists the other copies under the lead copy.
Recent searches
With nothing typed, Search lists your recent searches, newest first, up to 10. A search is kept once you open a result from it or press Search on the keyboard, not while you type, so the list holds what you meant instead of every half-typed word. Choose one and it searches again. Hold OK on a television, or press the cross on a phone, to remove one.
A Watch list for what you are not watching yet
Search made a gap obvious. Continue playing is for titles you have started, but a title you find in Search and want to watch tomorrow had nowhere to go.
The Watch list is one list on the device, across every source and independent of the active one. Some choices I want to point at:
- An entry holds what a poster needs (name, type, picture, year, rating) and nothing about playback, so there is nothing to resume.
- Saved is not started, so it has its own Home shelf after Continue playing, hidden when empty.
- A title leaves the list when you play it, because it is in Continue playing then, and when you delete its source.
- Nothing is dropped to make room: you save on purpose, so the list holds 1000 titles.
- On a television the shelf shows the newest 20 and ends in See all, so the remote never has to cross a long row.
- The control is an icon, a plus that turns into a tick beside Play on a television, a playlist icon under Play on a phone, not a wide text button.
Opening a saved title asks its own source, even when another source is active, the same way a Search result does.
What to take from it
If a screen asks several slow things, let each answer land on its own and re-rank on every arrival. Give every ask a way to report back, even when it is cancelled, and say what was left out. Merge strictly, and list the other copies under the lead one, so an uncertain match stays two cards.