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
119 lines (101 loc) · 2.8 KB
/
Copy pathscan-iterator.ts
File metadata and controls
119 lines (101 loc) · 2.8 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
import type {Invoke, ScanRequest} from './repm-invoker.js';
import type {JSONValue} from './json.js';
import {throwIfClosed} from './transaction-closed-error.js';
import {ScanOptions, toRPC} from './scan-options.js';
interface IdCloser {
close(): void;
closed: boolean;
id: number;
}
const VALUE = 0;
const KEY = 1;
const ENTRY = 2;
type ScanIterableKind = typeof VALUE | typeof KEY | typeof ENTRY;
type Args = [
options: ScanOptions | undefined,
invoke: Invoke,
getTransaction: () => Promise<IdCloser> | IdCloser,
shouldCloseTransaction: boolean,
];
export class ScanResult<K> 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<K> {
return this._newIterator(KEY);
}
entries(): AsyncIterableIterator<[K, JSONValue]> {
return this._newIterator(ENTRY);
}
private _newIterator<V>(kind: ScanIterableKind): AsyncIterableIterator<V> {
return scanIterator(kind, ...this._args);
}
}
async function* scanIterator<V>(
kind: ScanIterableKind,
options: ScanOptions | undefined,
invoke: Invoke,
getTransaction: () => Promise<IdCloser> | IdCloser,
shouldCloseTransaction: boolean,
): AsyncGenerator<V> {
const transaction = await getTransaction();
throwIfClosed(transaction);
const items = await load<V>(kind, options, transaction.id, invoke);
try {
for (const item of items) {
yield item;
}
} finally {
if (shouldCloseTransaction && !transaction.closed) {
transaction.close();
}
}
}
async function load<V>(
kind: ScanIterableKind,
options: ScanOptions | undefined,
transactionID: number,
invoke: Invoke,
): Promise<V[]> {
const items: V[] = [];
const decoder = new TextDecoder();
const parse = (v: Uint8Array) => JSON.parse(decoder.decode(v));
type MaybeIndexName = {indexName?: string};
const key = (primaryKey: string, secondaryKey: string | null) =>
(options as MaybeIndexName)?.indexName !== undefined
? [secondaryKey, primaryKey]
: primaryKey;
const receiver = (
primaryKey: string,
secondaryKey: string | null,
value: Uint8Array,
) => {
switch (kind) {
case VALUE:
items.push(parse(value));
return;
case KEY:
items.push((key(primaryKey, secondaryKey) as unknown) as V);
return;
case ENTRY:
items.push(([
key(primaryKey, secondaryKey),
parse(value),
] as unknown) as V);
}
};
const args: ScanRequest = {
transactionId: transactionID,
opts: toRPC(options),
receiver,
};
await invoke('scan', args);
return items;
}