forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan-iterator.ts
More file actions
189 lines (163 loc) · 4.67 KB
/
Copy pathscan-iterator.ts
File metadata and controls
189 lines (163 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import type {ScanItem} from './scan-item.js';
import type {ScanBound} from './scan-bound.js';
import type {Invoke} from './repm-invoker.js';
import type {JSONValue} from './json.js';
import {throwIfClosed} from './transactions.js';
interface IdCloser {
close(): void;
closed: boolean;
id: number;
}
type ScanIterableKind = 'key' | 'value' | 'entry';
/**
* An async iterator that is used with {@link ReadTransaction.scan}.
*/
class ScanIterator<V> implements AsyncIterableIterator<V> {
private readonly _scanItems: ScanItem[] = [];
private _current = 0;
private readonly _prefix: string;
private readonly _kind: ScanIterableKind;
private readonly _start?: ScanBound;
private readonly _limit?: number;
private readonly _indexName?: string;
private _loadPromise?: Promise<unknown> = undefined;
private readonly _getTransaction: () => Promise<IdCloser> | IdCloser;
private readonly _shouldCloseTransaction: boolean;
private _transaction?: IdCloser = undefined;
private readonly _invoke: Invoke;
constructor(
kind: ScanIterableKind,
prefix: string,
start: ScanBound | undefined,
limit: number | undefined,
indexName: string | undefined,
invoke: Invoke,
getTransaction: () => Promise<IdCloser> | IdCloser,
shouldCloseTranscation: boolean,
) {
this._kind = kind;
this._prefix = prefix;
if (start) {
const key = start.key ?? start.id;
start = {
id: key,
index: start.index,
key,
};
}
this._start = start;
this._limit = limit;
this._indexName = indexName;
this._invoke = invoke;
this._getTransaction = getTransaction;
this._shouldCloseTransaction = shouldCloseTranscation;
}
[Symbol.asyncIterator](): AsyncIterableIterator<V> {
return this;
}
private async _ensureTransaction(): Promise<IdCloser> {
if (!this._transaction) {
this._transaction = await this._getTransaction();
}
return this._transaction;
}
async next(): Promise<IteratorResult<V>> {
throwIfClosed(await this._ensureTransaction());
await this._load();
if (
this._current >= this._scanItems.length ||
(this._limit !== undefined && this._current >= this._limit)
) {
return this.return();
}
const value = this._scanItems[this._current++];
switch (this._kind) {
case 'value':
return {value: value.value} as IteratorResult<V>;
case 'key':
return {value: value.key} as IteratorResult<V>;
case 'entry':
return {value: [value.key, value.value]} as IteratorResult<V>;
}
}
async return(): Promise<IteratorResult<V>> {
if (this._transaction) {
throwIfClosed(this._transaction);
if (this._shouldCloseTransaction) {
this._transaction.close();
}
}
return {done: true, value: undefined};
}
private async _load(): Promise<void> {
if (this._loadPromise) {
await this._loadPromise;
return;
}
if (!this._transaction) {
this._transaction = await this._getTransaction();
}
let start = this._start;
if (start) {
const key = start.key ?? start.id;
start = {
key,
id: key,
index: start.index,
};
}
const opts = {
prefix: this._prefix,
start,
limit: this._limit,
indexName: this._indexName,
};
const responseItems: ScanItem[] = [];
const decoder = new TextDecoder();
const receiver = (k: string, v: Uint8Array) => {
const text = decoder.decode(v);
responseItems.push({
key: k,
value: JSON.parse(text),
});
};
const args = {
transactionId: this._transaction.id,
opts,
receiver,
};
this._loadPromise = this._invoke('scan', args);
await this._loadPromise;
this._scanItems.push(...responseItems);
}
}
type Args = [
prefix: string,
start: ScanBound | undefined,
limit: number | undefined,
indexName: string | undefined,
invoke: Invoke,
getTransaction: () => Promise<IdCloser> | IdCloser,
shouldCloseTranscation: boolean,
];
export class ScanResult implements AsyncIterable<JSONValue> {
private readonly _args: Args;
constructor(...args: Args) {
this._args = args;
}
[Symbol.asyncIterator](): AsyncIterableIterator<JSONValue> {
return this.values();
}
values(): AsyncIterableIterator<JSONValue> {
return this._newIterator('value');
}
keys(): AsyncIterableIterator<string> {
return this._newIterator('key');
}
entries(): AsyncIterableIterator<[string, JSONValue]> {
return this._newIterator('entry');
}
private _newIterator<V>(kind: ScanIterableKind): AsyncIterableIterator<V> {
return new ScanIterator<V>(kind, ...this._args);
}
}