@crombolo/render 0.40.0 → 0.42.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/render",
3
- "version": "0.40.0",
3
+ "version": "0.42.1",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -34,8 +34,8 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@crombolo/common": "0.40.0",
38
- "@crombolo/core": "0.40.0",
37
+ "@crombolo/common": "0.42.1",
38
+ "@crombolo/core": "0.42.1",
39
39
  "@types/react": "^19.3.0"
40
40
  }
41
41
  }
@@ -303,6 +303,11 @@ export interface PublishedCacheOptions {
303
303
  * menus look items up here. Without one, nothing resolves to media.
304
304
  */
305
305
  media?: MediaCache
306
+ /**
307
+ * How long a reader waits for a rebuild when there is a previous snapshot to
308
+ * answer from instead (story 230). 50 ms by default.
309
+ */
310
+ readerGraceMs?: number
306
311
  }
307
312
 
308
313
  const NO_MEDIA = () => Promise.resolve(undefined)
@@ -410,18 +415,29 @@ export class PublishedCache {
410
415
  }
411
416
 
412
417
  /**
413
- * The current snapshot, building it if it was dropped. The reader that starts
414
- * a build, or queues the one after it, waits for it; readers arriving while
415
- * one builds are answered from the previous snapshot rather than waiting
416
- * (story 175). Before the first build there is none, so they share the build.
418
+ * The current snapshot, building it if it was dropped. Readers arriving while
419
+ * one builds are answered from the previous snapshot (story 175); the reader
420
+ * that starts a build, or queues the one after it, waits for it only for the
421
+ * grace, then is answered from the previous snapshot too (story 230). Before
422
+ * the first build there is none, so they share the build.
417
423
  */
418
424
  async snapshot(): Promise<CacheSnapshot> {
419
425
  if (this.#loaded) return this.#snapshot
420
426
  const generation = this.#generation
421
427
  const previous = this.#snapshotGeneration >= 0 ? this.#snapshot : undefined
422
428
  if (this.#building?.generation === generation) return previous ?? this.#building.done
423
- if (this.#building) return this.#next && previous ? previous : this.#followUp()
424
- return this.#start(generation)
429
+ if (this.#building && this.#next && previous) return previous
430
+ const build = this.#building ? this.#followUp() : this.#start(generation)
431
+ return previous ? this.#within(build, previous) : build
432
+ }
433
+
434
+ /** The build if it finishes within the grace, otherwise the previous snapshot. */
435
+ #within(build: Promise<CacheSnapshot>, previous: CacheSnapshot): Promise<CacheSnapshot> {
436
+ let timer: ReturnType<typeof setTimeout> | undefined
437
+ const grace = new Promise<CacheSnapshot>((resolve) => {
438
+ timer = setTimeout(() => resolve(previous), this.#options.readerGraceMs ?? 50)
439
+ })
440
+ return Promise.race([build, grace]).finally(() => clearTimeout(timer))
425
441
  }
426
442
 
427
443
  /** Whether readers are being answered from a snapshot older than the latest change. */