@spooky-sync/core 1.0.0-beta.10 → 1.0.0-beta.11
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/AGENTS.md +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +328 -72
- package/dist/rows-worker.js +95 -3
- package/dist/types.d.ts +2 -0
- package/package.json +3 -3
- package/src/client/sp00ky-client.ts +5 -4
- package/src/services/database/rows-cache-engine.test.ts +573 -3
- package/src/services/database/rows-cache-engine.ts +349 -67
- package/src/services/database/rows-prewarm.ts +29 -3
- package/src/services/database/rows-worker.ts +22 -3
- package/src/services/database/rows-yield.test.ts +141 -0
- package/src/services/database/rows-yield.ts +92 -0
- package/src/types.ts +2 -0
package/dist/index.js
CHANGED
|
@@ -4546,8 +4546,8 @@ function trimHistory(history, cap) {
|
|
|
4546
4546
|
|
|
4547
4547
|
//#endregion
|
|
4548
4548
|
//#region src/modules/devtools/index.ts
|
|
4549
|
-
const CORE_VERSION = "1.0.0-beta.
|
|
4550
|
-
const WASM_VERSION = "1.0.0-beta.
|
|
4549
|
+
const CORE_VERSION = "1.0.0-beta.11";
|
|
4550
|
+
const WASM_VERSION = "1.0.0-beta.11";
|
|
4551
4551
|
const SURREAL_VERSION = "3.1.0";
|
|
4552
4552
|
var DevToolsService = class DevToolsService {
|
|
4553
4553
|
version = CORE_VERSION;
|
|
@@ -13835,6 +13835,12 @@ const CHECKPOINT_TAIL_BYTES = 8 << 20;
|
|
|
13835
13835
|
*
|
|
13836
13836
|
* The engine (`rows-cache-engine.ts`) adopts all three when it connects, and
|
|
13837
13837
|
* starts its own worker when anything about them is wrong.
|
|
13838
|
+
*
|
|
13839
|
+
* With shared tabs the page does not know yet which tab will lead. If it is
|
|
13840
|
+
* this one, its engine adopts the worker as it would alone. If it is another,
|
|
13841
|
+
* that tab's worker finds the files held, asks for them (`rows-worker.js`,
|
|
13842
|
+
* `ROWS_YIELD_CHANNEL`), and the worker started here closes its store and
|
|
13843
|
+
* says so under the open's id, so nothing here goes on believing it is open.
|
|
13838
13844
|
*/
|
|
13839
13845
|
/** localStorage key under which the engine leaves what {@link prewarmRows} needs. */
|
|
13840
13846
|
const ROWS_PREWARM_KEY = "sp00ky.rows.prewarm";
|
|
@@ -13881,9 +13887,13 @@ function prewarmRows() {
|
|
|
13881
13887
|
pre.worker.onmessage = (ev) => {
|
|
13882
13888
|
const data = ev.data;
|
|
13883
13889
|
if (!data) return;
|
|
13884
|
-
if (data.id === ROWS_PREWARM_OPEN_ID)
|
|
13885
|
-
|
|
13886
|
-
|
|
13890
|
+
if (data.id === ROWS_PREWARM_OPEN_ID) if (data.ok === true) pre.opened = data;
|
|
13891
|
+
else {
|
|
13892
|
+
pre.opened = void 0;
|
|
13893
|
+
pre.reads = [];
|
|
13894
|
+
pre.replies = {};
|
|
13895
|
+
}
|
|
13896
|
+
else if (data.id <= ROWS_AHEAD_ID) pre.replies[ROWS_AHEAD_ID - data.id] = data;
|
|
13887
13897
|
};
|
|
13888
13898
|
pre.worker.postMessage({
|
|
13889
13899
|
id: ROWS_PREWARM_OPEN_ID,
|
|
@@ -13910,7 +13920,7 @@ function prewarmRows() {
|
|
|
13910
13920
|
} catch {}
|
|
13911
13921
|
}
|
|
13912
13922
|
/** {@link prewarmRows} as a script to inline in `<head>`, ahead of the app's bundle. */
|
|
13913
|
-
const ROWS_PREWARM_SNIPPET = `(function(){try{var g=globalThis,p=JSON.parse(localStorage.getItem("${ROWS_PREWARM_KEY}")||"null");if(!p||!p.url||!p.bucket||g.__sp00kyRowsPrewarm)return;var r=Array.isArray(p.reads)?p.reads:[],s={worker:new Worker(p.url,{type:"module"}),bucket:p.bucket,failed:false,reads:r,replies:{}};s.worker.onerror=function(){s.failed=true};s.worker.onmessage=function(e){var d=e.data;if(!d)return;if(d.id===${ROWS_PREWARM_OPEN_ID}){if(d.ok===true)s.opened=d}else if(d.id<=${ROWS_AHEAD_ID})s.replies[${ROWS_AHEAD_ID}-d.id]=d};s.worker.postMessage({id:${ROWS_PREWARM_OPEN_ID},type:"open",payload:{dbName:p.bucket,useOpfs:true,openOptions:{maxAttempts:1,disallowMemoryFallback:true}}});for(var i=0;i<r.length;i++)s.worker.postMessage({id:${ROWS_AHEAD_ID}-i,type:r[i][0],payload:r[i][1]});s.worker.postMessage({id:-2,type:"warm"});g.__sp00kyRowsPrewarm=s}catch(e){}})();`;
|
|
13923
|
+
const ROWS_PREWARM_SNIPPET = `(function(){try{var g=globalThis,p=JSON.parse(localStorage.getItem("${ROWS_PREWARM_KEY}")||"null");if(!p||!p.url||!p.bucket||g.__sp00kyRowsPrewarm)return;var r=Array.isArray(p.reads)?p.reads:[],s={worker:new Worker(p.url,{type:"module"}),bucket:p.bucket,failed:false,reads:r,replies:{}};s.worker.onerror=function(){s.failed=true};s.worker.onmessage=function(e){var d=e.data;if(!d)return;if(d.id===${ROWS_PREWARM_OPEN_ID}){if(d.ok===true)s.opened=d;else{s.opened=void 0;s.reads=[];s.replies={}}}else if(d.id<=${ROWS_AHEAD_ID})s.replies[${ROWS_AHEAD_ID}-d.id]=d};s.worker.postMessage({id:${ROWS_PREWARM_OPEN_ID},type:"open",payload:{dbName:p.bucket,useOpfs:true,openOptions:{maxAttempts:1,disallowMemoryFallback:true}}});for(var i=0;i<r.length;i++)s.worker.postMessage({id:${ROWS_AHEAD_ID}-i,type:r[i][0],payload:r[i][1]});s.worker.postMessage({id:-2,type:"warm"});g.__sp00kyRowsPrewarm=s}catch(e){}})();`;
|
|
13914
13924
|
/** The prewarmed worker, once: whoever takes it owns it. */
|
|
13915
13925
|
function takePrewarmed() {
|
|
13916
13926
|
const g = globalThis;
|
|
@@ -13942,8 +13952,9 @@ function jsonSafe(value) {
|
|
|
13942
13952
|
* Why the answer is the one the worker would give now: it read the store
|
|
13943
13953
|
* after opening it and before this page asked for anything, and nothing has
|
|
13944
13954
|
* written since. The engine drops everything left here the moment it sends a
|
|
13945
|
-
* write (or anything else that is not a plain read), and
|
|
13946
|
-
*
|
|
13955
|
+
* write (or anything else that is not a plain read), and the moment another
|
|
13956
|
+
* tab can write through the same worker (a follower's port handed to a
|
|
13957
|
+
* shared-tabs leader).
|
|
13947
13958
|
*/
|
|
13948
13959
|
var ReadAhead = class {
|
|
13949
13960
|
wanted = /* @__PURE__ */ new Map();
|
|
@@ -13964,6 +13975,7 @@ var ReadAhead = class {
|
|
|
13964
13975
|
};
|
|
13965
13976
|
}
|
|
13966
13977
|
arrived(data) {
|
|
13978
|
+
if (data.id === ROWS_PREWARM_OPEN_ID && !data.ok) return this.drop();
|
|
13967
13979
|
if (data.id > ROWS_AHEAD_ID) return;
|
|
13968
13980
|
const i = ROWS_AHEAD_ID - data.id;
|
|
13969
13981
|
const waiter = this.waiting.get(i);
|
|
@@ -14052,6 +14064,24 @@ const ROWS_READ_OPS = new Set([
|
|
|
14052
14064
|
const PAINT_MAX_KEYS = 64;
|
|
14053
14065
|
const PAINT_MAX_ENTRY_CHARS = 256e3;
|
|
14054
14066
|
const PAINT_FLUSH_MS = 1500;
|
|
14067
|
+
/** What a kept read starts with: this layout. A copy that starts otherwise is another release's, and is not read. */
|
|
14068
|
+
const PAINT_FORMAT = "2";
|
|
14069
|
+
/** A kept read's header: whether it holds bytes to revive, the marks it was read under, and where its rows start. */
|
|
14070
|
+
function paintHead(text) {
|
|
14071
|
+
if (text[0] !== PAINT_FORMAT) return null;
|
|
14072
|
+
const end = text.indexOf("\n");
|
|
14073
|
+
if (end < 0) return null;
|
|
14074
|
+
try {
|
|
14075
|
+
const [u8, marks] = JSON.parse(text.slice(1, end));
|
|
14076
|
+
return Array.isArray(marks) ? {
|
|
14077
|
+
u8: u8 === 1,
|
|
14078
|
+
marks,
|
|
14079
|
+
body: end + 1
|
|
14080
|
+
} : null;
|
|
14081
|
+
} catch {
|
|
14082
|
+
return null;
|
|
14083
|
+
}
|
|
14084
|
+
}
|
|
14055
14085
|
function hashKey(text) {
|
|
14056
14086
|
let a = 2166136261;
|
|
14057
14087
|
let b = 16777619;
|
|
@@ -14070,25 +14100,46 @@ function hashKey(text) {
|
|
|
14070
14100
|
* while the store opens.
|
|
14071
14101
|
*
|
|
14072
14102
|
* What it holds is a copy, and the contract says so: a peek is something to
|
|
14073
|
-
* paint from, the read that follows is the answer.
|
|
14074
|
-
*
|
|
14075
|
-
*
|
|
14076
|
-
* (
|
|
14077
|
-
*
|
|
14103
|
+
* paint from, the read that follows is the answer.
|
|
14104
|
+
*
|
|
14105
|
+
* Several tabs keep copies in the same place and write to the same store
|
|
14106
|
+
* (shared tabs), so a copy says what it was read under. Every table has a
|
|
14107
|
+
* mark in localStorage, and a tab that writes to a table leaves a new one
|
|
14108
|
+
* there, once before the write is sent and once after the store has taken it.
|
|
14109
|
+
* A copy carries the marks its tables had when it was read and is handed out
|
|
14110
|
+
* only while they still stand, whichever tab wrote. The same marks tell a tab
|
|
14111
|
+
* that a read of its own was overtaken by another tab's write (see
|
|
14112
|
+
* {@link stamp}). What is left is a tab that dies between a write and its
|
|
14113
|
+
* second mark, a write given up on (its deadline passed) that the store takes
|
|
14114
|
+
* after all, and a browser that dies before a mark reaches the disk: the read
|
|
14115
|
+
* that follows the peek then corrects the screen a moment later.
|
|
14116
|
+
*
|
|
14117
|
+
* A tab that is alone in the store (no shared tabs) leaves no marks: nobody
|
|
14118
|
+
* else writes to it, and dropping its own copies as it writes is enough.
|
|
14078
14119
|
*/
|
|
14079
14120
|
var PaintCache = class {
|
|
14080
14121
|
entries = /* @__PURE__ */ new Map();
|
|
14081
14122
|
/** Reads this session made at startup: the ones worth keeping a copy of, whenever they run again. */
|
|
14082
14123
|
known = /* @__PURE__ */ new Set();
|
|
14083
|
-
/** table -> keys, for the entries of earlier sessions too. */
|
|
14124
|
+
/** table -> keys, for the entries of earlier sessions and of other tabs too. */
|
|
14084
14125
|
byTable = null;
|
|
14085
|
-
/** table -> writes this tab
|
|
14126
|
+
/** table -> writes this tab knows of: its own, and another tab's each time the table's mark was found moved. */
|
|
14086
14127
|
writes = /* @__PURE__ */ new Map();
|
|
14128
|
+
/** table -> its mark as this tab last read it. */
|
|
14129
|
+
seen = /* @__PURE__ */ new Map();
|
|
14130
|
+
/** table -> the mark this tab last left there. */
|
|
14131
|
+
mine = /* @__PURE__ */ new Map();
|
|
14132
|
+
tab = Math.random().toString(36).slice(2, 10);
|
|
14133
|
+
marked = 0;
|
|
14134
|
+
/** Moves with the bucket, so a read sent under one is never kept under the next. */
|
|
14135
|
+
era = 0;
|
|
14087
14136
|
timer = null;
|
|
14088
14137
|
bucket = "";
|
|
14089
14138
|
since = 0;
|
|
14090
|
-
|
|
14139
|
+
/** `shared`: other tabs write to the same store, so writes are marked for them. */
|
|
14140
|
+
constructor(maxChars, shared = true) {
|
|
14091
14141
|
this.maxChars = maxChars;
|
|
14142
|
+
this.shared = shared;
|
|
14092
14143
|
if (typeof addEventListener === "function") addEventListener("pagehide", () => this.flush());
|
|
14093
14144
|
}
|
|
14094
14145
|
/** The bucket about to be opened: peeks answer from it until `start` says which one it is. */
|
|
@@ -14099,49 +14150,153 @@ var PaintCache = class {
|
|
|
14099
14150
|
start(bucket) {
|
|
14100
14151
|
if (bucket !== this.bucket) {
|
|
14101
14152
|
this.flush();
|
|
14102
|
-
this.
|
|
14103
|
-
this.known.clear();
|
|
14104
|
-
this.byTable = null;
|
|
14153
|
+
this.forget();
|
|
14105
14154
|
}
|
|
14106
14155
|
this.bucket = bucket;
|
|
14107
14156
|
this.since = Date.now();
|
|
14108
14157
|
}
|
|
14158
|
+
/**
|
|
14159
|
+
* {@link start} for a tab that is handed its store more than once (shared
|
|
14160
|
+
* tabs: a new role over the same bucket). Only the first hand-over of a
|
|
14161
|
+
* bucket starts a session; what the tab reads after a later one is not what
|
|
14162
|
+
* it reads at startup.
|
|
14163
|
+
*/
|
|
14164
|
+
adopt(bucket) {
|
|
14165
|
+
if (bucket !== this.bucket || this.since === 0) this.start(bucket);
|
|
14166
|
+
}
|
|
14167
|
+
/** The store turned out to live in memory: nothing read from it is worth showing next time. */
|
|
14168
|
+
stop() {
|
|
14169
|
+
if (this.timer) clearTimeout(this.timer);
|
|
14170
|
+
this.timer = null;
|
|
14171
|
+
this.forget();
|
|
14172
|
+
this.bucket = "";
|
|
14173
|
+
this.since = 0;
|
|
14174
|
+
}
|
|
14175
|
+
forget() {
|
|
14176
|
+
this.entries.clear();
|
|
14177
|
+
this.known.clear();
|
|
14178
|
+
this.byTable = null;
|
|
14179
|
+
this.seen.clear();
|
|
14180
|
+
this.mine.clear();
|
|
14181
|
+
this.era++;
|
|
14182
|
+
}
|
|
14109
14183
|
name(key) {
|
|
14110
14184
|
return `sp00ky.paint.${this.bucket}.${key}`;
|
|
14111
14185
|
}
|
|
14186
|
+
/** Where `table`'s mark is kept. A key is a hash and has no dot, so the two never meet. */
|
|
14187
|
+
markName(table) {
|
|
14188
|
+
return `sp00ky.paint.${this.bucket}.t.${table}`;
|
|
14189
|
+
}
|
|
14112
14190
|
index() {
|
|
14113
14191
|
if (this.byTable) return this.byTable;
|
|
14114
14192
|
this.byTable = /* @__PURE__ */ new Map();
|
|
14193
|
+
this.readIndex();
|
|
14194
|
+
return this.byTable;
|
|
14195
|
+
}
|
|
14196
|
+
/** Take in the keys kept by earlier sessions, and by other tabs since this one last looked. */
|
|
14197
|
+
readIndex() {
|
|
14115
14198
|
try {
|
|
14116
14199
|
const saved = JSON.parse(localStorage.getItem(`sp00ky.paint.${this.bucket}`) ?? "{}");
|
|
14117
|
-
for (const [key, tables] of Object.entries(saved)) this.link(key, tables);
|
|
14200
|
+
for (const [key, tables] of Object.entries(saved)) if (Array.isArray(tables)) this.link(key, tables);
|
|
14118
14201
|
} catch {}
|
|
14119
|
-
return this.byTable;
|
|
14120
14202
|
}
|
|
14121
14203
|
link(key, tables) {
|
|
14122
|
-
const index = this.
|
|
14204
|
+
const index = this.index();
|
|
14123
14205
|
for (const table of tables) {
|
|
14124
14206
|
let keys = index.get(table);
|
|
14125
14207
|
if (!keys) index.set(table, keys = /* @__PURE__ */ new Set());
|
|
14126
14208
|
keys.add(key);
|
|
14127
14209
|
}
|
|
14128
14210
|
}
|
|
14211
|
+
/**
|
|
14212
|
+
* Read `table`'s mark. One that moved, and not to what this tab left there,
|
|
14213
|
+
* is another tab's write.
|
|
14214
|
+
*/
|
|
14215
|
+
look(table) {
|
|
14216
|
+
let now = null;
|
|
14217
|
+
try {
|
|
14218
|
+
now = localStorage.getItem(this.markName(table));
|
|
14219
|
+
} catch {}
|
|
14220
|
+
if (now === (this.seen.get(table) ?? null)) return;
|
|
14221
|
+
this.seen.set(table, now);
|
|
14222
|
+
if (now !== this.mine.get(table)) this.writes.set(table, (this.writes.get(table) ?? 0) + 1);
|
|
14223
|
+
}
|
|
14224
|
+
/** Leave a new mark on `table`: every copy read under the old one is void, in every tab. */
|
|
14225
|
+
leave(table) {
|
|
14226
|
+
if (!this.shared) return;
|
|
14227
|
+
this.look(table);
|
|
14228
|
+
let mark = `${this.tab}.${++this.marked}`;
|
|
14229
|
+
try {
|
|
14230
|
+
try {
|
|
14231
|
+
localStorage.setItem(this.markName(table), mark);
|
|
14232
|
+
} catch {
|
|
14233
|
+
this.empty();
|
|
14234
|
+
localStorage.setItem(this.markName(table), mark);
|
|
14235
|
+
}
|
|
14236
|
+
} catch {
|
|
14237
|
+
mark = null;
|
|
14238
|
+
try {
|
|
14239
|
+
localStorage.removeItem(this.markName(table));
|
|
14240
|
+
} catch {}
|
|
14241
|
+
}
|
|
14242
|
+
if (mark === null) this.mine.delete(table);
|
|
14243
|
+
else this.mine.set(table, mark);
|
|
14244
|
+
this.seen.set(table, mark);
|
|
14245
|
+
}
|
|
14246
|
+
/** Every copy kept for this bucket, by any tab, whether the index knows it or not. Marks stay. */
|
|
14247
|
+
copies() {
|
|
14248
|
+
const prefix = `sp00ky.paint.${this.bucket}.`;
|
|
14249
|
+
const out = [];
|
|
14250
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
14251
|
+
const name = localStorage.key(i);
|
|
14252
|
+
if (name !== null && name.startsWith(prefix) && !name.startsWith(prefix + "t.")) out.push(name.slice(prefix.length));
|
|
14253
|
+
}
|
|
14254
|
+
return out;
|
|
14255
|
+
}
|
|
14256
|
+
/** Let go of every copy of this bucket, this tab's and the others'. */
|
|
14257
|
+
empty() {
|
|
14258
|
+
this.entries.clear();
|
|
14259
|
+
this.byTable = /* @__PURE__ */ new Map();
|
|
14260
|
+
try {
|
|
14261
|
+
for (const key of this.copies()) localStorage.removeItem(this.name(key));
|
|
14262
|
+
localStorage.removeItem(`sp00ky.paint.${this.bucket}`);
|
|
14263
|
+
} catch {}
|
|
14264
|
+
}
|
|
14265
|
+
/** Whether no tab has written to these tables since they were read. Throws without storage. */
|
|
14266
|
+
holds(marks) {
|
|
14267
|
+
for (const [table, mark] of marks) if (localStorage.getItem(this.markName(table)) !== mark) return false;
|
|
14268
|
+
return true;
|
|
14269
|
+
}
|
|
14129
14270
|
get(key, revive) {
|
|
14130
14271
|
if (!this.bucket) return null;
|
|
14131
14272
|
try {
|
|
14132
|
-
const
|
|
14133
|
-
|
|
14134
|
-
|
|
14135
|
-
|
|
14273
|
+
const own = this.entries.get(key)?.text;
|
|
14274
|
+
for (const text of own ? [own, null] : [null]) {
|
|
14275
|
+
const kept = text ?? localStorage.getItem(this.name(key));
|
|
14276
|
+
const head = kept ? paintHead(kept) : null;
|
|
14277
|
+
if (!kept || !head || !this.holds(head.marks)) continue;
|
|
14278
|
+
const rows = kept.slice(head.body);
|
|
14279
|
+
if (!head.u8) return JSON.parse(rows);
|
|
14280
|
+
return JSON.parse(rows, (_k, v) => v && typeof v === "object" && typeof v.__u8 === "string" ? revive(v.__u8) : v);
|
|
14281
|
+
}
|
|
14282
|
+
return null;
|
|
14136
14283
|
} catch {
|
|
14137
14284
|
return null;
|
|
14138
14285
|
}
|
|
14139
14286
|
}
|
|
14140
|
-
/**
|
|
14287
|
+
/**
|
|
14288
|
+
* What is known of the writes to `tables`: taken when a read is sent,
|
|
14289
|
+
* checked when it answers. It moves with every write this tab sends and
|
|
14290
|
+
* with every write of another tab that this one has seen the mark of.
|
|
14291
|
+
*/
|
|
14141
14292
|
stamp(tables) {
|
|
14293
|
+
if (!this.bucket) return "";
|
|
14142
14294
|
let n = 0;
|
|
14143
|
-
for (const table of tables)
|
|
14144
|
-
|
|
14295
|
+
for (const table of tables) {
|
|
14296
|
+
this.look(table);
|
|
14297
|
+
n += this.writes.get(table) ?? 0;
|
|
14298
|
+
}
|
|
14299
|
+
return `${this.era}.${n}`;
|
|
14145
14300
|
}
|
|
14146
14301
|
/**
|
|
14147
14302
|
* A read just answered: keep its text if it is a startup read, or one kept
|
|
@@ -14155,18 +14310,30 @@ var PaintCache = class {
|
|
|
14155
14310
|
this.known.add(key);
|
|
14156
14311
|
}
|
|
14157
14312
|
if (rows.length > PAINT_MAX_ENTRY_CHARS) return;
|
|
14313
|
+
const head = JSON.stringify([u8 ? 1 : 0, tables.map((table) => [table, this.seen.get(table) ?? null])]);
|
|
14158
14314
|
this.entries.set(key, {
|
|
14159
14315
|
tables,
|
|
14160
|
-
text:
|
|
14316
|
+
text: `${PAINT_FORMAT}${head}\n${rows}`,
|
|
14161
14317
|
dirty: true
|
|
14162
14318
|
});
|
|
14319
|
+
this.link(key, tables);
|
|
14163
14320
|
this.timer ??= setTimeout(() => this.flush(), PAINT_FLUSH_MS);
|
|
14164
14321
|
}
|
|
14165
|
-
/**
|
|
14166
|
-
|
|
14322
|
+
/**
|
|
14323
|
+
* This tab writes to `tables`: what was read from them is no longer what
|
|
14324
|
+
* they hold. Called as the write is sent, and again with `done` once the
|
|
14325
|
+
* store has answered it. The first call drops this tab's copies and fails
|
|
14326
|
+
* its reads still on their way. The second only leaves a new mark: a tab
|
|
14327
|
+
* that read between the two (it saw the first mark, the store had not yet
|
|
14328
|
+
* taken the write) holds a copy that mark makes void, while this tab's own
|
|
14329
|
+
* reads, queued behind its write and answered after it, are kept.
|
|
14330
|
+
*/
|
|
14331
|
+
touch(tables, done = false) {
|
|
14167
14332
|
if (!this.bucket) return;
|
|
14168
|
-
const index = this.index();
|
|
14333
|
+
const index = done ? null : this.index();
|
|
14169
14334
|
for (const table of tables) {
|
|
14335
|
+
this.leave(table);
|
|
14336
|
+
if (!index) continue;
|
|
14170
14337
|
this.writes.set(table, (this.writes.get(table) ?? 0) + 1);
|
|
14171
14338
|
const keys = index.get(table);
|
|
14172
14339
|
if (!keys || keys.size === 0) continue;
|
|
@@ -14179,32 +14346,64 @@ var PaintCache = class {
|
|
|
14179
14346
|
keys.clear();
|
|
14180
14347
|
}
|
|
14181
14348
|
}
|
|
14182
|
-
/**
|
|
14349
|
+
/**
|
|
14350
|
+
* Write what changed, newest reads first, up to the budget, and clear out
|
|
14351
|
+
* what no longer stands: the budget is one for all tabs of the bucket.
|
|
14352
|
+
*/
|
|
14183
14353
|
flush() {
|
|
14184
14354
|
if (this.timer) clearTimeout(this.timer);
|
|
14185
14355
|
this.timer = null;
|
|
14186
14356
|
if (!this.bucket) return;
|
|
14357
|
+
const known = this.byTable !== null;
|
|
14187
14358
|
const index = this.index();
|
|
14359
|
+
if (known) this.readIndex();
|
|
14188
14360
|
try {
|
|
14189
14361
|
let used = 0;
|
|
14190
14362
|
const kept = {};
|
|
14191
14363
|
for (const [key, entry] of [...this.entries].reverse()) {
|
|
14364
|
+
if (!this.holds(paintHead(entry.text)?.marks ?? [])) {
|
|
14365
|
+
this.entries.delete(key);
|
|
14366
|
+
continue;
|
|
14367
|
+
}
|
|
14192
14368
|
if (used + entry.text.length > this.maxChars) {
|
|
14193
14369
|
this.entries.delete(key);
|
|
14194
14370
|
localStorage.removeItem(this.name(key));
|
|
14371
|
+
for (const keys of index.values()) keys.delete(key);
|
|
14195
14372
|
continue;
|
|
14196
14373
|
}
|
|
14197
|
-
|
|
14198
|
-
kept[key] = entry.tables;
|
|
14199
|
-
if (entry.dirty) {
|
|
14374
|
+
if (entry.dirty) try {
|
|
14200
14375
|
localStorage.setItem(this.name(key), entry.text);
|
|
14201
14376
|
entry.dirty = false;
|
|
14202
|
-
|
|
14377
|
+
} catch {
|
|
14378
|
+
this.entries.delete(key);
|
|
14379
|
+
localStorage.removeItem(this.name(key));
|
|
14380
|
+
for (const keys of index.values()) keys.delete(key);
|
|
14381
|
+
continue;
|
|
14203
14382
|
}
|
|
14383
|
+
used += entry.text.length;
|
|
14384
|
+
kept[key] = entry.tables;
|
|
14204
14385
|
}
|
|
14205
|
-
|
|
14206
|
-
|
|
14207
|
-
|
|
14386
|
+
const others = /* @__PURE__ */ new Set();
|
|
14387
|
+
for (const keys of index.values()) for (const key of keys) if (!(key in kept)) others.add(key);
|
|
14388
|
+
const strays = /* @__PURE__ */ new Set();
|
|
14389
|
+
for (const key of this.copies()) {
|
|
14390
|
+
if (key in kept || others.has(key)) continue;
|
|
14391
|
+
if (this.shared) others.add(key);
|
|
14392
|
+
else strays.add(key);
|
|
14393
|
+
}
|
|
14394
|
+
for (const key of strays) localStorage.removeItem(this.name(key));
|
|
14395
|
+
for (const key of others) {
|
|
14396
|
+
const text = localStorage.getItem(this.name(key));
|
|
14397
|
+
const head = text ? paintHead(text) : null;
|
|
14398
|
+
if (!text || !head || !this.holds(head.marks) || used + text.length > this.maxChars) {
|
|
14399
|
+
if (text) localStorage.removeItem(this.name(key));
|
|
14400
|
+
for (const keys of index.values()) keys.delete(key);
|
|
14401
|
+
continue;
|
|
14402
|
+
}
|
|
14403
|
+
used += text.length;
|
|
14404
|
+
const tables = head.marks.map((mark) => mark[0]);
|
|
14405
|
+
kept[key] = tables;
|
|
14406
|
+
this.link(key, tables);
|
|
14208
14407
|
}
|
|
14209
14408
|
localStorage.setItem(`sp00ky.paint.${this.bucket}`, JSON.stringify(kept));
|
|
14210
14409
|
} catch {}
|
|
@@ -14369,8 +14568,10 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14369
14568
|
engineKind = "rows";
|
|
14370
14569
|
/** The transport in use wraps a worker `prewarmRows` started. */
|
|
14371
14570
|
adopted = false;
|
|
14571
|
+
/** Drops what the adopted worker read ahead of the app, while any of it is still held. */
|
|
14572
|
+
forgetAhead = null;
|
|
14372
14573
|
/** Last-seen startup reads, for {@link peekByIds} / {@link peekSelect}. Off for a memory store. */
|
|
14373
|
-
paint = this.useOpfs && this.config.paintCache !== false && typeof localStorage !== "undefined" ? new PaintCache(1e6) : null;
|
|
14574
|
+
paint = this.useOpfs && this.config.paintCache !== false && typeof localStorage !== "undefined" ? new PaintCache(1e6, this.shared) : null;
|
|
14374
14575
|
createTransport() {
|
|
14375
14576
|
const pre = takePrewarmed();
|
|
14376
14577
|
if (pre?.failed) pre.worker.terminate();
|
|
@@ -14381,6 +14582,7 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14381
14582
|
ahead?.drop();
|
|
14382
14583
|
ahead = null;
|
|
14383
14584
|
};
|
|
14585
|
+
this.forgetAhead = forget;
|
|
14384
14586
|
if (ahead) background(setTimeout(forget, PAINT_CAPTURE_MS));
|
|
14385
14587
|
let opens = 0;
|
|
14386
14588
|
const call = transport.call.bind(transport);
|
|
@@ -14388,11 +14590,11 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14388
14590
|
if (type === "open") {
|
|
14389
14591
|
const asked = payload;
|
|
14390
14592
|
if (asked.workerLockName && asked.useOpfs && this.config.followerReads === "direct") asked.share = true;
|
|
14391
|
-
const mine = ahead !== null && opens++ === 0 && asked.useOpfs && asked.dbName === pre.bucket
|
|
14593
|
+
const mine = ahead !== null && opens++ === 0 && asked.useOpfs && asked.dbName === pre.bucket;
|
|
14392
14594
|
const answered = pre?.opened;
|
|
14393
14595
|
if (pre) pre.opened = void 0;
|
|
14394
14596
|
if (!mine) forget();
|
|
14395
|
-
else if (answered?.persisted) {
|
|
14597
|
+
else if (answered?.persisted && !asked.workerLockName && !this.shared) {
|
|
14396
14598
|
this.opened(transport, asked, answered);
|
|
14397
14599
|
return answered;
|
|
14398
14600
|
}
|
|
@@ -14437,7 +14639,7 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14437
14639
|
this.paint?.start(bucketId);
|
|
14438
14640
|
try {
|
|
14439
14641
|
await super.connect(bucketId);
|
|
14440
|
-
this.
|
|
14642
|
+
this.sessionBegan();
|
|
14441
14643
|
} catch (error) {
|
|
14442
14644
|
if (!this.adopted) throw error;
|
|
14443
14645
|
this.adopted = false;
|
|
@@ -14450,21 +14652,36 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14450
14652
|
}, "Prewarmed row worker was unusable; starting a fresh one");
|
|
14451
14653
|
await this.close();
|
|
14452
14654
|
await super.connect(bucketId);
|
|
14453
|
-
this.
|
|
14655
|
+
this.sessionBegan();
|
|
14454
14656
|
}
|
|
14455
14657
|
}
|
|
14456
14658
|
async switchBucket(bucketId) {
|
|
14457
14659
|
this.dropReader("bucket switch");
|
|
14458
14660
|
await super.switchBucket(bucketId);
|
|
14459
14661
|
this.paint?.start(bucketId);
|
|
14662
|
+
this.sessionBegan();
|
|
14663
|
+
}
|
|
14664
|
+
/**
|
|
14665
|
+
* The store is open. One that fell back to memory keeps no copies: its rows
|
|
14666
|
+
* are gone with the tab, and it must not write over what a tab with the
|
|
14667
|
+
* real store has kept.
|
|
14668
|
+
*/
|
|
14669
|
+
sessionBegan() {
|
|
14670
|
+
if (this.useOpfs && this.storageHealth.status !== "persistent") {
|
|
14671
|
+
this.paint?.stop();
|
|
14672
|
+
return;
|
|
14673
|
+
}
|
|
14460
14674
|
this.beginStartupReads();
|
|
14461
14675
|
}
|
|
14462
14676
|
/** A follower reads through the leader's worker; one started ahead for this tab is not needed. */
|
|
14463
14677
|
async adoptAttached(dbPort, snapshot, onPortDead) {
|
|
14464
14678
|
takePrewarmed()?.worker.terminate();
|
|
14465
14679
|
this.dropReader("attaching to a leader");
|
|
14680
|
+
const durable = this.useOpfs && snapshot.storageHealth.status === "persistent";
|
|
14681
|
+
if (durable) this.paint?.adopt(snapshot.bucketId);
|
|
14466
14682
|
await super.adoptAttached(dbPort, snapshot, onPortDead);
|
|
14467
|
-
|
|
14683
|
+
this.sessionBegan();
|
|
14684
|
+
if (this.config.followerReads === "direct" && durable) await this.startReader(snapshot.bucketId);
|
|
14468
14685
|
}
|
|
14469
14686
|
/** The follower's reader worker, while this engine follows and has one. */
|
|
14470
14687
|
reader = null;
|
|
@@ -14521,9 +14738,36 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14521
14738
|
return super.rawCall(type, payload, transfer);
|
|
14522
14739
|
}).finally(() => clearTimeout(timer));
|
|
14523
14740
|
}
|
|
14741
|
+
/**
|
|
14742
|
+
* A leader's store is this tab's own worker, as alone: one started ahead of
|
|
14743
|
+
* the app is adopted (`createTransport`), and replaced when it turns out to
|
|
14744
|
+
* be another release's.
|
|
14745
|
+
*/
|
|
14524
14746
|
async adoptOwner(...args) {
|
|
14525
14747
|
this.dropReader("becoming the leader");
|
|
14526
|
-
|
|
14748
|
+
this.paint?.adopt(args[0]);
|
|
14749
|
+
let health;
|
|
14750
|
+
try {
|
|
14751
|
+
health = await super.adoptOwner(...args);
|
|
14752
|
+
} catch (error) {
|
|
14753
|
+
if (!this.adopted || !String(error).includes("worker protocol")) throw error;
|
|
14754
|
+
this.adopted = false;
|
|
14755
|
+
try {
|
|
14756
|
+
localStorage.removeItem(ROWS_PREWARM_KEY);
|
|
14757
|
+
} catch {}
|
|
14758
|
+
this.logger.warn({
|
|
14759
|
+
error,
|
|
14760
|
+
Category: "sp00ky-client::RowsCacheEngine::adoptOwner"
|
|
14761
|
+
}, "Prewarmed row worker was unusable; starting a fresh one");
|
|
14762
|
+
health = await super.adoptOwner(...args);
|
|
14763
|
+
}
|
|
14764
|
+
this.sessionBegan();
|
|
14765
|
+
return health;
|
|
14766
|
+
}
|
|
14767
|
+
/** Another tab is about to write through this worker: what it read ahead of the app no longer stands. */
|
|
14768
|
+
async exposeClientPort(clientId, port) {
|
|
14769
|
+
this.forgetAhead?.();
|
|
14770
|
+
return super.exposeClientPort(clientId, port);
|
|
14527
14771
|
}
|
|
14528
14772
|
async releaseOwnership() {
|
|
14529
14773
|
this.dropReader("releasing the store");
|
|
@@ -14544,8 +14788,12 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14544
14788
|
startupUntil = 0;
|
|
14545
14789
|
startupTimer = null;
|
|
14546
14790
|
startupHooked = false;
|
|
14791
|
+
/** The bucket whose startup reads were last begun. */
|
|
14792
|
+
startupBucket = null;
|
|
14547
14793
|
beginStartupReads() {
|
|
14548
|
-
if (!this.useOpfs ||
|
|
14794
|
+
if (!this.useOpfs || typeof localStorage === "undefined") return;
|
|
14795
|
+
if (this.shared && this.startupBucket === this.bucketId) return;
|
|
14796
|
+
this.startupBucket = this.bucketId;
|
|
14549
14797
|
if (!this.startupHooked && typeof addEventListener === "function") {
|
|
14550
14798
|
addEventListener("pagehide", () => this.saveStartupReads());
|
|
14551
14799
|
this.startupHooked = true;
|
|
@@ -14582,7 +14830,7 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14582
14830
|
}
|
|
14583
14831
|
/** A read that answers rows. `paint` names it for the paint cache, when it is worth keeping. */
|
|
14584
14832
|
async rows(type, payload, paint) {
|
|
14585
|
-
const sentAt = paint && this.paint ? this.paint.stamp(paint.tables) :
|
|
14833
|
+
const sentAt = paint && this.paint ? this.paint.stamp(paint.tables) : "";
|
|
14586
14834
|
const reply = await this.call(type, payload);
|
|
14587
14835
|
this.noteStartupRead(type, payload, reply.buf?.byteLength ?? 0);
|
|
14588
14836
|
if (!reply.buf) return [];
|
|
@@ -14617,21 +14865,19 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14617
14865
|
/**
|
|
14618
14866
|
* What `select(plan, params)` answered the last time the app asked at
|
|
14619
14867
|
* startup, synchronously, or `null`. Available as soon as `connect` was
|
|
14620
|
-
* CALLED
|
|
14621
|
-
*
|
|
14622
|
-
*
|
|
14868
|
+
* CALLED (or a role was handed over, or `expectBucket` said which bucket):
|
|
14869
|
+
* it reads localStorage, not the store. Paint from it, then run the real
|
|
14870
|
+
* `select`, which is the answer. A copy is not handed out once any tab has
|
|
14871
|
+
* written to a table it read (see {@link PaintCache}).
|
|
14623
14872
|
*/
|
|
14624
14873
|
peekSelect(plan, params = {}, opts) {
|
|
14625
14874
|
if (!this.paint) return null;
|
|
14626
14875
|
const payload = opts?.paintKey === void 0 ? this.selectPayload(plan, params) : null;
|
|
14627
14876
|
return this.paint.get(this.selectKey(payload, opts), fromBase64$1);
|
|
14628
14877
|
}
|
|
14629
|
-
/**
|
|
14630
|
-
* See `LocalStore.expectBucket`. Not with shared tabs: the paint cache is
|
|
14631
|
-
* not kept there (other tabs write to the store unseen by this one).
|
|
14632
|
-
*/
|
|
14878
|
+
/** See `LocalStore.expectBucket`. */
|
|
14633
14879
|
expectBucket(bucketId) {
|
|
14634
|
-
|
|
14880
|
+
this.paint?.expect(bucketId);
|
|
14635
14881
|
}
|
|
14636
14882
|
/** {@link peekSelect} for `selectByIds`. */
|
|
14637
14883
|
peekByIds(table, ids, opts) {
|
|
@@ -14687,10 +14933,14 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14687
14933
|
const plan = planColumnReset(stored, fingerprints, new Set(tables), keep);
|
|
14688
14934
|
if (plan.statements.length === 0) return plan.changed;
|
|
14689
14935
|
this.paint?.touch(plan.changed);
|
|
14690
|
-
|
|
14691
|
-
|
|
14692
|
-
|
|
14693
|
-
|
|
14936
|
+
try {
|
|
14937
|
+
if (plan.changed.length > 0) await this.call("reset-versions", {
|
|
14938
|
+
tables: plan.changed,
|
|
14939
|
+
keep: [...keep]
|
|
14940
|
+
});
|
|
14941
|
+
} finally {
|
|
14942
|
+
this.paint?.touch(plan.changed, true);
|
|
14943
|
+
}
|
|
14694
14944
|
const body = encoder.encode(JSON.stringify(fingerprints));
|
|
14695
14945
|
const buf = body.buffer;
|
|
14696
14946
|
await this.call("write", {
|
|
@@ -14758,12 +15008,17 @@ var RowsCacheEngine = class extends SqliteCacheEngine {
|
|
|
14758
15008
|
async write(items) {
|
|
14759
15009
|
const { wire, buf, sync } = packWrites(items);
|
|
14760
15010
|
if (wire.length === 0) return;
|
|
14761
|
-
|
|
14762
|
-
|
|
14763
|
-
|
|
14764
|
-
|
|
14765
|
-
|
|
14766
|
-
|
|
15011
|
+
const tables = new Set(items.map((item) => item.table));
|
|
15012
|
+
this.paint?.touch(tables);
|
|
15013
|
+
try {
|
|
15014
|
+
await this.call("write", {
|
|
15015
|
+
ops: wire,
|
|
15016
|
+
buf,
|
|
15017
|
+
sync
|
|
15018
|
+
}, buf ? [buf] : void 0);
|
|
15019
|
+
} finally {
|
|
15020
|
+
this.paint?.touch(tables, true);
|
|
15021
|
+
}
|
|
14767
15022
|
}
|
|
14768
15023
|
runWriteBatch(ops) {
|
|
14769
15024
|
return this.write(itemsOf(ops));
|
|
@@ -19683,7 +19938,7 @@ function createTabsCoordinator(config, s, host) {
|
|
|
19683
19938
|
return new TabsCoordinator({
|
|
19684
19939
|
tabId: s.tabId,
|
|
19685
19940
|
fingerprint: computeTabsFingerprint({
|
|
19686
|
-
coreVersion: "1.0.0-beta.
|
|
19941
|
+
coreVersion: "1.0.0-beta.11",
|
|
19687
19942
|
schemaHash: hash53(config.schemaSurql),
|
|
19688
19943
|
endpoint: config.database.endpoint ?? "",
|
|
19689
19944
|
namespace: config.database.namespace,
|
|
@@ -20497,7 +20752,7 @@ var Sp00kyClient = class {
|
|
|
20497
20752
|
});
|
|
20498
20753
|
this.wireAdapters();
|
|
20499
20754
|
this.ready.promise.catch(() => {});
|
|
20500
|
-
|
|
20755
|
+
s.local.expectBucket?.(readBootBucketHint() ?? ANON_USER_ID);
|
|
20501
20756
|
}
|
|
20502
20757
|
/** Adapter callbacks become runtime events; runtime events reach the legacy services. */
|
|
20503
20758
|
wireAdapters() {
|
|
@@ -20677,8 +20932,9 @@ var Sp00kyClient = class {
|
|
|
20677
20932
|
* handed back at once and with no need for `init()` to have run: something
|
|
20678
20933
|
* for a component to render on its first pass, where it would have rendered
|
|
20679
20934
|
* an empty list. `null` when the local store keeps no such copy (only
|
|
20680
|
-
* `localEngine: 'rows'` with its paint cache does,
|
|
20681
|
-
*
|
|
20935
|
+
* `localEngine: 'rows'` with its paint cache does), or has none for this
|
|
20936
|
+
* query: it never rendered at startup, or a tab has written to one of its
|
|
20937
|
+
* tables since.
|
|
20682
20938
|
*
|
|
20683
20939
|
* They are rows to paint from, not a result. The query still has to be run
|
|
20684
20940
|
* and subscribed to; what that delivers is the answer, and it replaces these
|