src/read/sources.ts is the boundary between the root contract and the read loaders. It exports a sources object that app.ts merges with every other package's, see The data module.
Sources
Source | Arguments | Returns | Asked by |
|---|---|---|---|
|
|
| mutation |
|
|
| resource |
|
|
| resource |
|
|
| resource |
Argument types come from the generated app.contract.d.ts (Sources), so a change to a resource call in app.contract is a type error here until the source matches. See The Contract language.
The epoch
Pull to refresh does not call a "refresh" function. The root action readRefresh increments the state readEpoch, which is an argument of all three resources, so Exact asks them again. In the module, epochSeen(server, epoch) compares the argument with the last epoch seen for that server and, when it differs, calls forgetAll(server) before loading:
a new epoch drops the module caches for that server, so the next answer is fresh (the old one stays on screen while it loads)
Facts worth knowing:
Epochs are tracked per server string in a Map. The Bun test "refresh epochs are tracked independently for each server" covers this.
The three sources share the map, so whichever is asked first with a new epoch clears the caches for the others.
A server never seen before compares against 0. If readEpoch is already above 0 the first ask for a new server clears caches that are empty anyway.
Dropping caches bumps the server's generation, so answers still in flight from the old epoch cannot write back (caches and generations).
The Retry buttons use Contract's refresh docs (and refresh directory, refresh authors) instead. That re-asks with the same epoch, which works because failures are never cached.
The general pattern, and why sources receive time and epochs as arguments instead of reading them, is in State, refresh and stale-reply patterns.
The exactTime workaround
The exported object adds one more key (L35-L36):
export const sources = {...readSources, exactTime: async () => ({epochAtZero: 0, utcOffset: 0})} as Partial<Sources>The comment at L17-L20 explains it. exactTime is a reserved source that browser and native hosts answer themselves. The web build's server render "installs no reserved exactTime answer, so once the app has any TS source the prerender asks the data module for it and the build fails". This stub is reached only by that prerender. The comment notes "The lead may move this into app.ts". It is typed outside Sources because Sources has no exactTime key. See The web host for development and The Exact framework.
Tests
The sources are exercised through the loaders in load.test.ts (7 tests) and end to end by read.test.contract (12 tests) and open-site.test.contract (4 tests), see Contract UI tests.
Related
Read: root contract wiring: the resources and actions that call these sources
Discuss: loading and posting: the same epoch pattern, driven by discussEpoch + readEpoch
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime