MCPcopy Create free account
hub / github.com/Effect-TS/effect / watch

Function watch

packages/effect/src/ui/Query.ts:6242–6322  ·  view source on GitHub ↗
(
    def,
    args,
    fetcher,
    onChange,
    onDefect,
    watchOptions
  )

Source from the content-addressed store, hash-verified

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: () =>

Callers

nothing calls this directly

Calls 15

ensureEntryFunction · 0.85
buildOfFunction · 0.85
ensureFetchFunction · 0.85
isBareFailureFunction · 0.85
provisionalViewFunction · 0.85
patchedViewOfFunction · 0.85
cancelIdleFunction · 0.85
scheduleIntervalFunction · 0.85
logKeyOfFunction · 0.85
withheldFunction · 0.85
stopIntervalFunction · 0.85
abandonFetchFunction · 0.85

Tested by

no test coverage detected