js-bao 0.5.2 → 0.6.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.
@@ -140,15 +140,324 @@ var init_uniqueIndex = __esm({
140
140
  }
141
141
  });
142
142
 
143
+ // src/utils/largeDocuments.ts
144
+ var Y5;
145
+ var init_largeDocuments = __esm({
146
+ "src/utils/largeDocuments.ts"() {
147
+ "use strict";
148
+ Y5 = __toESM(require("yjs"), 1);
149
+ init_yjsValues();
150
+ }
151
+ });
152
+
153
+ // src/models/OrmContext.ts
154
+ var OrmContext, defaultOrmContext, liveOrmContexts;
155
+ var init_OrmContext = __esm({
156
+ "src/models/OrmContext.ts"() {
157
+ "use strict";
158
+ OrmContext = class _OrmContext {
159
+ /** Declared before the field that reads it: static initialization is ordered. */
160
+ static instanceCounter = 0;
161
+ /**
162
+ * Which instance this is, for diagnostics. A process holding several clients
163
+ * needs a way to say WHICH one a warning is about, so every context takes the
164
+ * next number as it is created and keeps it for its lifetime.
165
+ */
166
+ instanceId = ++_OrmContext.instanceCounter;
167
+ /** The engine this instance's models read and write through. */
168
+ dbEngine = null;
169
+ connectedDocuments = /* @__PURE__ */ new Map();
170
+ /** `docId_modelName` → the Y.Map that document holds for that model. */
171
+ documentYMaps = /* @__PURE__ */ new Map();
172
+ /** `subscribe()` callbacks, by model name. */
173
+ listenersMap = /* @__PURE__ */ new Map();
174
+ // Default-document resolution
175
+ modelNameToDefaultDocId = /* @__PURE__ */ new Map();
176
+ globalDefaultDocId = void 0;
177
+ defaultDocChangedListeners = /* @__PURE__ */ new Set();
178
+ modelDocMappingChangedListeners = /* @__PURE__ */ new Set();
179
+ /** Format-2 ("large document", #2816) record stores by document id. */
180
+ format2Documents = /* @__PURE__ */ new Map();
181
+ /** Format-2 epoch moves in flight (#2816). */
182
+ format2Transitions = /* @__PURE__ */ new Map();
183
+ /**
184
+ * Nested-Y.Map stringset fields this context has already observed. Per
185
+ * context, not global: a Y.Doc reconnected to a fresh instance has to be
186
+ * observed again by that instance, and a global guard would silently
187
+ * suppress it.
188
+ */
189
+ observedStringSetMaps = /* @__PURE__ */ new WeakSet();
190
+ /**
191
+ * The registry this instance's schema lookups use. `null` on the standing
192
+ * default context, where callers fall back to the global singleton.
193
+ */
194
+ modelRegistry = null;
195
+ /** Model classes routed to this context. */
196
+ boundModels = /* @__PURE__ */ new Set();
197
+ /** True once {@link destroy} has run; bound classes keep pointing here. */
198
+ destroyed = false;
199
+ /** Yjs `unobserve` thunks by owning document id. */
200
+ observerDisposers = /* @__PURE__ */ new Map();
201
+ /** Teardown the owner (the document manager) registers for `destroy()`. */
202
+ teardownHooks = /* @__PURE__ */ new Set();
203
+ static NO_DOCUMENT_KEY = "__no_document__";
204
+ /**
205
+ * Record how to remove an observer this context installed. Every `observe()`
206
+ * in the ORM pairs with one of these: without it a torn-down instance's
207
+ * callbacks keep firing on a Y.Doc that outlives it.
208
+ */
209
+ registerObserverDisposer(docId, dispose) {
210
+ const key = docId ?? _OrmContext.NO_DOCUMENT_KEY;
211
+ const list = this.observerDisposers.get(key);
212
+ if (list) list.push(dispose);
213
+ else this.observerDisposers.set(key, [dispose]);
214
+ }
215
+ /** Remove every observer this context installed for `docId`. */
216
+ disposeObserversForDocument(docId) {
217
+ const list = this.observerDisposers.get(docId);
218
+ if (!list) return;
219
+ this.observerDisposers.delete(docId);
220
+ for (const dispose of list) {
221
+ try {
222
+ dispose();
223
+ } catch {
224
+ }
225
+ }
226
+ }
227
+ disposeAllObservers() {
228
+ const buckets = Array.from(this.observerDisposers.values());
229
+ this.observerDisposers.clear();
230
+ for (const list of buckets) {
231
+ for (const dispose of list) {
232
+ try {
233
+ dispose();
234
+ } catch {
235
+ }
236
+ }
237
+ }
238
+ }
239
+ /** Run `hook` as part of `destroy()` (the format-2 binding registers here). */
240
+ onDestroy(hook) {
241
+ this.teardownHooks.add(hook);
242
+ }
243
+ /** The record store for `docId`, or `null` when the document is format 1. */
244
+ getFormat2Store(docId) {
245
+ if (!docId) return null;
246
+ return this.format2Documents.get(docId) ?? null;
247
+ }
248
+ /**
249
+ * The epoch move a write to `docId` has to wait out (#2816, behavior 10), or
250
+ * `null` when none is in flight.
251
+ */
252
+ format2WritesHeld(docId) {
253
+ if (!docId) return null;
254
+ return this.format2Transitions.get(docId) ?? null;
255
+ }
256
+ /** Run an epoch move with `docId`'s local writes held back (#2816). */
257
+ async holdFormat2Writes(docId, run) {
258
+ const inFlight = this.format2Transitions.get(docId);
259
+ if (inFlight) await inFlight.catch(() => {
260
+ });
261
+ let release;
262
+ const held = new Promise((resolve4) => {
263
+ release = resolve4;
264
+ });
265
+ this.format2Transitions.set(docId, held);
266
+ try {
267
+ return await run();
268
+ } finally {
269
+ if (this.format2Transitions.get(docId) === held) {
270
+ this.format2Transitions.delete(docId);
271
+ }
272
+ release();
273
+ }
274
+ }
275
+ /** Notify `subscribe()` callbacks registered on this context for `modelName`. */
276
+ notifyListeners(modelName, log, onError) {
277
+ const listeners = this.listenersMap.get(modelName);
278
+ if (!listeners) return;
279
+ log?.(`Notifying ${listeners.size} listeners for ${modelName}`);
280
+ listeners.forEach((callback) => {
281
+ try {
282
+ callback();
283
+ } catch (e) {
284
+ onError?.(e);
285
+ }
286
+ });
287
+ }
288
+ // ---- Default-document resolution (the `BaseModel` statics delegate here) ----
289
+ setModelDefaultDocumentId(modelName, docId) {
290
+ const previous = this.modelNameToDefaultDocId.get(modelName);
291
+ this.modelNameToDefaultDocId.set(modelName, docId);
292
+ this.announceModelDocMapping(modelName, previous, docId);
293
+ }
294
+ removeModelDefaultDocumentId(modelName) {
295
+ const previous = this.modelNameToDefaultDocId.get(modelName);
296
+ this.modelNameToDefaultDocId.delete(modelName);
297
+ this.announceModelDocMapping(modelName, previous, void 0);
298
+ }
299
+ clearModelDefaultDocumentIds() {
300
+ const entries = Array.from(this.modelNameToDefaultDocId.entries());
301
+ this.modelNameToDefaultDocId.clear();
302
+ for (const [modelName, previous] of entries) {
303
+ this.announceModelDocMapping(modelName, previous, void 0);
304
+ }
305
+ }
306
+ setGlobalDefaultDocumentId(docId) {
307
+ const previous = this.globalDefaultDocId;
308
+ this.globalDefaultDocId = docId;
309
+ this.announceDefaultDoc(previous, docId);
310
+ }
311
+ clearGlobalDefaultDocumentId() {
312
+ const previous = this.globalDefaultDocId;
313
+ this.globalDefaultDocId = void 0;
314
+ this.announceDefaultDoc(previous, void 0);
315
+ }
316
+ getModelDefaultDocumentMapping() {
317
+ return Object.fromEntries(this.modelNameToDefaultDocId.entries());
318
+ }
319
+ getDocumentIdForModel(modelName) {
320
+ return this.modelNameToDefaultDocId.get(modelName);
321
+ }
322
+ getGlobalDefaultDocumentId() {
323
+ return this.globalDefaultDocId;
324
+ }
325
+ onDefaultDocChanged(listener) {
326
+ this.defaultDocChangedListeners.add(listener);
327
+ return () => this.defaultDocChangedListeners.delete(listener);
328
+ }
329
+ onModelDocMappingChanged(listener) {
330
+ this.modelDocMappingChangedListeners.add(listener);
331
+ return () => this.modelDocMappingChangedListeners.delete(listener);
332
+ }
333
+ /** Drop the model→document defaults that point at a disconnected document. */
334
+ clearMappingsForDocId(docId) {
335
+ const toRemove = [];
336
+ for (const [modelName, mappedDocId] of this.modelNameToDefaultDocId) {
337
+ if (mappedDocId === docId) toRemove.push(modelName);
338
+ }
339
+ for (const modelName of toRemove) {
340
+ this.removeModelDefaultDocumentId(modelName);
341
+ }
342
+ if (this.globalDefaultDocId === docId) {
343
+ this.clearGlobalDefaultDocumentId();
344
+ }
345
+ }
346
+ announceDefaultDoc(previous, current) {
347
+ for (const listener of this.defaultDocChangedListeners) {
348
+ try {
349
+ listener({ previous, current });
350
+ } catch {
351
+ }
352
+ }
353
+ }
354
+ announceModelDocMapping(modelName, previous, current) {
355
+ for (const listener of this.modelDocMappingChangedListeners) {
356
+ try {
357
+ listener({ modelName, previous, current });
358
+ } catch {
359
+ }
360
+ }
361
+ }
362
+ /**
363
+ * Tear this instance down: observers off, engine destroyed, state cleared.
364
+ *
365
+ * Deliberately does NOT unbind its model classes. They keep resolving this
366
+ * context, whose engine is now null, so an operation through them throws the
367
+ * ordinary not-initialized error instead of silently landing on whatever
368
+ * other instance happens to be live (#3039, behavior 7). Only a later
369
+ * `initJsBao` may rebind a class whose context is destroyed.
370
+ */
371
+ async destroy() {
372
+ if (this.destroyed) return;
373
+ this.destroyed = true;
374
+ this.disposeAllObservers();
375
+ const hooks = Array.from(this.teardownHooks);
376
+ this.teardownHooks.clear();
377
+ for (const hook of hooks) {
378
+ try {
379
+ await hook();
380
+ } catch {
381
+ }
382
+ }
383
+ const engine = this.dbEngine;
384
+ this.dbEngine = null;
385
+ if (engine) {
386
+ try {
387
+ await engine.destroy();
388
+ } catch {
389
+ }
390
+ }
391
+ this.connectedDocuments.clear();
392
+ this.documentYMaps.clear();
393
+ this.listenersMap.clear();
394
+ this.modelNameToDefaultDocId.clear();
395
+ this.globalDefaultDocId = void 0;
396
+ this.defaultDocChangedListeners.clear();
397
+ this.modelDocMappingChangedListeners.clear();
398
+ this.format2Documents.clear();
399
+ this.format2Transitions.clear();
400
+ this.observedStringSetMaps = /* @__PURE__ */ new WeakSet();
401
+ liveOrmContexts.delete(this);
402
+ if (defaultOrmContext === this) {
403
+ defaultOrmContext = new _OrmContext();
404
+ }
405
+ }
406
+ };
407
+ defaultOrmContext = new OrmContext();
408
+ liveOrmContexts = /* @__PURE__ */ new Set();
409
+ }
410
+ });
411
+
412
+ // src/models/format2QueryProjection.ts
413
+ var FORMAT2_QUERY_PROJECTION_DDL;
414
+ var init_format2QueryProjection = __esm({
415
+ "src/models/format2QueryProjection.ts"() {
416
+ "use strict";
417
+ FORMAT2_QUERY_PROJECTION_DDL = `
418
+ CREATE TABLE IF NOT EXISTS _query_projection (
419
+ doc_id TEXT NOT NULL,
420
+ model TEXT NOT NULL,
421
+ PRIMARY KEY (doc_id, model)
422
+ )`.trim();
423
+ }
424
+ });
425
+
426
+ // src/models/format2Observer.ts
427
+ var Y6;
428
+ var init_format2Observer = __esm({
429
+ "src/models/format2Observer.ts"() {
430
+ "use strict";
431
+ Y6 = __toESM(require("yjs"), 1);
432
+ init_largeDocuments();
433
+ }
434
+ });
435
+
436
+ // src/models/format2ClientWrite.ts
437
+ var init_format2ClientWrite = __esm({
438
+ "src/models/format2ClientWrite.ts"() {
439
+ "use strict";
440
+ }
441
+ });
442
+
443
+ // src/models/format2OfflineWindow.ts
444
+ var DAY_MS;
445
+ var init_format2OfflineWindow = __esm({
446
+ "src/models/format2OfflineWindow.ts"() {
447
+ "use strict";
448
+ DAY_MS = 24 * 60 * 60 * 1e3;
449
+ }
450
+ });
451
+
143
452
  // src/models/BaseModel.ts
