@phibac/sdk 0.0.0-stage → 0.1.0
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.
- package/LICENSE +202 -0
- package/README.md +53 -2
- package/dist/auth.d.ts +104 -0
- package/dist/auth.js +343 -0
- package/dist/auth.js.map +1 -0
- package/dist/brand.d.ts +7 -0
- package/dist/brand.js +8 -0
- package/dist/brand.js.map +1 -0
- package/dist/cache.d.ts +170 -0
- package/dist/cache.js +293 -0
- package/dist/cache.js.map +1 -0
- package/dist/collection.d.ts +119 -0
- package/dist/collection.js +511 -0
- package/dist/collection.js.map +1 -0
- package/dist/dpop.d.ts +50 -0
- package/dist/dpop.js +105 -0
- package/dist/dpop.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +55 -0
- package/dist/errors.js.map +1 -0
- package/dist/files.d.ts +25 -0
- package/dist/files.js +65 -0
- package/dist/files.js.map +1 -0
- package/dist/http.d.ts +53 -0
- package/dist/http.js +130 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +209 -0
- package/dist/index.js.map +1 -0
- package/dist/live.d.ts +116 -0
- package/dist/live.js +448 -0
- package/dist/live.js.map +1 -0
- package/dist/queue.d.ts +103 -0
- package/dist/queue.js +397 -0
- package/dist/queue.js.map +1 -0
- package/package.json +45 -4
- package/src/auth.ts +371 -0
- package/src/brand.ts +8 -0
- package/src/cache.ts +379 -0
- package/src/collection.ts +533 -0
- package/src/dpop.ts +132 -0
- package/src/errors.ts +61 -0
- package/src/files.ts +60 -0
- package/src/http.ts +154 -0
- package/src/index.ts +238 -0
- package/src/live.ts +434 -0
- package/src/queue.ts +419 -0
package/src/live.ts
ADDED
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
// Keeping the cache current: catch-up from /v1/changes, and live changes
|
|
2
|
+
// over a WebSocket opened with a single-use ticket for the subscribed
|
|
3
|
+
// collections. Changes are applied by revision, so ones that arrive late or
|
|
4
|
+
// twice never move a record backwards.
|
|
5
|
+
import type { Cache, WireChange } from "./cache.js";
|
|
6
|
+
import { PbError } from "./errors.js";
|
|
7
|
+
import { asPbError, isTransient, type Net } from "./queue.js";
|
|
8
|
+
|
|
9
|
+
const BACKOFF_START_MS = 1000;
|
|
10
|
+
const BACKOFF_MAX_MS = 60_000;
|
|
11
|
+
|
|
12
|
+
/** Close reasons after which the SDK must not reconnect. */
|
|
13
|
+
const FINAL_REASONS = new Set(["app_suspended", "account_closed"]);
|
|
14
|
+
|
|
15
|
+
export interface LiveDeps {
|
|
16
|
+
net: Net;
|
|
17
|
+
cache: Cache;
|
|
18
|
+
/** The server's base URL (http/https); the WebSocket uses ws/wss. */
|
|
19
|
+
base: string;
|
|
20
|
+
/** The majors the app reads of a collection, when it declared them. */
|
|
21
|
+
versionsOf: (collection: string) => number[] | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Live holds at most one WebSocket for the collections that have
|
|
26
|
+
* subscribers. On every open it first catches up from /v1/changes, then
|
|
27
|
+
* applies live changes; it reconnects with backoff (1 s doubling to 60 s)
|
|
28
|
+
* after lagging, revoked or a dropped connection, and stops for good on
|
|
29
|
+
* app_suspended or account_closed (reported through Net.problem).
|
|
30
|
+
*/
|
|
31
|
+
export class Live {
|
|
32
|
+
private readonly watched = new Map<string, number>();
|
|
33
|
+
/** The collections the current (or opening) connection is for. */
|
|
34
|
+
private target: string[] = [];
|
|
35
|
+
private ws: WebSocket | null = null;
|
|
36
|
+
private caughtUp = false;
|
|
37
|
+
private gen = 0;
|
|
38
|
+
private attempt = 0;
|
|
39
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
40
|
+
private syncQueued = false;
|
|
41
|
+
private stopped = false;
|
|
42
|
+
private halted = false;
|
|
43
|
+
/** Waiting for something to change (a permanent ticket error). */
|
|
44
|
+
private parked = false;
|
|
45
|
+
/** Serialises catch-ups and change application. */
|
|
46
|
+
private tail: Promise<unknown> = Promise.resolve();
|
|
47
|
+
/**
|
|
48
|
+
* Collections the server has shown this app can read (a ticket or query
|
|
49
|
+
* for them succeeded). Only for these, or for a collection the changes
|
|
50
|
+
* feed returned a change of, is a catch-up position stored: for an
|
|
51
|
+
* unreadable collection the feed answers "nothing, up to the latest rev",
|
|
52
|
+
* and storing that would skip the history once access is granted.
|
|
53
|
+
*/
|
|
54
|
+
private readonly proven = new Set<string>();
|
|
55
|
+
/**
|
|
56
|
+
* Watched collections left out of the live connection because the server
|
|
57
|
+
* refused them (no consent, or unknown), so the others still get live
|
|
58
|
+
* updates. Cleared when access is granted or the device comes online.
|
|
59
|
+
*/
|
|
60
|
+
private readonly blocked = new Set<string>();
|
|
61
|
+
/** Collections whose background catch-up has started in this session. */
|
|
62
|
+
private readonly started = new Set<string>();
|
|
63
|
+
/** Background catch-up retries, by collection. */
|
|
64
|
+
private readonly retries = new Map<string, { attempt: number; timer: ReturnType<typeof setTimeout> | undefined }>();
|
|
65
|
+
|
|
66
|
+
constructor(private readonly d: LiveDeps) {}
|
|
67
|
+
|
|
68
|
+
/** watch keeps collection live until the returned function is called. */
|
|
69
|
+
watch(collection: string): () => void {
|
|
70
|
+
this.watched.set(collection, (this.watched.get(collection) ?? 0) + 1);
|
|
71
|
+
this.schedule();
|
|
72
|
+
let done = false;
|
|
73
|
+
return () => {
|
|
74
|
+
if (done) return;
|
|
75
|
+
done = true;
|
|
76
|
+
const n = (this.watched.get(collection) ?? 1) - 1;
|
|
77
|
+
if (n <= 0) this.watched.delete(collection);
|
|
78
|
+
else this.watched.set(collection, n);
|
|
79
|
+
this.schedule();
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Whether collection is covered by an open, caught-up connection. */
|
|
84
|
+
isLive(collection: string): boolean {
|
|
85
|
+
return this.caughtUp && this.ws !== null && this.target.includes(collection);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* ensure starts collection's background catch-up the first time it is
|
|
90
|
+
* used in this session (reads never wait for it). A failure is retried
|
|
91
|
+
* with backoff and, if it fails again, shown in status().problem.
|
|
92
|
+
*/
|
|
93
|
+
ensure(collection: string): void {
|
|
94
|
+
if (this.started.has(collection)) return;
|
|
95
|
+
this.started.add(collection);
|
|
96
|
+
this.background(collection);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private background(collection: string): void {
|
|
100
|
+
if (this.stopped || !this.d.net.deviceOnline || this.isLive(collection)) return;
|
|
101
|
+
const r = this.retries.get(collection) ?? { attempt: 0, timer: undefined };
|
|
102
|
+
this.retries.set(collection, r);
|
|
103
|
+
if (r.timer !== undefined) clearTimeout(r.timer);
|
|
104
|
+
r.timer = undefined;
|
|
105
|
+
this.serial(() => this.catchUp([collection])).then(
|
|
106
|
+
() => {
|
|
107
|
+
r.attempt = 0;
|
|
108
|
+
this.d.net.setProblem(`catch-up ${collection}`, undefined);
|
|
109
|
+
},
|
|
110
|
+
(e: unknown) => {
|
|
111
|
+
if (this.stopped) return;
|
|
112
|
+
const err = asPbError(e);
|
|
113
|
+
if (err.code === "consent_required") this.unreadable([collection]);
|
|
114
|
+
if (r.attempt > 0 && err.code !== "network") this.d.net.setProblem(`catch-up ${collection}`, err);
|
|
115
|
+
const delay = Math.min(BACKOFF_MAX_MS, BACKOFF_START_MS * 2 ** r.attempt);
|
|
116
|
+
r.attempt++;
|
|
117
|
+
r.timer = setTimeout(() => {
|
|
118
|
+
r.timer = undefined;
|
|
119
|
+
this.background(collection);
|
|
120
|
+
}, delay);
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** readable records that the server just served collection to this app. */
|
|
126
|
+
readable(collection: string): void {
|
|
127
|
+
this.proven.add(collection);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* unreadable forgets the catch-up positions of collections the server
|
|
132
|
+
* refused (consent_required, revoked): when access comes back, their
|
|
133
|
+
* history is fetched again from the start.
|
|
134
|
+
*/
|
|
135
|
+
unreadable(collections: string[]): void {
|
|
136
|
+
for (const c of collections) {
|
|
137
|
+
this.proven.delete(c);
|
|
138
|
+
void this.serial(async () => {
|
|
139
|
+
if (!this.stopped) await this.d.cache.setLastRev(c, 0);
|
|
140
|
+
}).catch(() => undefined);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* granted is called when the user granted read access to collection (or
|
|
146
|
+
* the majors read of it widened): its history is fetched from the start,
|
|
147
|
+
* and the live connection is renewed.
|
|
148
|
+
*/
|
|
149
|
+
granted(collection: string): void {
|
|
150
|
+
this.blocked.delete(collection);
|
|
151
|
+
this.unreadable([collection]);
|
|
152
|
+
this.started.add(collection);
|
|
153
|
+
const r = this.retries.get(collection);
|
|
154
|
+
if (r) r.attempt = 0;
|
|
155
|
+
this.background(collection);
|
|
156
|
+
this.refresh();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** idle resolves once no catch-up or change is being applied. */
|
|
160
|
+
async idle(): Promise<void> {
|
|
161
|
+
let t: Promise<unknown>;
|
|
162
|
+
do {
|
|
163
|
+
t = this.tail;
|
|
164
|
+
await t;
|
|
165
|
+
} while (t !== this.tail);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* fresh brings collection's cache up to date from /v1/changes now, unless
|
|
170
|
+
* the live connection already does (used before refusing a write to a
|
|
171
|
+
* record the cache does not know). Offline, or when the server cannot be
|
|
172
|
+
* reached, the cache is used as it is.
|
|
173
|
+
*/
|
|
174
|
+
async fresh(collection: string): Promise<void> {
|
|
175
|
+
if (this.isLive(collection) || !this.d.net.deviceOnline) return;
|
|
176
|
+
try {
|
|
177
|
+
await this.serial(() => this.catchUp([collection]));
|
|
178
|
+
} catch (e) {
|
|
179
|
+
if (asPbError(e).code !== "network") throw asPbError(e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* refresh reconnects now and re-runs the background catch-ups (the device
|
|
185
|
+
* came online, or access was granted).
|
|
186
|
+
*/
|
|
187
|
+
refresh(): void {
|
|
188
|
+
if (this.stopped || this.halted) return;
|
|
189
|
+
for (const c of this.started) this.background(c);
|
|
190
|
+
this.blocked.clear();
|
|
191
|
+
this.attempt = 0;
|
|
192
|
+
this.parked = false;
|
|
193
|
+
this.close();
|
|
194
|
+
this.schedule();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
stop(): void {
|
|
198
|
+
this.stopped = true;
|
|
199
|
+
for (const r of this.retries.values()) if (r.timer !== undefined) clearTimeout(r.timer);
|
|
200
|
+
this.close();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---- connection ----
|
|
204
|
+
|
|
205
|
+
private schedule(): void {
|
|
206
|
+
if (this.syncQueued) return;
|
|
207
|
+
this.syncQueued = true;
|
|
208
|
+
queueMicrotask(() => {
|
|
209
|
+
this.syncQueued = false;
|
|
210
|
+
this.sync();
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private sync(): void {
|
|
215
|
+
if (this.stopped || this.halted) return;
|
|
216
|
+
const want = [...this.watched.keys()].filter((c) => !this.blocked.has(c)).sort();
|
|
217
|
+
const same = want.length === this.target.length && want.every((c, i) => c === this.target[i]);
|
|
218
|
+
if (same && (this.ws !== null || this.timer !== undefined || this.opening || this.parked)) return;
|
|
219
|
+
this.close();
|
|
220
|
+
if (want.length > 0) void this.open(want);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private opening = false;
|
|
224
|
+
|
|
225
|
+
private close(): void {
|
|
226
|
+
this.gen++;
|
|
227
|
+
if (this.timer !== undefined) clearTimeout(this.timer);
|
|
228
|
+
this.timer = undefined;
|
|
229
|
+
this.opening = false;
|
|
230
|
+
this.caughtUp = false;
|
|
231
|
+
this.target = [];
|
|
232
|
+
const ws = this.ws;
|
|
233
|
+
this.ws = null;
|
|
234
|
+
if (ws) {
|
|
235
|
+
ws.onopen = ws.onmessage = ws.onclose = ws.onerror = null;
|
|
236
|
+
try {
|
|
237
|
+
ws.close();
|
|
238
|
+
} catch {
|
|
239
|
+
// already closed
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private retryLater(): void {
|
|
245
|
+
if (this.stopped || this.halted) return;
|
|
246
|
+
const delay = Math.min(BACKOFF_MAX_MS, BACKOFF_START_MS * 2 ** this.attempt);
|
|
247
|
+
this.attempt++;
|
|
248
|
+
const want = this.target;
|
|
249
|
+
this.timer = setTimeout(() => {
|
|
250
|
+
this.timer = undefined;
|
|
251
|
+
this.target = [];
|
|
252
|
+
if (want.length > 0) this.sync();
|
|
253
|
+
}, delay);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private halt(e: PbError): void {
|
|
257
|
+
this.halted = true;
|
|
258
|
+
this.close();
|
|
259
|
+
this.d.net.setProblem("live", e);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private async open(want: string[]): Promise<void> {
|
|
263
|
+
const gen = this.gen;
|
|
264
|
+
this.target = want;
|
|
265
|
+
this.parked = false;
|
|
266
|
+
if (!this.d.net.deviceOnline) {
|
|
267
|
+
this.parked = true; // the online event calls refresh()
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
this.opening = true;
|
|
271
|
+
let ticket: string;
|
|
272
|
+
try {
|
|
273
|
+
const res = await this.d.net.request<{ ticket?: unknown }>("POST", "/v1/subscribe/ticket", { collections: want });
|
|
274
|
+
if (typeof res?.ticket !== "string") throw new PbError("invalid_response", "The server returned no subscribe ticket.", "Retry later.");
|
|
275
|
+
ticket = res.ticket;
|
|
276
|
+
} catch (e) {
|
|
277
|
+
if (gen !== this.gen) return;
|
|
278
|
+
this.opening = false;
|
|
279
|
+
const err = asPbError(e);
|
|
280
|
+
if (FINAL_REASONS.has(err.code)) return this.halt(err);
|
|
281
|
+
if (isTransient(err)) return this.retryLater();
|
|
282
|
+
this.d.net.setProblem("live", err);
|
|
283
|
+
if (err.code === "consent_required" || err.code === "not_found") {
|
|
284
|
+
// Leave out only the collections the server refuses; the rest
|
|
285
|
+
// reconnect at once.
|
|
286
|
+
const bad = await this.refused(want, err);
|
|
287
|
+
if (gen !== this.gen) return;
|
|
288
|
+
if (bad.length > 0) {
|
|
289
|
+
for (const c of bad) this.blocked.add(c);
|
|
290
|
+
if (err.code === "consent_required") this.unreadable(bad);
|
|
291
|
+
this.target = [];
|
|
292
|
+
this.sync();
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// Nothing to single out: wait until the subscriptions change or
|
|
297
|
+
// access is granted (refresh).
|
|
298
|
+
this.parked = true;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (gen !== this.gen) return;
|
|
302
|
+
this.opening = false;
|
|
303
|
+
// The ticket is only issued when every collection is readable.
|
|
304
|
+
for (const c of want) this.proven.add(c);
|
|
305
|
+
const url = `${this.d.base.replace(/^http/i, "ws")}/v1/subscribe?ticket=${encodeURIComponent(ticket)}`;
|
|
306
|
+
let ws: WebSocket;
|
|
307
|
+
try {
|
|
308
|
+
ws = new WebSocket(url);
|
|
309
|
+
} catch {
|
|
310
|
+
return this.retryLater();
|
|
311
|
+
}
|
|
312
|
+
this.ws = ws;
|
|
313
|
+
let reason = "";
|
|
314
|
+
ws.onopen = () => {
|
|
315
|
+
if (gen !== this.gen) return;
|
|
316
|
+
this.serial(() => this.catchUp(want)).then(
|
|
317
|
+
() => {
|
|
318
|
+
if (gen !== this.gen) return;
|
|
319
|
+
this.caughtUp = true;
|
|
320
|
+
this.attempt = 0;
|
|
321
|
+
if (this.blocked.size === 0) this.d.net.setProblem("live", undefined);
|
|
322
|
+
},
|
|
323
|
+
() => {
|
|
324
|
+
if (gen !== this.gen) return;
|
|
325
|
+
this.close();
|
|
326
|
+
this.target = want;
|
|
327
|
+
this.retryLater();
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
};
|
|
331
|
+
ws.onmessage = (ev: MessageEvent) => {
|
|
332
|
+
if (gen !== this.gen) return;
|
|
333
|
+
let msg: { type?: unknown; change?: WireChange; reason?: unknown };
|
|
334
|
+
try {
|
|
335
|
+
msg = JSON.parse(String(ev.data));
|
|
336
|
+
} catch {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (msg.type === "change" && msg.change) {
|
|
340
|
+
const ch = msg.change;
|
|
341
|
+
void this.serial(() => this.apply([ch])).catch(() => undefined);
|
|
342
|
+
} else if (msg.type === "closed" && typeof msg.reason === "string") {
|
|
343
|
+
reason = msg.reason;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
ws.onclose = () => {
|
|
347
|
+
if (gen !== this.gen) return;
|
|
348
|
+
this.ws = null;
|
|
349
|
+
this.caughtUp = false;
|
|
350
|
+
// Access may have been revoked: positions are no longer trusted until
|
|
351
|
+
// a read proves the collection readable again; the reconnect's ticket
|
|
352
|
+
// finds which collection (if any) is now refused.
|
|
353
|
+
if (reason === "revoked") for (const c of want) this.proven.delete(c);
|
|
354
|
+
if (FINAL_REASONS.has(reason)) {
|
|
355
|
+
return this.halt(new PbError(reason, `The live connection was closed: ${reason}.`, reason === "app_suspended" ? "This app has been suspended; contact its developer." : "The account is closed or being deleted."));
|
|
356
|
+
}
|
|
357
|
+
this.gen++; // ignore anything late from this socket
|
|
358
|
+
this.target = want;
|
|
359
|
+
this.retryLater();
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* refused returns which of collections the server refused a ticket for:
|
|
365
|
+
* the one the error message names (the server quotes it), else each
|
|
366
|
+
* other app's collection (declared with versions) that a one-record read
|
|
367
|
+
* refuses. The app's own collections are never singled out.
|
|
368
|
+
*/
|
|
369
|
+
private async refused(collections: string[], err: PbError): Promise<string[]> {
|
|
370
|
+
const named = collections.filter((c) => err.message.includes(`"${c}"`));
|
|
371
|
+
if (named.length > 0) return named;
|
|
372
|
+
const out: string[] = [];
|
|
373
|
+
for (const c of collections) {
|
|
374
|
+
const versions = this.d.versionsOf(c);
|
|
375
|
+
if (!versions) continue;
|
|
376
|
+
try {
|
|
377
|
+
await this.d.net.request("GET", `/v1/records/${encodeURIComponent(c)}?limit=1&versions=${versions.join(",")}`);
|
|
378
|
+
} catch (e) {
|
|
379
|
+
const code = asPbError(e).code;
|
|
380
|
+
if (code === "consent_required" || code === "not_found") out.push(c);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// ---- applying changes ----
|
|
387
|
+
|
|
388
|
+
private serial<R>(fn: () => Promise<R>): Promise<R> {
|
|
389
|
+
const run = this.tail.then(fn, fn);
|
|
390
|
+
this.tail = run.catch(() => undefined);
|
|
391
|
+
return run;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* catchUp applies every change since the collections' last caught-up
|
|
396
|
+
* revision, calling /v1/changes until the returned since stops moving,
|
|
397
|
+
* then stores that revision for each collection.
|
|
398
|
+
*/
|
|
399
|
+
private async catchUp(collections: string[]): Promise<void> {
|
|
400
|
+
const revs = await Promise.all(collections.map((c) => this.d.cache.lastRev(c)));
|
|
401
|
+
let since = Math.min(...revs);
|
|
402
|
+
const names = collections.map(encodeURIComponent).join(",");
|
|
403
|
+
const seen = new Set<string>();
|
|
404
|
+
for (;;) {
|
|
405
|
+
if (this.stopped) return;
|
|
406
|
+
const res = await this.d.net.request<{ changes?: WireChange[]; since?: unknown }>("GET", `/v1/changes?since=${since}&collections=${names}`);
|
|
407
|
+
const changes = res?.changes ?? [];
|
|
408
|
+
for (const ch of changes) seen.add(ch.collection);
|
|
409
|
+
await this.apply(changes);
|
|
410
|
+
const next = res?.since;
|
|
411
|
+
if (typeof next !== "number" || next <= since) break;
|
|
412
|
+
since = next;
|
|
413
|
+
}
|
|
414
|
+
if (this.stopped) return;
|
|
415
|
+
for (const [i, c] of collections.entries()) {
|
|
416
|
+
// Only where the server showed the collection is readable (see proven).
|
|
417
|
+
if (this.stopped) return;
|
|
418
|
+
if (since > revs[i]! && (this.proven.has(c) || seen.has(c))) await this.d.cache.setLastRev(c, since);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private async apply(changes: WireChange[]): Promise<void> {
|
|
423
|
+
if (changes.length === 0 || this.stopped) return;
|
|
424
|
+
await this.d.cache.write(async (w) => {
|
|
425
|
+
for (const ch of changes) {
|
|
426
|
+
const rec = ch.op === "delete" ? null : ch.record;
|
|
427
|
+
// A changes-feed record is the record's current state, which may be
|
|
428
|
+
// newer than the change itself.
|
|
429
|
+
const rev = Math.max(ch.rev, rec?.rev ?? 0);
|
|
430
|
+
await w.applyServer(ch.collection, ch.rkey, rev, rec, this.d.versionsOf(ch.collection));
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|