@swirls/looms 0.21.0 → 0.22.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @swirls/looms
2
2
 
3
+ ## 0.22.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 9277d5f: A rebuilt Durable Object no longer drops a wake its alarm was holding. When an object was evicted with its alarm set for a wake owed now (from `launch` or a signal sent with `wake: 'schedule'`) or for a liveness deadline, and a request rebuilt it before that alarm fired, the new object replaced the alarm with the later deadline read back from run state, and the run sat until then. The alarm an object starts with is now kept until the runtime asks for a wake at or before it, or an alarm fires.
8
+
9
+ ## 0.22.0
10
+
11
+ ### Minor Changes
12
+
13
+ - a651e3b: `LoomsProvider`, `preloadRunStore` and `createLoomsStore` take `fetch`, `webSocket` and `grant`, so a page on another origin than its host can read a run: `fetch` adds the host's credentials to every HTTP request, and `grant` puts a run's read grant on the requests that read the log, the WebSocket included.
14
+
3
15
  ## 0.21.0
4
16
 
5
17
  ### Minor Changes
@@ -12,17 +12,25 @@ export interface DurableObjectAlarms {
12
12
  readonly scheduleAtEarliest: (at: number) => Effect.Effect<void>;
13
13
  /**
14
14
  * Run when the alarm fires, before waking. A runtime deadline that is already due is being
15
- * delivered by this alarm, so it is forgotten instead of being set again by a later rearm.
15
+ * delivered by this alarm, so it is forgotten instead of being set again by a later rearm. So is
16
+ * an alarm inherited from an earlier instance.
16
17
  */
17
18
  readonly fired: Effect.Effect<void>;
18
19
  }
19
20
  /**
20
21
  * The single owner of a Durable Object's one alarm. Every write is serialized and sets the alarm to
21
22
  * the earliest of the runtime deadline, the liveness deadline of a wake with in-process effects in
22
- * flight, `additionalDeadline` (such as projector retries), and the requested time, so concurrent
23
- * writers cannot overwrite each other's deadlines.
23
+ * flight, `additionalDeadline` (such as projector retries), the requested time, and an alarm
24
+ * inherited from an earlier instance, so concurrent writers cannot overwrite each other's
25
+ * deadlines.
24
26
  *
25
27
  * The liveness deadline is what recovers a run whose isolate is lost mid-effect: the live wake
26
28
  * keeps pushing it forward, and once nothing does it fires in a fresh isolate, which wakes the run.
29
+ *
30
+ * An alarm already set when this instance starts was set by an earlier one, and may be the only
31
+ * record of a wake that instance owed: one asked for now by `launch` or a scheduled signal, or its
32
+ * liveness deadline. Run state does not hold those, so the deadline the runtime reads back from
33
+ * state can be much later. The inherited alarm is kept until the runtime asks for a wake at or
34
+ * before it, or an alarm fires.
27
35
  */
28
36
  export declare function durableObjectAlarms(storage: AlarmStorage, additionalDeadline?: () => Effect.Effect<number | null>): DurableObjectAlarms;
@@ -6,24 +6,45 @@ function earliest(...deadlines) {
6
6
  /**
7
7
  * The single owner of a Durable Object's one alarm. Every write is serialized and sets the alarm to
8
8
  * the earliest of the runtime deadline, the liveness deadline of a wake with in-process effects in
9
- * flight, `additionalDeadline` (such as projector retries), and the requested time, so concurrent
10
- * writers cannot overwrite each other's deadlines.
9
+ * flight, `additionalDeadline` (such as projector retries), the requested time, and an alarm
10
+ * inherited from an earlier instance, so concurrent writers cannot overwrite each other's
11
+ * deadlines.
11
12
  *
12
13
  * The liveness deadline is what recovers a run whose isolate is lost mid-effect: the live wake
13
14
  * keeps pushing it forward, and once nothing does it fires in a fresh isolate, which wakes the run.
15
+ *
16
+ * An alarm already set when this instance starts was set by an earlier one, and may be the only
17
+ * record of a wake that instance owed: one asked for now by `launch` or a scheduled signal, or its
18
+ * liveness deadline. Run state does not hold those, so the deadline the runtime reads back from
19
+ * state can be much later. The inherited alarm is kept until the runtime asks for a wake at or
20
+ * before it, or an alarm fires.
14
21
  */
15
22
  export function durableObjectAlarms(storage, additionalDeadline) {
16
- // `undefined` until the runtime reports a deadline; until then an existing alarm is preserved.
23
+ // `undefined` until the runtime reports a deadline.
17
24
  let runtimeDeadline;
18
25
  let livenessDeadline = null;
26
+ // The alarm an earlier instance left set. `undefined` until the first write reads it.
27
+ let inherited;
19
28
  const writes = Semaphore.makeUnsafe(1);
20
29
  const rearm = (requested) => Semaphore.withPermits(writes, 1, Effect.gen(function* () {
21
30
  const [additional, current] = yield* Effect.all([
22
31
  additionalDeadline?.() ?? Effect.succeed(null),
23
32
  Effect.promise(() => storage.getAlarm?.() ?? Promise.resolve(null)),
24
33
  ], { concurrency: 'unbounded' });
34
+ if (inherited === undefined) {
35
+ inherited = current;
36
+ }
37
+ // A wake the runtime asks for at or before the inherited alarm stands in for it.
38
+ if (inherited !== null &&
39
+ runtimeDeadline !== undefined &&
40
+ runtimeDeadline !== null &&
41
+ runtimeDeadline <= inherited) {
42
+ inherited = null;
43
+ }
44
+ // Until the runtime reports, nothing here knows what the alarm is for, so it only moves
45
+ // earlier.
25
46
  const preserved = runtimeDeadline === undefined ? current : null;
26
- const desired = earliest(runtimeDeadline, livenessDeadline, additional, requested, preserved);
47
+ const desired = earliest(runtimeDeadline, livenessDeadline, additional, requested, inherited, preserved);
27
48
  if (desired === current) {
28
49
  return;
29
50
  }
@@ -46,6 +67,8 @@ export function durableObjectAlarms(storage, additionalDeadline) {
46
67
  },
47
68
  scheduleAtEarliest: rearm,
48
69
  fired: Clock.currentTimeMillis.pipe(Effect.map((now) => {
70
+ // The wake that follows does whatever the inherited alarm was owed for.
71
+ inherited = null;
49
72
  if (runtimeDeadline !== undefined && runtimeDeadline !== null && runtimeDeadline <= now) {
50
73
  runtimeDeadline = null;
51
74
  }