@procedures/client 0.48.1 → 0.48.2

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.
@@ -22,6 +22,16 @@
22
22
  * listens to the topic, the procedures publish nothing, and the chain stops.
23
23
  * - The publishes of one storage and one topic run one at a time, in the order of the writes. A
24
24
  * write resolves after its event is on the bus. A failed publish never fails the write.
25
+ * - The writes that end while a publish runs share the next publish, so one read of the change
26
+ * log replies to all of them. At most one publish runs, and at most one publish waits.
27
+ * - A version can stop below a write in flight (`StorageChanges.held`, roadmap CLI-102). The chain
28
+ * then reads the log again, after a wait that starts at 100 ms and doubles up to 5 s. It stops when
29
+ * no write holds the version or no one listens. Thus a write that a write in flight holds back
30
+ * reaches the subscribers with no later write. The event of such a read has no origin. It has
31
+ * no rows before the changes, so a row filter makes a hint without ids.
32
+ * - The log can give a write two times (`StorageChanges`), so a subscriber applies a change by its id.
33
+ * - A read with no change sends no event, and the chain keeps its version. The version of a log can
34
+ * move with no change, when another process writes a row again at a later number.
25
35
  * - With the rule `admission.admit`, the publisher filters the ids of each event for each
26
36
  * subscriber (roadmap CLI-78, `hint-filter.ts`). The event carries the rows after the changes,
27
37
  * from the change log. While a subscriber has a filter, each write first reads the rows before
@@ -103,6 +113,11 @@ interface ChangeLog {
103
113
  getBatch?(ids: string[]): Promise<Map<string, unknown>>;
104
114
  }
105
115
 
116
+ /** The first wait before a read again of a log that a write in flight holds back. */
117
+ const HELD_FIRST_MS = 100;
118
+ /** The longest wait before a read again. */
119
+ const HELD_MOST_MS = 5000;
120
+
106
121
  /** A write that ended, and that no event told yet. */
