(
def,
args,
fetcher,
onChange,
onDefect,
watchOptions
)
| 6240 | |
| 6241 | // -- watch, peek, get, prefetch --------------------------------------------- |
| 6242 | const watch: CacheInternals["watch"] = ( |
| 6243 | def, |
| 6244 | args, |
| 6245 | fetcher, |
| 6246 | onChange, |
| 6247 | onDefect, |
| 6248 | watchOptions |
| 6249 | ) => { |
| 6250 | const entry = ensureEntry(register(def), args) |
| 6251 | const build = buildOf(fetcher) |
| 6252 | const first = entry.watchers.size === 0 |
| 6253 | const windowed = inWindow(entry) |
| 6254 | if (holds > 0) { |
| 6255 | // Decided at release. The window is read now, as the decision would have been. |
| 6256 | held.add(entry) |
| 6257 | // It takes over any fetch abandoned in this hold. |
| 6258 | abandoned.delete(entry) |
| 6259 | } else if (first && isStale(entry) && !windowed) { |
| 6260 | // Only the zero-to-one transition is a fetch trigger: a joining watcher never refetches. Inside the |
| 6261 | // fetch-free window a hydrated entry isn't fetched. |
| 6262 | ensureFetch(entry, "watch", build, undefined, watchOptions?.watcher) |
| 6263 | } |
| 6264 | const patched = watchOptions?.patched === true |
| 6265 | // A bare Failure that the last watcher leaving would drop is withheld until release, which drops it if no |
| 6266 | // watcher that has seen it survives, so the order of releases and registrations never shows it. |
| 6267 | const provisional = holds > 0 && isBareFailure(entry.result) && !settleOnce && !windowed |
| 6268 | ? entry.result |
| 6269 | : undefined |
| 6270 | const withheld = (): boolean => watcher.provisional !== undefined && watcher.provisional === entry.result |
| 6271 | // What it would read had the Failure already been dropped. |
| 6272 | const provisionalView = (): AsyncResult.AsyncResult<any, any> => AsyncResult.initial(entry.fetch !== undefined) |
| 6273 | const current = provisional !== undefined |
| 6274 | ? { result: provisionalView(), patched: false } |
| 6275 | : patched |
| 6276 | ? patchedViewOf(entry, view(entry)) |
| 6277 | : { result: view(entry), patched: false } |
| 6278 | const watcher: Watcher = { |
| 6279 | onChange, |
| 6280 | onDefect, |
| 6281 | build, |
| 6282 | lastMore: moreOf(entry), |
| 6283 | patched, |
| 6284 | path: watchOptions?.watcher, |
| 6285 | cause: watchOptions?.cause, |
| 6286 | generation: holds > 0 ? generation : -1, |
| 6287 | windowed, |
| 6288 | provisional, |
| 6289 | last: current.result, |
| 6290 | lastPatched: current.patched, |
| 6291 | active: true |
| 6292 | } |
| 6293 | entry.watchers.add(watcher) |
| 6294 | cancelIdle(entry) |
| 6295 | if (first && holds === 0) scheduleInterval(entry) |
| 6296 | return { |
| 6297 | logKey: () => logKeyOf(entry), |
| 6298 | current: () => withheld() ? provisionalView() : view(entry), |
| 6299 | patchedView: () => |
nothing calls this directly
no test coverage detected