You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement resource cache semantics phase 1: `cache.staleTime` for time-based staleness and `cache.deduplicate` for in-flight load/reload deduplication. Resources without `cache` options behave exactly as before.
Copy file name to clipboardExpand all lines: docs/Resources.md
+53-2Lines changed: 53 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -251,14 +251,65 @@ On failure, `error` contains the error message and `status` is `"failed"`.
251
251
252
252
See the [Inspect Screen and Diagnostics Guide](Inspect-Screen.md) for more detail.
253
253
254
+
## Cache options (Phase 1)
255
+
256
+
Resources support optional `cache` configuration:
257
+
258
+
```ts
259
+
const team =$.resource("team", {
260
+
load: async () =>loadTeam(),
261
+
cache: {
262
+
staleTime: 5_000, // ms (optional, default: Infinity — no time-based stale)
263
+
deduplicate: true, // boolean (optional, default: true when cache is set)
264
+
},
265
+
})
266
+
```
267
+
268
+
### staleTime
269
+
270
+
`cache.staleTime` is an optional number in milliseconds. After a successful `load()` or `reload()`, a timer starts. When it fires, the resource transitions to stale automatically:
271
+
272
+
-`resource.stale` becomes `true`
273
+
-`resource.status` remains `"ready"`
274
+
-`resource.value` remains available
275
+
- Subscribers to the `stale` condition are notified
276
+
277
+
Behavior:
278
+
279
+
-**Timer resets** on every successful `load()` or `reload()`.
280
+
-**`invalidate()`** always marks stale immediately, regardless of `staleTime`.
281
+
-**Failed loads** do not start a stale timer.
282
+
-**`dispose()`** clears the stale timer and prevents late notifications.
283
+
284
+
### deduplicate
285
+
286
+
`cache.deduplicate` is an optional boolean. When `true`, concurrent `load()` and `reload()` calls while a load is already pending share the same in-flight promise:
287
+
288
+
- Only one loader invocation runs.
289
+
- All callers resolve or fail with the same result.
290
+
- The in-flight promise is cleared when the load completes (success or failure).
291
+
292
+
When `false`, each call runs independently (preserving existing behavior).
293
+
294
+
When `cache` is not set at all, deduplication is disabled to preserve backward compatibility. When `cache` is set, `deduplicate` defaults to `true`.
295
+
296
+
### Future cache options
297
+
298
+
The following cache options remain as future work and are **not yet supported**:
299
+
300
+
-**`cache.key`** — cache key function for parameterized resources
301
+
-**`cacheTime`** — retention period for stale values before eviction
0 commit comments