@wervt/client 0.0.2 → 0.0.3
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/dist/shape.js +60 -17
- package/package.json +2 -2
package/dist/shape.js
CHANGED
|
@@ -3,30 +3,52 @@ import { shapeParser } from "@wervt/core";
|
|
|
3
3
|
// src/shape.ts
|
|
4
4
|
/** The rows of the shape at `url` (absolute) as of now, without subscribing. */
|
|
5
5
|
async function loadSnapshot(url, fetchClient) {
|
|
6
|
-
const rows = /* @__PURE__ */ new Map();
|
|
7
|
-
let fail = () => {};
|
|
8
|
-
const stream = new ShapeStream({
|
|
9
|
-
url,
|
|
10
|
-
subscribe: false,
|
|
11
|
-
fetchClient,
|
|
12
|
-
parser: shapeParser,
|
|
13
|
-
onError: (e) => fail(e)
|
|
14
|
-
});
|
|
15
6
|
try {
|
|
16
|
-
await
|
|
17
|
-
fail = reject;
|
|
18
|
-
stream.subscribe((messages) => {
|
|
19
|
-
applyMessages(rows, messages);
|
|
20
|
-
if (stream.isUpToDate) resolve();
|
|
21
|
-
}, (e) => reject(e));
|
|
22
|
-
});
|
|
7
|
+
return await readCurrentRows(url, fetchClient) ?? await replayLog(url, fetchClient);
|
|
23
8
|
} catch (e) {
|
|
24
|
-
if (e
|
|
9
|
+
if (isUnauthorized(e)) return {
|
|
25
10
|
rows: [],
|
|
26
11
|
offset: "-1",
|
|
27
12
|
unauthorized: true
|
|
28
13
|
};
|
|
29
14
|
throw e;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const isUnauthorized = (e) => e?.status === 401;
|
|
18
|
+
/**
|
|
19
|
+
* Asks Postgres for the current rows, so a page render costs the same however many changes the
|
|
20
|
+
* shape has seen. Replaying the log grew without bound: a table of 35 rows updated every 30 s took
|
|
21
|
+
* 105 MB and 5 s after ten days.
|
|
22
|
+
*
|
|
23
|
+
* Undefined when that isn't possible (a runtime without the snapshot route, or the shape replaced
|
|
24
|
+
* meanwhile); the log still has the answer.
|
|
25
|
+
*/
|
|
26
|
+
async function readCurrentRows(url, fetchClient) {
|
|
27
|
+
const stream = openStream(url, fetchClient, "now");
|
|
28
|
+
try {
|
|
29
|
+
await untilUpToDate(stream);
|
|
30
|
+
const handle = stream.shapeHandle;
|
|
31
|
+
const { data, responseHandle } = await stream.fetchSnapshot({ where: "true" });
|
|
32
|
+
if (responseHandle !== handle) return void 0;
|
|
33
|
+
const rows = /* @__PURE__ */ new Map();
|
|
34
|
+
applyMessages(rows, data);
|
|
35
|
+
return {
|
|
36
|
+
rows: [...rows],
|
|
37
|
+
offset: stream.lastOffset,
|
|
38
|
+
handle
|
|
39
|
+
};
|
|
40
|
+
} catch (e) {
|
|
41
|
+
if (isUnauthorized(e)) throw e;
|
|
42
|
+
return;
|
|
43
|
+
} finally {
|
|
44
|
+
stream.unsubscribeAll();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function replayLog(url, fetchClient) {
|
|
48
|
+
const rows = /* @__PURE__ */ new Map();
|
|
49
|
+
const stream = openStream(url, fetchClient);
|
|
50
|
+
try {
|
|
51
|
+
await untilUpToDate(stream, (messages) => applyMessages(rows, messages));
|
|
30
52
|
} finally {
|
|
31
53
|
stream.unsubscribeAll();
|
|
32
54
|
}
|
|
@@ -36,6 +58,27 @@ async function loadSnapshot(url, fetchClient) {
|
|
|
36
58
|
handle: stream.shapeHandle
|
|
37
59
|
};
|
|
38
60
|
}
|
|
61
|
+
function openStream(url, fetchClient, offset) {
|
|
62
|
+
let fail = () => {};
|
|
63
|
+
const stream = new ShapeStream({
|
|
64
|
+
url,
|
|
65
|
+
offset,
|
|
66
|
+
subscribe: false,
|
|
67
|
+
fetchClient,
|
|
68
|
+
parser: shapeParser,
|
|
69
|
+
onError: (e) => fail(e)
|
|
70
|
+
});
|
|
71
|
+
return Object.assign(stream, { onFailure: (handler) => fail = handler });
|
|
72
|
+
}
|
|
73
|
+
function untilUpToDate(stream, onMessages) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
stream.onFailure(reject);
|
|
76
|
+
stream.subscribe((messages) => {
|
|
77
|
+
onMessages?.(messages);
|
|
78
|
+
if (stream.isUpToDate) resolve();
|
|
79
|
+
}, (e) => reject(e));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
39
82
|
/** Applies Electric change messages to `rows`. Returns whether anything changed. */
|
|
40
83
|
function applyMessages(rows, messages) {
|
|
41
84
|
let changed = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wervt/client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Browser SDK for apps on wervt, for any framework: the signed-in user, live data from shapes, analytics events, the feedback widget and WebMCP tools.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"electric",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@wervt/core": "^0.0.
|
|
34
|
+
"@wervt/core": "^0.0.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@electric-sql/client": "^1.5.28"
|