@peerbit/document 9.13.10 → 10.0.0-954957e

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.
@@ -1,8 +1,9 @@
1
1
  import { Cache } from "@peerbit/cache";
2
2
  import {
3
- type CollectNextRequest,
4
- SearchRequest,
5
- SearchRequestIndexed,
3
+ CollectNextRequest,
4
+ type IterationRequest,
5
+ type SearchRequest,
6
+ type SearchRequestIndexed,
6
7
  } from "@peerbit/document-interface";
7
8
  import type * as indexerTypes from "@peerbit/indexer-interface";
8
9
 
@@ -11,44 +12,68 @@ export class ResumableIterators<T extends Record<string, any>> {
11
12
  readonly index: indexerTypes.Index<T>,
12
13
  readonly queues = new Cache<{
13
14
  iterator: indexerTypes.IndexIterator<T, undefined>;
14
- request: SearchRequest | SearchRequestIndexed;
15
+ request: SearchRequest | SearchRequestIndexed | IterationRequest;
16
+ keepAlive: boolean;
15
17
  }>({ max: 1e4 }),
16
18
  ) {
17
19
  // TODO choose upper limit better
18
20
  }
19
21
 
20
- async iterateAndFetch(request: SearchRequest | SearchRequestIndexed) {
22
+ async iterateAndFetch(
23
+ request: SearchRequest | SearchRequestIndexed | IterationRequest,
24
+ options?: { keepAlive?: boolean },
25
+ ) {
21
26
  const iterator = this.index.iterate(request);
22
27
  const firstResult = await iterator.next(request.fetch);
23
- if (iterator.done() === true) {
24
- return firstResult;
28
+ const keepAlive = options?.keepAlive === true;
29
+ if (keepAlive || iterator.done() !== true) {
30
+ const cachedIterator = {
31
+ iterator,
32
+ request,
33
+ keepAlive,
34
+ };
35
+ this.queues.add(request.idString, cachedIterator);
25
36
  }
26
- const cachedIterator = {
27
- iterator,
28
- request,
29
- };
30
- this.queues.add(request.idString, cachedIterator);
37
+ /* console.debug(
38
+ "[ResumableIterators] iterateAndFetch",
39
+ request.idString,
40
+ { keepAlive },
41
+ ); */
31
42
  return firstResult;
32
43
  }
33
44
 
34
45
  async next(
35
- request: SearchRequest | SearchRequestIndexed | CollectNextRequest,
36
- iterator = this.queues.get(request.idString),
46
+ request:
47
+ | SearchRequest
48
+ | SearchRequestIndexed
49
+ | IterationRequest
50
+ | CollectNextRequest,
51
+ options?: { keepAlive?: boolean },
37
52
  ) {
53
+ const iterator = this.queues.get(request.idString);
38
54
  if (!iterator) {
39
55
  throw new Error(
40
56
  "Missing iterator for request with id: " + request.idString,
41
57
  );
42
58
  }
43
59
 
44
- const next = await iterator.iterator.next(
45
- request instanceof SearchRequest ||
46
- request instanceof SearchRequestIndexed
47
- ? request.fetch
48
- : request.amount,
49
- );
60
+ if (options?.keepAlive && !iterator.keepAlive) {
61
+ iterator.keepAlive = true;
62
+ this.queues.add(request.idString, iterator);
63
+ }
64
+
65
+ const fetchAmount =
66
+ request instanceof CollectNextRequest
67
+ ? request.amount
68
+ : iterator.request.fetch;
69
+ const next = await iterator.iterator.next(fetchAmount);
70
+ /* console.debug(
71
+ "[ResumableIterators] next",
72
+ request.idString,
73
+ { keepAlive: iterator.keepAlive, resultLength: next.length },
74
+ ); */
50
75
 
51
- if (iterator.iterator.done() === true) {
76
+ if (!iterator.keepAlive && iterator.iterator.done() === true) {
52
77
  this.clear(request.idString);
53
78
  }
54
79
  return next;
@@ -67,12 +92,12 @@ export class ResumableIterators<T extends Record<string, any>> {
67
92
  }
68
93
 
69
94
  async getPending(id: string) {
70
- let iterator = this.queues.get(id);
95
+ const iterator = this.queues.get(id);
71
96
  if (!iterator) {
72
97
  return undefined;
73
98
  }
74
99
  const pending = await iterator.iterator.pending();
75
- if (pending === 0 && iterator.iterator.done()) {
100
+ if (pending === 0 && iterator.iterator.done() && !iterator.keepAlive) {
76
101
  this.clear(id);
77
102
  }
78
103
  return pending;