107
122
  interface EndedWrite {
108
123
  /** The ids of the write, or "*" for a clear. */
@@ -120,8 +135,30 @@ interface ChangeChain {
120
135
  anchor: string | undefined;
121
136
  /** The publishes, one at a time. */
122
137
  tail: Promise<void>;
138
+ /** The publish that did not start. A write that ends joins it, because it reads the writes that ended before its start. */
139
+ next: Promise<void> | undefined;
123
140
  /** The writes that ended since the last publish. */
124
141
  readonly ended: EndedWrite[];
142
+ /** True when the next publish reads the log with no write that ended. */
143
+ owed: boolean;
144
+ /** The last wait before a read again, or 0 when no write in flight holds the log back. */
145
+ heldWaitMs: number;
146
+ /** The timer of the next read again. */
147
+ heldTimer: ReturnType<typeof setTimeout> | undefined;
148
+ }
149
+
150
+ /** This function gives the publish that did not start, and it makes one when none waits. A failed publish rejects nothing here. */
151
+ function nextPublish(chain: ChangeChain, start: () => Promise<void>): Promise<void> {
152
+ if (chain.next) return chain.next;
153
+ const next = chain.tail
154
+ .then(() => {
155
+ chain.next = undefined;
156
+ return start();
157
+ })
158
+ .catch(() => undefined);
159
+ chain.next = next;
160
+ chain.tail = next;
161
+ return next;
125
162
  }
126
163
 
127
164
  /** The chains of each storage, by topic. A storage that goes away takes its chains with it. */
@@ -135,7 +172,7 @@ function chainOf(storage: object, topic: string): ChangeChain {
135
172
  }
136
173
  let chain = topics.get(topic);
137
174
  if (!chain) {
138
- chain = { anchor: undefined, tail: Promise.resolve(), ended: [] };
175
+ chain = { anchor: undefined, tail: Promise.resolve(), next: undefined, ended: [], owed: false, heldWaitMs: 0, heldTimer: undefined };
139
176
  topics.set(topic, chain);
140
177
  }
141
178
  return chain;
@@ -187,20 +224,41 @@ function withValues(event: CollectionChangeEvent, entries: ReadonlyArray<{ reado
187
224
  );
188
225
  }
189
226
 
190
- /** One publish: the event of the writes that ended since the last publish. */
227
+ /** This function starts the timer of the next read again while a write holds the log back, and it stops the timer otherwise. */
228
+ function whileHeld(chain: ChangeChain, held: boolean, read: () => void): void {
229
+ if (!held) {
230
+ chain.heldWaitMs = 0;
231
+ if (chain.heldTimer !== undefined) clearTimeout(chain.heldTimer);
232
+ chain.heldTimer = undefined;
233
+ return;
234
+ }
235
+ if (chain.heldTimer !== undefined) return;
236
+ chain.heldWaitMs = Math.min(HELD_MOST_MS, chain.heldWaitMs * 2 || HELD_FIRST_MS);
237
+ chain.heldTimer = setTimeout(() => {
238
+ chain.heldTimer = undefined;
239
+ read();
240
+ }, chain.heldWaitMs);
241
+ chain.heldTimer.unref?.();
242
+ }
243
+
244
+ /** One publish: the event of the writes that ended since the last publish, and of a read again. */
191
245
  async function publish(chain: ChangeChain, storage: ChangeLog, bus: EventBus, topic: string, values: boolean, admission: CollectionAdmission | undefined): Promise<void> {
192
246
  const admit = admission?.admit;
193
247
  const writes = chain.ended.splice(0);
248
+ const owed = chain.owed;
249
+ chain.owed = false;
194
250
  // An earlier publish told these writes
195
- if (writes.length === 0) return;
251
+ if (writes.length === 0 && !owed) return;
252
+ const readAgain = (): void => rereadChain(chain, storage, bus, topic, values, admission);
196
253
  if (!bus.hasSubscribers(topic)) {
197
254
  // No one listens: the procedures do not read the log. The next call of `changes` starts the chain again.
198
255
  chain.anchor = undefined;
256
+ whileHeld(chain, false, readAgain);
199
257
  return;
200
258
  }
201
259
  const since = chain.anchor;
202
260
  if (!storage.getChangesSince || since === undefined) {
203
- bus.emit(topic, hint(undefined, originOf(writes, undefined)));
261
+ if (writes.length > 0) bus.emit(topic, hint(undefined, originOf(writes, undefined)));
204
262
  return;
205
263
  }
206
264
  let changes: StorageChanges<unknown>;
@@ -208,16 +266,21 @@ async function publish(chain: ChangeChain, storage: ChangeLog, bus: EventBus, to
208
266
  changes = await storage.getChangesSince(since);
209
267
  } catch {
210
268
  // The subscribers read the changes themselves. The chain keeps its version.
211
- bus.emit(topic, hint(undefined, originOf(writes, undefined)));
269
+ if (writes.length > 0) bus.emit(topic, hint(undefined, originOf(writes, undefined)));
270
+ // A read again that failed told no subscriber, so it stays owed
271
+ else whileHeld(chain, true, readAgain);
212
272
  return;
213
273
  }
214
- chain.anchor = changes.version;
274
+ whileHeld(chain, changes.held === true, readAgain);
215
275
  // A full result: the log does not know the version of the chain any more
216
276
  if (changes.full || writes.some((write) => !write.anchored)) {
277
+ chain.anchor = changes.version;
217
278
  bus.emit(topic, hint(changes.version, originOf(writes, undefined)));
218
279
  return;
219
280
  }
281
+ // No event tells this version, so the chain stays at the version of the last event. The next read from it gets each change.
220
282
  if (changes.changed.length === 0 && changes.deleted.length === 0) return;
283
+ chain.anchor = changes.version;
221
284
  const origin = originOf(writes, changes);
222
285
  const event: CollectionChangeEvent = {
223
286
  since,
@@ -231,6 +294,12 @@ async function publish(chain: ChangeChain, storage: ChangeLog, bus: EventBus, to
231
294
  bus.emit(topic, admit ? withHintImages(out, { after: afterOf(changes), before: beforeOf(writes), admission: admission! }) : out);
232
295
  }
233
296
 
297
+ /** This function reads the log of a chain again. A publish that did not start does this read. */
298
+ function rereadChain(chain: ChangeChain, storage: ChangeLog, bus: EventBus, topic: string, values: boolean, admission: CollectionAdmission | undefined): void {
299
+ chain.owed = true;
300
+ void nextPublish(chain, () => publish(chain, storage, bus, topic, values, admission));
301
+ }
302
+
234
303
  /** The rows after the changes, from the change log: a changed id has its item, a deleted id has none. */
235
304
  function afterOf(changes: StorageChanges<unknown>): Map<string, HintRowState> {
236
305
  const after = new Map<string, HintRowState>();
@@ -295,9 +364,7 @@ export async function writeWithChangeEvent<R>(
295
364
  return await write();
296
365
  } finally {
297
366
  chain.ended.push(started);
298
- const run = chain.tail.then(() => publish(chain, storage, bus, topic, values, admission));
299
- chain.tail = run.catch(() => undefined);
300
- await chain.tail;
367
+ await nextPublish(chain, () => publish(chain, storage, bus, topic, values, admission));
301
368
  }
302
369
  }
303
370
 
@@ -311,3 +378,14 @@ export function startChangeChain(bus: EventBus | undefined, storage: object, top
311
378
  const chain = chainOf(storage, topic);
312
379
  if (chain.anchor === undefined) chain.anchor = version;
313
380
  }
381
+
382
+ /**
383
+ * This function makes the chain read the log again when a call of `changes` gets a version that a
384
+ * write in flight holds back. A write outside the procedures then reaches the subscribers too.
385
+ * With no bus, no topic or no subscriber, it does nothing.
386
+ */
387
+ export function rereadWhileHeld(bus: EventBus | undefined, storage: object & ChangeLog, topic: string | undefined, values = false, admission?: CollectionAdmission): void {
388
+ if (bus === undefined || topic === undefined || !bus.hasSubscribers(topic)) return;
389
+ const chain = chainOf(storage, topic);
390
+ whileHeld(chain, true, () => rereadChain(chain, storage, bus, topic, values, admission));
391
+ }
@@ -58,7 +58,7 @@ import {
58
58
  } from "./schemas.js";
59
59
  import { DEFAULT_FIND_LIMIT, queryEntries, type StorageQueryOptions, type StorageQueryPolicy } from "./query.js";
60
60
  import type { CollectionStorage, StorageBatchWriteOptions, StorageItemWriteOptions, StorageWriteOptions } from "./storage.js";
61
- import { collectionChangeTopic, startChangeChain, writeWithChangeEvent } from "./changes.js";
61
+ import { collectionChangeTopic, rereadWhileHeld, startChangeChain, writeWithChangeEvent } from "./changes.js";
62
62
  import { partitionOfCall, storageOfCall, withPartition, type PartitionInput } from "./partition.js";
63
63
  import { admitsRow, admittedStorage, checkAdmission, type CollectionAdmission } from "./admission.js";
64
64
  import { patchItem } from "./patch-in-parts.js";
@@ -339,7 +339,12 @@ export function createCollectionProcedures(collectionName: string, options: Coll
339
339
  const changes = await view.getChangesSince(input.since);
340
340
  // The chain of the change events starts at this version: the caller then applies the
341
341
  // next event without a read (roadmap CLI-15)
342
- if (topicRule) startChangeChain(ctx.bus, storage, topicOf(ctx, partition), changes.version);
342
+ if (topicRule) {
343
+ const topic = topicOf(ctx, partition);
344
+ startChangeChain(ctx.bus, storage, topic, changes.version);
345
+ // A write in flight holds this read back, so the chain reads again until the write ends (roadmap CLI-102)
346
+ if (changes.held) rereadWhileHeld(ctx.bus, storage, topic, withValues, admission);
347
+ }
343
348
  return {
344
349
  version: changes.version,
345
350
  full: changes.full,
@@ -185,6 +185,11 @@ export type StorageObserver<T> = (changes: StorageChangeBatch<T>) => void;
185
185
  * has the ids that were deleted after it.
186
186
  * - The order of a full result is the order of the entries. The order of a delta is the order of
187
187
  * the last change of each id.
188
+ * - A result gives each write at least one time. A write can come again in two cases, with the same
189
+ * value or a later value. A full result takes its version before it lists the items, so it can
190
+ * hold a write above that version, and the next delta gives the write again. A write that ends
191
+ * after its lease can come again in a later delta, with a new version. A consumer applies a change
192
+ * by its id, and does not count changes.
188
193
  */
189
194
  export interface StorageChanges<T> {
190
195
  /** The version of the storage that includes these changes. */
@@ -195,6 +200,11 @@ export interface StorageChanges<T> {
195
200
  changed: Array<[string, T]>;
196
201
  /** The ids that were deleted. Empty when `full` is true. */
197
202
  deleted: string[];
203
+ /**
204
+ * True when this version stops below a write in flight. A later read can give more changes, with no
205
+ * new write.
206
+ */
207
+ held?: boolean;
198
208
  }
199
209
 
200
210
  /** The metadata that a storage gives about itself. */