144
453
  function generateULID() {
145
454
  return (0, import_ulid2.ulid)();
146
455
  }
147
- var Y5, import_ulid2;
456
+ var Y7, import_ulid2;
148
457
  var init_BaseModel = __esm({
149
458
  "src/models/BaseModel.ts"() {
150
459
  "use strict";
151
- Y5 = __toESM(require("yjs"), 1);
460
+ Y7 = __toESM(require("yjs"), 1);
152
461
  import_ulid2 = require("ulid");
153
462
  init_StringSet();
154
463
  init_documentTypes();
@@ -159,6 +468,12 @@ var init_BaseModel = __esm({
159
468
  init_writeLifecycle();
160
469
  init_yjsValues();
161
470
  init_uniqueIndex();
471
+ init_largeDocuments();
472
+ init_OrmContext();
473
+ init_format2QueryProjection();
474
+ init_format2Observer();
475
+ init_format2ClientWrite();
476
+ init_format2OfflineWindow();
162
477
  registerFunctionDefault(generateULID, "generate_ulid");
163
478
  }
164
479
  });
@@ -177,6 +492,7 @@ var init_ModelRegistry = __esm({
177
492
  "use strict";
178
493
  init_BaseModel();
179
494
  init_relationshipManager();
495
+ init_format2QueryProjection();
180
496
  }
181
497
  });
182
498
 
@@ -1593,7 +1909,7 @@ var Logger2 = class _Logger {
1593
1909
  // package.json
1594
1910
  var package_default = {
1595
1911
  name: "js-bao",
1596
- version: "0.5.2",
1912
+ version: "0.6.0",
1597
1913
  description: "A library providing data modeling capabilities which support live updates and queries.",
1598
1914
  types: "dist/index.d.ts",
1599
1915
  type: "module",
package/dist/codegen.cjs CHANGED
@@ -1186,7 +1186,7 @@ var SchemaExtractor = class {
1186
1186
  // package.json
1187
1187
  var package_default = {
1188
1188
  name: "js-bao",
1189
- version: "0.5.2",
1189
+ version: "0.6.0",
1190
1190
  description: "A library providing data modeling capabilities which support live updates and queries.",
1191
1191
  types: "dist/index.d.ts",
1192
1192
  type: "module",