forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepm-invoker.ts
More file actions
209 lines (175 loc) · 4.53 KB
/
Copy pathrepm-invoker.ts
File metadata and controls
209 lines (175 loc) · 4.53 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import type {JSONValue, ToJSON} from './json.js';
import type {ScanOptions} from './scan-options.js';
import init, {dispatch} from './wasm/release/replicache_client.js';
import type {InitInput, InitOutput} from './wasm/release/replicache_client.js';
// TODO(repc-switchover): isWasm can go away, but so can this whole
// type and all the machinery connected to it. Look at the commit
// that introduced this to unwind it.
export type Invoker = {
readonly invoke: REPMInvoke;
};
export interface Invoke {
<Rpc extends keyof InvokeMapNoArgs>(rpc: Rpc): Promise<InvokeMapNoArgs[Rpc]>;
<Rpc extends keyof InvokeMap>(rpc: Rpc, args: InvokeMap[Rpc][0]): Promise<
InvokeMap[Rpc][1]
>;
(rpc: string, args?: JSONValue | ToJSON): Promise<JSONValue>;
}
export interface REPMInvoke {
<Rpc extends keyof InvokeMapNoArgs>(dbName: string, rpc: Rpc): Promise<
InvokeMapNoArgs[Rpc]
>;
<Rpc extends keyof InvokeMap>(
dbName: string,
rpc: Rpc,
args: InvokeMap[Rpc][0],
): Promise<InvokeMap[Rpc][1]>;
(dbName: string, rpc: string, args?: JSONValue | ToJSON): Promise<JSONValue>;
}
let wasmModuleOutput: Promise<InitOutput> | undefined;
export class REPMWasmInvoker {
constructor(wasmModuleOrPath?: InitInput) {
if (!wasmModuleOutput) {
wasmModuleOutput = init(wasmModuleOrPath);
}
}
invoke: REPMInvoke = async (
dbName: string,
rpc: string,
args: JSONValue | ToJSON = {},
): Promise<JSONValue> => {
console.debug('>', dbName, rpc, args);
await wasmModuleOutput;
const json = await dispatch(dbName, rpc, JSON.stringify(args));
const ret = json == '' ? null : JSON.parse(json);
console.debug('<', dbName, rpc, ret);
return ret;
};
}
type GetRequest = TransactionRequest & {
key: string;
};
type GetResponse = {
has?: boolean;
value: string;
};
type HasRequest = TransactionRequest & {
key: string;
};
type HasResponse = {
has: boolean;
};
type TransactionRequest = {
transactionId: number;
};
interface ScanItemResponse {
readonly key: string;
readonly value: string;
}
export type ScanRequest = TransactionRequest &
ScanOptions & {
opts?: ScanOptions;
};
export type ScanResponse = {
items: ScanItemResponse[];
};
type PutRequest = TransactionRequest & {
key: string;
value: string;
};
type PutResponse = unknown;
type DelRequest = TransactionRequest & {key: string};
type DelResponse = {ok: boolean};
type RebaseOpts =
| Record<string, unknown>
| {
basis: string;
original: string;
};
export type OpenTransactionRequest = {
name?: string;
args?: JSONValue | ToJSON;
rebaseOpts?: RebaseOpts;
};
type OpenTransactionResponse = {
transactionId: number;
};
type CloseTransactionRequest = TransactionRequest;
type CloseTransactionResponse = unknown;
type CommitTransactionRequest = TransactionRequest;
export type CommitTransactionResponse =
| {
retryCommit: false;
ref: string;
}
| {
retryCommit: true;
};
type BeginSyncRequest = {
batchPushURL: string;
dataLayerAuth: string;
diffServerURL: string;
diffServerAuth: string;
};
type MutationInfo = {
id: number;
error: string;
};
type BatchPushResponse = {
mutationInfos?: MutationInfo[];
};
type BatchPushInfo = {
httpStatusCode: number;
errorMessage: string;
batchPushResponse: BatchPushResponse;
};
type ClientViewInfo = {
httpStatusCode: number;
errorMessage: string;
};
type SyncInfo = {
syncID: string;
batchPushInfo?: BatchPushInfo;
clientViewInfo: ClientViewInfo;
};
export type BeginSyncResponse = {
syncHead: string;
syncInfo: SyncInfo;
};
type MaybeEndSyncRequest = {
syncID?: string;
syncHead?: string;
};
type Mutation = {
id: number;
name: string;
args: JSONValue;
};
type ReplayMutation = Mutation & {
original: string;
};
type MaybeEndSyncResponse = {
replayMutations?: ReplayMutation[];
};
export type InvokeMap = {
get: [GetRequest, GetResponse];
has: [HasRequest, HasResponse];
scan: [ScanRequest, ScanResponse];
put: [PutRequest, PutResponse];
del: [DelRequest, DelResponse];
openTransaction: [OpenTransactionRequest, OpenTransactionResponse];
closeTransaction: [CloseTransactionRequest, CloseTransactionResponse];
commitTransaction: [CommitTransactionRequest, CommitTransactionResponse];
beginSync: [BeginSyncRequest, BeginSyncResponse];
maybeEndSync: [MaybeEndSyncRequest, MaybeEndSyncResponse];
};
type OpenResponse = '';
type CloseResponse = '';
type GetRootResponse = {
root: string;
};
export type InvokeMapNoArgs = {
open: OpenResponse;
close: CloseResponse;
getRoot: GetRootResponse;
};