@dialecte/core 0.3.0 → 0.4.2

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.
@@ -124,7 +124,7 @@ function te(e) {
124
124
  deleted: a
125
125
  };
126
126
  }
127
- function ne(e) {
127
+ function T(e) {
128
128
  let t = /* @__PURE__ */ new Set();
129
129
  for (let n of e) n.status === "created" && t.add(n.newRecord.id);
130
130
  let n = /* @__PURE__ */ new Map();
@@ -144,42 +144,45 @@ function ne(e) {
144
144
  }
145
145
  //#endregion
146
146
  //#region src/document/query/get/record/get-record.ts
147
- async function T(e) {
148
- let { context: t, ref: n } = e;
149
- if (t.stagedOperations.length > 0) {
147
+ async function E(e) {
148
+ let { context: n, ref: r } = e;
149
+ if (n.stagedOperations.length > 0) {
150
150
  let e = ee({
151
- stagedOperations: t.stagedOperations,
152
- tagName: n.tagName,
153
- id: n.id
151
+ stagedOperations: n.stagedOperations,
152
+ tagName: r.tagName,
153
+ id: r.id
154
154
  });
155
155
  if (e?.status === "deleted") return;
156
156
  if (e) return e;
157
157
  }
158
- let r;
159
- if (n.id === void 0) {
160
- let e = `__singleton_${n.tagName}`, i = Lt(t) ? t.recordCache.get(e) : void 0;
161
- i ? r = i : (r = (await t.store.getByTagNameInDocument(n.tagName, t.documentId))[0], r && Lt(t) && (t.recordCache.set(r.id, r), t.recordCache.set(e, r)));
158
+ let i;
159
+ if (r.id === void 0) {
160
+ let e = `__singleton_${r.tagName}`, t = Lt(n) ? n.recordCache.get(e) : void 0;
161
+ t ? i = t : (i = (await n.store.getByTagNameInDocument(r.tagName, n.documentId))[0], i && Lt(n) && (n.recordCache.set(i.id, i), n.recordCache.set(e, i)));
162
162
  } else {
163
- let e = Lt(t) ? t.recordCache.get(n.id) : void 0;
164
- e ? r = e : (r = await t.store.get(n.id, t.documentId), r && Lt(t) && t.recordCache.set(n.id, r));
163
+ let e = Lt(n) ? n.recordCache.get(r.id) : void 0;
164
+ e ? i = e : (i = await n.store.get(r.id, n.documentId), i && Lt(n) && n.recordCache.set(r.id, i));
165
165
  }
166
- if (r) return {
167
- ...r,
166
+ if (i) return i.tagName !== r.tagName && t("ELEMENT_TAGNAME_MISMATCH", {
167
+ detail: `Expected tagName '${r.tagName}', got '${i.tagName}' for id '${r.id}'`,
168
+ ref: r
169
+ }), {
170
+ ...i,
168
171
  status: "unchanged"
169
172
  };
170
173
  }
171
174
  //#endregion
172
175
  //#region src/document/query/get/record/get-records.ts
173
- async function re(e) {
176
+ async function ne(e) {
174
177
  let { context: t, refs: n } = e;
175
- return Promise.all(n.map((e) => T({
178
+ return Promise.all(n.map((e) => E({
176
179
  context: t,
177
180
  ref: e
178
181
  })));
179
182
  }
180
183
  //#endregion
181
184
  //#region src/document/query/get/record/get-records-by-tagname.ts
182
- async function ie(e) {
185
+ async function re(e) {
183
186
  let { context: t, tagName: n } = e, r = await t.store.getByTagNameInDocument(n, t.documentId);
184
187
  for (let e of r) Lt(t) && t.recordCache.set(e.id, e);
185
188
  return w({
@@ -190,14 +193,14 @@ async function ie(e) {
190
193
  }
191
194
  //#endregion
192
195
  //#region src/document/query/get/record/get-child.ts
193
- async function ae(e) {
194
- let { context: t, ref: n, tagName: r } = e, i = await T({
196
+ async function ie(e) {
197
+ let { context: t, ref: n, tagName: r } = e, i = await E({
195
198
  context: t,
196
199
  ref: n
197
200
  });
198
201
  if (!i) return;
199
202
  let a = i.children.find((e) => e.tagName === r);
200
- if (a) return T({
203
+ if (a) return E({
201
204
  context: t,
202
205
  ref: a
203
206
  });
@@ -205,13 +208,13 @@ async function ae(e) {
205
208
  if (!o?.length) return;
206
209
  let s = i.children.filter((e) => o.includes(e.tagName));
207
210
  for (let e of s) {
208
- let n = await T({
211
+ let n = await E({
209
212
  context: t,
210
213
  ref: e
211
214
  });
212
215
  if (!n) continue;
213
216
  let i = n.children.find((e) => e.tagName === r);
214
- if (i) return T({
217
+ if (i) return E({
215
218
  context: t,
216
219
  ref: i
217
220
  });
@@ -219,8 +222,8 @@ async function ae(e) {
219
222
  }
220
223
  //#endregion
221
224
  //#region src/document/query/get/record/get-children.ts
222
- async function E(e) {
223
- let { context: t, ref: n, tagName: r } = e, i = await T({
225
+ async function D(e) {
226
+ let { context: t, ref: n, tagName: r } = e, i = await E({
224
227
  context: t,
225
228
  ref: n
226
229
  });
@@ -229,7 +232,7 @@ async function E(e) {
229
232
  tagName: r,
230
233
  id: e.id
231
234
  }));
232
- if (a.length) return (await re({
235
+ if (a.length) return (await ne({
233
236
  context: t,
234
237
  refs: a
235
238
  })).filter((e) => e !== void 0);
@@ -237,27 +240,27 @@ async function E(e) {
237
240
  if (!o?.length) return [];
238
241
  let s = i.children.filter((e) => o.includes(e.tagName));
239
242
  if (!s.length) return [];
240
- let c = (await re({
243
+ let c = (await ne({
241
244
  context: t,
242
245
  refs: s
243
246
  })).filter((e) => e !== void 0).flatMap((e) => e.children.filter((e) => e.tagName === r)).map((e) => ({
244
247
  tagName: r,
245
248
  id: e.id
246
249
  }));
247
- return c.length ? (await re({
250
+ return c.length ? (await ne({
248
251
  context: t,
249
252
  refs: c
250
253
  })).filter((e) => e !== void 0) : [];
251
254
  }
252
255
  //#endregion
253
256
  //#region src/document/query/find/ancestor/find-ancestor.ts
254
- async function D(e) {
255
- let { context: t, ref: n, options: r } = e, i = r?.depth ?? Infinity, a = r?.stopAtTagName, o = r?.order ?? "bottom-up", s = [], c = await T({
257
+ async function O(e) {
258
+ let { context: t, ref: n, options: r } = e, i = r?.depth ?? Infinity, a = r?.stopAtTagName, o = r?.order ?? "bottom-up", s = [], c = await E({
256
259
  context: t,
257
260
  ref: n
258
261
  });
259
262
  for (; c?.parent && s.length < i;) {
260
- let e = c.parent, n = await T({
263
+ let e = c.parent, n = await E({
261
264
  context: t,
262
265
  ref: e
263
266
  });
@@ -268,8 +271,8 @@ async function D(e) {
268
271
  }
269
272
  //#endregion
270
273
  //#region src/document/query/find/by-attribute/find-by-attributes.ts
271
- async function O(e) {
272
- let { context: t, tagName: n, attributes: r } = e, i = await ie({
274
+ async function ae(e) {
275
+ let { context: t, tagName: n, attributes: r } = e, i = await re({
273
276
  context: t,
274
277
  tagName: n
275
278
  }), a = [];
@@ -297,7 +300,7 @@ function k(e) {
297
300
  async function oe(e) {
298
301
  let { context: t, ref: n, options: r } = e, { collect: i, omit: a } = r, o = A(a), s = se(i), c = /* @__PURE__ */ new Map();
299
302
  for (let e of s.allTags) c.set(e, /* @__PURE__ */ new Map());
300
- let l = await T({
303
+ let l = await E({
301
304
  context: t,
302
305
  ref: n
303
306
  });
@@ -412,7 +415,7 @@ async function ue(e) {
412
415
  let { context: t, rootId: n, collectSpec: r, omitSpec: i, collected: a } = e;
413
416
  for (let e of r.targets) {
414
417
  if (i.unconditional.has(e.tagName)) continue;
415
- let r = await ie({
418
+ let r = await re({
416
419
  context: t,
417
420
  tagName: e.tagName
418
421
  });
@@ -444,7 +447,7 @@ async function fe(e) {
444
447
  for (; a;) {
445
448
  if (!a.parent || i.unconditional.has(a.parent.tagName)) return !1;
446
449
  if (a.parent.id === r) return !0;
447
- let e = await T({
450
+ let e = await E({
448
451
  context: t,
449
452
  ref: f(a.parent)
450
453
  });
@@ -510,7 +513,7 @@ async function me(e) {
510
513
  let { context: t, record: n, omitSpec: r } = e;
511
514
  if (!n.children?.length) return [];
512
515
  let i = n.children.filter((e) => !r.unconditional.has(e.tagName));
513
- return i.length ? (await Promise.all(i.map((e) => T({
516
+ return i.length ? (await Promise.all(i.map((e) => E({
514
517
  context: t,
515
518
  ref: f(e)
516
519
  })))).filter((e) => e !== void 0 && !j({
@@ -527,14 +530,14 @@ function he(e) {
527
530
  //#region src/document/query/get/attribute/get-attribute.ts
528
531
  async function N(e) {
529
532
  let { context: t, ref: n, name: r } = e;
530
- return ((await T({
533
+ return ((await E({
531
534
  context: t,
532
535
  ref: n
533
536
  }))?.attributes ?? []).find((e) => e.name === r)?.value ?? "";
534
537
  }
535
538
  async function ge(e) {
536
539
  let { context: t, ref: n, name: r } = e;
537
- return ((await T({
540
+ return ((await E({
538
541
  context: t,
539
542
  ref: n
540
543
  }))?.attributes ?? []).find((e) => e.name === r);
@@ -543,14 +546,14 @@ async function ge(e) {
543
546
  //#region src/document/query/get/attribute/get-attributes.ts
544
547
  async function _e(e) {
545
548
  let { context: t, ref: n } = e;
546
- return ((await T({
549
+ return ((await E({
547
550
  context: t,
548
551
  ref: n
549
552
  }))?.attributes ?? []).reduce((e, t) => (e[t.name] = t.value ?? "", e), {});
550
553
  }
551
554
  async function ve(e) {
552
555
  let { context: t, ref: n } = e;
553
- return (await T({
556
+ return (await E({
554
557
  context: t,
555
558
  ref: n
556
559
  }))?.attributes ?? [];
@@ -655,7 +658,7 @@ function P(e) {
655
658
  //#endregion
656
659
  //#region src/document/query/get/tree/get-tree.ts
657
660
  async function we(e) {
658
- let { context: t, ref: n, options: r = {}, dialecteConfig: a } = e, { select: o, omit: s, unwrap: c } = r, l = await T({
661
+ let { context: t, ref: n, options: r = {}, dialecteConfig: a } = e, { select: o, omit: s, unwrap: c } = r, l = await E({
659
662
  context: t,
660
663
  ref: n
661
664
  });
@@ -722,7 +725,7 @@ async function Ee(e) {
722
725
  transparentElements: o
723
726
  }));
724
727
  return c.length ? Ae({
725
- children: (await Promise.all(c.map((e) => T({
728
+ children: (await Promise.all(c.map((e) => E({
726
729
  context: t,
727
730
  ref: f(e)
728
731
  })))).filter((e) => e !== void 0).filter((e) => !xe({
@@ -841,17 +844,17 @@ async function Pe(e) {
841
844
  context: t,
842
845
  includeDeleted: o
843
846
  });
844
- let s = e.siblings ?? !1, c = s === !0 || typeof s == "object" && !!s, l = typeof s == "object" && !!s && s.expand === !0, u = n ?? { tagName: t.dialecteConfig.rootElementName }, d = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), m = o ? ne(t.stagedOperations) : void 0, h = (e) => {
847
+ let s = e.siblings ?? !1, c = s === !0 || typeof s == "object" && !!s, l = typeof s == "object" && !!s && s.expand === !0, u = n ?? { tagName: t.dialecteConfig.rootElementName }, d = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), m = o ? T(t.stagedOperations) : void 0, h = (e) => {
845
848
  if (m) for (let t of m.get(e) ?? []) p.has(t.id) || (p.set(t.id, t), h(t.id));
846
849
  }, g = async (e, n) => {
847
850
  if (d.set(e.id, e), h(e.id), !(a !== void 0 && n >= a)) for (let r of e.children) {
848
- let e = await T({
851
+ let e = await E({
849
852
  context: t,
850
853
  ref: f(r)
851
854
  });
852
855
  e && await g(e, n + 1);
853
856
  }
854
- }, _ = await T({
857
+ }, _ = await E({
855
858
  context: t,
856
859
  ref: u
857
860
  });
@@ -863,7 +866,7 @@ async function Pe(e) {
863
866
  await g(_, 0);
864
867
  let y = Math.max(r, +!!c);
865
868
  if (y > 0) {
866
- let e = await D({
869
+ let e = await O({
867
870
  context: t,
868
871
  ref: u,
869
872
  options: {
@@ -878,7 +881,7 @@ async function Pe(e) {
878
881
  let r = n[e], i = n[e + 1];
879
882
  for (let e of i.children) {
880
883
  if (e.id === r.id) continue;
881
- let n = await T({
884
+ let n = await E({
882
885
  context: t,
883
886
  ref: f(e)
884
887
  });
@@ -1429,7 +1432,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1429
1432
  function n(t, r) {
1430
1433
  if (!(this instanceof n)) return new n(t, r);
1431
1434
  var a = this;
1432
- i(a), a.q = a.c = "", a.bufferCheckPosition = e.MAX_BUFFER_LENGTH, a.encoding = null, a.opt = r || {}, a.opt.lowercase = a.opt.lowercase || a.opt.lowercasetags, a.looseCase = a.opt.lowercase ? "toLowerCase" : "toUpperCase", a.opt.maxEntityCount = a.opt.maxEntityCount || 512, a.opt.maxEntityDepth = a.opt.maxEntityDepth || 4, a.entityCount = a.entityDepth = 0, a.tags = [], a.closed = a.closedRoot = a.sawRoot = !1, a.tag = a.error = null, a.strict = !!t, a.noscript = !!(t || a.opt.noscript), a.state = w.BEGIN, a.strictEntities = a.opt.strictEntities, a.ENTITIES = a.strictEntities ? Object.create(e.XML_ENTITIES) : Object.create(e.ENTITIES), a.attribList = [], a.opt.xmlns && (a.ns = Object.create(h)), a.opt.unquotedAttributeValues === void 0 && (a.opt.unquotedAttributeValues = !t), a.trackPosition = a.opt.position !== !1, a.trackPosition && (a.position = a.line = a.column = 0), ne(a, "onready");
1435
+ i(a), a.q = a.c = "", a.bufferCheckPosition = e.MAX_BUFFER_LENGTH, a.encoding = null, a.opt = r || {}, a.opt.lowercase = a.opt.lowercase || a.opt.lowercasetags, a.looseCase = a.opt.lowercase ? "toLowerCase" : "toUpperCase", a.opt.maxEntityCount = a.opt.maxEntityCount || 512, a.opt.maxEntityDepth = a.opt.maxEntityDepth || 4, a.entityCount = a.entityDepth = 0, a.tags = [], a.closed = a.closedRoot = a.sawRoot = !1, a.tag = a.error = null, a.strict = !!t, a.noscript = !!(t || a.opt.noscript), a.state = w.BEGIN, a.strictEntities = a.opt.strictEntities, a.ENTITIES = a.strictEntities ? Object.create(e.XML_ENTITIES) : Object.create(e.ENTITIES), a.attribList = [], a.opt.xmlns && (a.ns = Object.create(h)), a.opt.unquotedAttributeValues === void 0 && (a.opt.unquotedAttributeValues = !t), a.trackPosition = a.opt.position !== !1, a.trackPosition && (a.position = a.line = a.column = 0), T(a, "onready");
1433
1436
  }
1434
1437
  Object.create || (Object.create = function(e) {
1435
1438
  function t() {}
@@ -1444,13 +1447,13 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1444
1447
  var s = n[t[a]].length;
1445
1448
  if (s > r) switch (t[a]) {
1446
1449
  case "textNode":
1447
- D(n);
1450
+ O(n);
1448
1451
  break;
1449
1452
  case "cdata":
1450
- E(n, "oncdata", n.cdata), n.cdata = "";
1453
+ D(n, "oncdata", n.cdata), n.cdata = "";
1451
1454
  break;
1452
1455
  case "script":
1453
- E(n, "onscript", n.script), n.script = "";
1456
+ D(n, "onscript", n.script), n.script = "";
1454
1457
  break;
1455
1458
  default: k(n, "Max buffer length exceeded: " + t[a]);
1456
1459
  }
@@ -1462,7 +1465,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1462
1465
  for (var n = 0, r = t.length; n < r; n++) e[t[n]] = "";
1463
1466
  }
1464
1467
  function a(e) {
1465
- D(e), e.cdata !== "" && (E(e, "oncdata", e.cdata), e.cdata = ""), e.script !== "" && (E(e, "onscript", e.script), e.script = "");
1468
+ O(e), e.cdata !== "" && (D(e, "oncdata", e.cdata), e.cdata = ""), e.script !== "" && (D(e, "onscript", e.script), e.script = "");
1466
1469
  }
1467
1470
  n.prototype = {
1468
1471
  end: function() {
@@ -1873,40 +1876,40 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1873
1876
  e.ENTITIES[t] = r;
1874
1877
  }), e.STATE) e.STATE[e.STATE[te]] = te;
1875
1878
  w = e.STATE;
1876
- function ne(e, t, n) {
1879
+ function T(e, t, n) {
1877
1880
  e[t] && e[t](n);
1878
1881
  }
1879
- function T(e) {
1882
+ function E(e) {
1880
1883
  var t = e && e.match(/(?:^|\s)encoding\s*=\s*(['"])([^'"]+)\1/i);
1881
1884
  return t ? t[2] : null;
1882
1885
  }
1883
- function re(e) {
1886
+ function ne(e) {
1884
1887
  return e ? e.toLowerCase().replace(/[^a-z0-9]/g, "") : null;
1885
1888
  }
1886
- function ie(e, t) {
1887
- let n = re(e), r = re(t);
1889
+ function re(e, t) {
1890
+ let n = ne(e), r = ne(t);
1888
1891
  return !n || !r ? !0 : r === "utf16" ? n === "utf16le" || n === "utf16be" : n === r;
1889
1892
  }
1890
- function ae(e, t) {
1893
+ function ie(e, t) {
1891
1894
  if (!(!e.strict || !e.encoding || !t || t.name !== "xml")) {
1892
- var n = T(t.body);
1893
- n && !ie(e.encoding, n) && A(e, "XML declaration encoding " + n + " does not match detected stream encoding " + e.encoding.toUpperCase());
1895
+ var n = E(t.body);
1896
+ n && !re(e.encoding, n) && A(e, "XML declaration encoding " + n + " does not match detected stream encoding " + e.encoding.toUpperCase());
1894
1897
  }
1895
1898
  }
1896
- function E(e, t, n) {
1897
- e.textNode && D(e), ne(e, t, n);
1899
+ function D(e, t, n) {
1900
+ e.textNode && O(e), T(e, t, n);
1898
1901
  }
1899
- function D(e) {
1900
- e.textNode = O(e.opt, e.textNode), e.textNode && ne(e, "ontext", e.textNode), e.textNode = "";
1902
+ function O(e) {
1903
+ e.textNode = ae(e.opt, e.textNode), e.textNode && T(e, "ontext", e.textNode), e.textNode = "";
1901
1904
  }
1902
- function O(e, t) {
1905
+ function ae(e, t) {
1903
1906
  return e.trim && (t = t.trim()), e.normalize && (t = t.replace(/\s+/g, " ")), t;
1904
1907
  }
1905
1908
  function k(e, t) {
1906
- return D(e), e.trackPosition && (t += "\nLine: " + e.line + "\nColumn: " + e.column + "\nChar: " + e.c), t = Error(t), e.error = t, ne(e, "onerror", t), e;
1909
+ return O(e), e.trackPosition && (t += "\nLine: " + e.line + "\nColumn: " + e.column + "\nChar: " + e.c), t = Error(t), e.error = t, T(e, "onerror", t), e;
1907
1910
  }
1908
1911
  function oe(e) {
1909
- return e.sawRoot && !e.closedRoot && A(e, "Unclosed root tag"), e.state !== w.BEGIN && e.state !== w.BEGIN_WHITESPACE && e.state !== w.TEXT && k(e, "Unexpected end"), D(e), e.c = "", e.closed = !0, ne(e, "onend"), n.call(e, e.strict, e.opt), e;
1912
+ return e.sawRoot && !e.closedRoot && A(e, "Unclosed root tag"), e.state !== w.BEGIN && e.state !== w.BEGIN_WHITESPACE && e.state !== w.TEXT && k(e, "Unexpected end"), O(e), e.c = "", e.closed = !0, T(e, "onend"), n.call(e, e.strict, e.opt), e;
1910
1913
  }
1911
1914
  function A(e, t) {
1912
1915
  if (typeof e != "object" || !(e instanceof n)) throw Error("bad call to strictFail");
@@ -1918,7 +1921,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1918
1921
  name: e.tagName,
1919
1922
  attributes: {}
1920
1923
  };
1921
- e.opt.xmlns && (n.ns = t.ns), e.attribList.length = 0, E(e, "onopentagstart", n);
1924
+ e.opt.xmlns && (n.ns = t.ns), e.attribList.length = 0, D(e, "onopentagstart", n);
1922
1925
  }
1923
1926
  function se(e, t) {
1924
1927
  var n = e.indexOf(":") < 0 ? ["", e] : e.split(":"), r = n[0], i = n[1];
@@ -1941,7 +1944,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1941
1944
  i.ns === a.ns && (i.ns = Object.create(a.ns)), i.ns[r] = e.attribValue;
1942
1945
  }
1943
1946
  e.attribList.push([e.attribName, e.attribValue]);
1944
- } else e.tag.attributes[e.attribName] = e.attribValue, E(e, "onattribute", {
1947
+ } else e.tag.attributes[e.attribName] = e.attribValue, D(e, "onattribute", {
1945
1948
  name: e.attribName,
1946
1949
  value: e.attribValue
1947
1950
  });
@@ -1953,7 +1956,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1953
1956
  n.prefix = r.prefix, n.local = r.local, n.uri = n.ns[r.prefix] || "", n.prefix && !n.uri && (A(e, "Unbound namespace prefix: " + JSON.stringify(e.tagName)), n.uri = r.prefix);
1954
1957
  var i = e.tags[e.tags.length - 1] || e;
1955
1958
  n.ns && i.ns !== n.ns && Object.keys(n.ns).forEach(function(t) {
1956
- E(e, "onopennamespace", {
1959
+ D(e, "onopennamespace", {
1957
1960
  prefix: t,
1958
1961
  uri: n.ns[t]
1959
1962
  });
@@ -1966,11 +1969,11 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1966
1969
  local: f,
1967
1970
  uri: p
1968
1971
  };
1969
- d && d !== "xmlns" && !p && (A(e, "Unbound namespace prefix: " + JSON.stringify(d)), m.uri = d), e.tag.attributes[c] = m, E(e, "onattribute", m);
1972
+ d && d !== "xmlns" && !p && (A(e, "Unbound namespace prefix: " + JSON.stringify(d)), m.uri = d), e.tag.attributes[c] = m, D(e, "onattribute", m);
1970
1973
  }
1971
1974
  e.attribList.length = 0;
1972
1975
  }
1973
- e.tag.isSelfClosing = !!t, e.sawRoot = !0, e.tags.push(e.tag), E(e, "onopentag", e.tag), t || (!e.noscript && e.tagName.toLowerCase() === "script" ? e.state = w.SCRIPT : e.state = w.TEXT, e.tag = null, e.tagName = ""), e.attribName = e.attribValue = "", e.attribList.length = 0;
1976
+ e.tag.isSelfClosing = !!t, e.sawRoot = !0, e.tags.push(e.tag), D(e, "onopentag", e.tag), t || (!e.noscript && e.tagName.toLowerCase() === "script" ? e.state = w.SCRIPT : e.state = w.TEXT, e.tag = null, e.tagName = ""), e.attribName = e.attribValue = "", e.attribList.length = 0;
1974
1977
  }
1975
1978
  function ue(e) {
1976
1979
  if (!e.tagName) {
@@ -1982,7 +1985,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1982
1985
  e.script += "</" + e.tagName + ">", e.tagName = "", e.state = w.SCRIPT;
1983
1986
  return;
1984
1987
  }
1985
- E(e, "onscript", e.script), e.script = "";
1988
+ D(e, "onscript", e.script), e.script = "";
1986
1989
  }
1987
1990
  var t = e.tags.length, n = e.tagName;
1988
1991
  e.strict || (n = n[e.looseCase]());
@@ -1994,13 +1997,13 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
1994
1997
  e.tagName = n;
1995
1998
  for (var i = e.tags.length; i-- > t;) {
1996
1999
  var a = e.tag = e.tags.pop();
1997
- e.tagName = e.tag.name, E(e, "onclosetag", e.tagName);
2000
+ e.tagName = e.tag.name, D(e, "onclosetag", e.tagName);
1998
2001
  var o = {};
1999
2002
  for (var s in a.ns) o[s] = a.ns[s];
2000
2003
  var c = e.tags[e.tags.length - 1] || e;
2001
2004
  e.opt.xmlns && a.ns !== c.ns && Object.keys(a.ns).forEach(function(t) {
2002
2005
  var n = a.ns[t];
2003
- E(e, "onclosenamespace", {
2006
+ D(e, "onclosenamespace", {
2004
2007
  prefix: t,
2005
2008
  uri: n
2006
2009
  });
@@ -2064,13 +2067,13 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
2064
2067
  n.state = w.COMMENT, n.comment = "", n.sgmlDecl = "";
2065
2068
  continue;
2066
2069
  }
2067
- n.doctype && n.doctype !== !0 && n.sgmlDecl ? (n.state = w.DOCTYPE_DTD, n.doctype += "<!" + n.sgmlDecl + a, n.sgmlDecl = "") : (n.sgmlDecl + a).toUpperCase() === d ? (E(n, "onopencdata"), n.state = w.CDATA, n.sgmlDecl = "", n.cdata = "") : (n.sgmlDecl + a).toUpperCase() === f ? (n.state = w.DOCTYPE, (n.doctype || n.sawRoot) && A(n, "Inappropriately located doctype declaration"), n.doctype = "", n.sgmlDecl = "") : a === ">" ? (E(n, "onsgmldeclaration", n.sgmlDecl), n.sgmlDecl = "", n.state = w.TEXT) : (x(a) && (n.state = w.SGML_DECL_QUOTED), n.sgmlDecl += a);
2070
+ n.doctype && n.doctype !== !0 && n.sgmlDecl ? (n.state = w.DOCTYPE_DTD, n.doctype += "<!" + n.sgmlDecl + a, n.sgmlDecl = "") : (n.sgmlDecl + a).toUpperCase() === d ? (D(n, "onopencdata"), n.state = w.CDATA, n.sgmlDecl = "", n.cdata = "") : (n.sgmlDecl + a).toUpperCase() === f ? (n.state = w.DOCTYPE, (n.doctype || n.sawRoot) && A(n, "Inappropriately located doctype declaration"), n.doctype = "", n.sgmlDecl = "") : a === ">" ? (D(n, "onsgmldeclaration", n.sgmlDecl), n.sgmlDecl = "", n.state = w.TEXT) : (x(a) && (n.state = w.SGML_DECL_QUOTED), n.sgmlDecl += a);
2068
2071
  continue;
2069
2072
  case w.SGML_DECL_QUOTED:
2070
2073
  a === n.q && (n.state = w.SGML_DECL, n.q = ""), n.sgmlDecl += a;
2071
2074
  continue;
2072
2075
  case w.DOCTYPE:
2073
- a === ">" ? (n.state = w.TEXT, E(n, "ondoctype", n.doctype), n.doctype = !0) : (n.doctype += a, a === "[" ? n.state = w.DOCTYPE_DTD : x(a) && (n.state = w.DOCTYPE_QUOTED, n.q = a));
2076
+ a === ">" ? (n.state = w.TEXT, D(n, "ondoctype", n.doctype), n.doctype = !0) : (n.doctype += a, a === "[" ? n.state = w.DOCTYPE_DTD : x(a) && (n.state = w.DOCTYPE_QUOTED, n.q = a));
2074
2077
  continue;
2075
2078
  case w.DOCTYPE_QUOTED:
2076
2079
  n.doctype += a, a === n.q && (n.q = "", n.state = w.DOCTYPE);
@@ -2085,7 +2088,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
2085
2088
  a === "-" ? n.state = w.COMMENT_ENDING : n.comment += a;
2086
2089
  continue;
2087
2090
  case w.COMMENT_ENDING:
2088
- a === "-" ? (n.state = w.COMMENT_ENDED, n.comment = O(n.opt, n.comment), n.comment && E(n, "oncomment", n.comment), n.comment = "") : (n.comment += "-" + a, n.state = w.COMMENT);
2091
+ a === "-" ? (n.state = w.COMMENT_ENDED, n.comment = ae(n.opt, n.comment), n.comment && D(n, "oncomment", n.comment), n.comment = "") : (n.comment += "-" + a, n.state = w.COMMENT);
2089
2092
  continue;
2090
2093
  case w.COMMENT_ENDED:
2091
2094
  a === ">" ? n.doctype && n.doctype !== !0 ? n.state = w.DOCTYPE_DTD : n.state = w.TEXT : (A(n, "Malformed comment"), n.comment += "--" + a, n.state = w.COMMENT);
@@ -2098,7 +2101,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
2098
2101
  a === "]" ? n.state = w.CDATA_ENDING_2 : (n.cdata += "]" + a, n.state = w.CDATA);
2099
2102
  continue;
2100
2103
  case w.CDATA_ENDING_2:
2101
- a === ">" ? (n.cdata && E(n, "oncdata", n.cdata), E(n, "onclosecdata"), n.cdata = "", n.state = w.TEXT) : a === "]" ? n.cdata += "]" : (n.cdata += "]]" + a, n.state = w.CDATA);
2104
+ a === ">" ? (n.cdata && D(n, "oncdata", n.cdata), D(n, "onclosecdata"), n.cdata = "", n.state = w.TEXT) : a === "]" ? n.cdata += "]" : (n.cdata += "]]" + a, n.state = w.CDATA);
2102
2105
  continue;
2103
2106
  case w.PROC_INST:
2104
2107
  a === "?" ? n.state = w.PROC_INST_ENDING : b(a) ? n.state = w.PROC_INST_BODY : n.procInstName += a;
@@ -2113,7 +2116,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
2113
2116
  name: n.procInstName,
2114
2117
  body: n.procInstBody
2115
2118
  };
2116
- ae(n, e), E(n, "onprocessinginstruction", e), n.procInstName = n.procInstBody = "", n.state = w.TEXT;
2119
+ ie(n, e), D(n, "onprocessinginstruction", e), n.procInstName = n.procInstBody = "", n.state = w.TEXT;
2117
2120
  } else n.procInstBody += "?" + a, n.state = w.PROC_INST_BODY;
2118
2121
  continue;
2119
2122
  case w.OPEN_TAG:
@@ -2132,7 +2135,7 @@ var Xe = /* @__PURE__ */ b(((e, t) => {
2132
2135
  case w.ATTRIB_NAME_SAW_WHITE:
2133
2136
  if (a === "=") n.state = w.ATTRIB_VALUE;
2134
2137
  else if (b(a)) continue;
2135
- else A(n, "Attribute without value"), n.tag.attributes[n.attribName] = "", n.attribValue = "", E(n, "onattribute", {
2138
+ else A(n, "Attribute without value"), n.tag.attributes[n.attribName] = "", n.attribValue = "", D(n, "onattribute", {
2136
2139
  name: n.attribName,
2137
2140
  value: ""
2138
2141
  }), n.attribName = "", a === ">" ? le(n) : C(g, a) ? (n.attribName = a, n.state = w.ATTRIB_NAME) : (A(n, "Invalid attribute name"), n.state = w.ATTRIB);
@@ -2563,33 +2566,33 @@ var St = class {
2563
2566
  this.getContext = e, this.dialecteConfig = t;
2564
2567
  }
2565
2568
  async getRecord(e) {
2566
- return T({
2569
+ return E({
2567
2570
  context: this.getContext(),
2568
2571
  ref: e
2569
2572
  });
2570
2573
  }
2571
2574
  async getRecords(e) {
2572
- return re({
2575
+ return ne({
2573
2576
  context: this.getContext(),
2574
2577
  refs: e
2575
2578
  });
2576
2579
  }
2577
2580
  async getChild(e, t) {
2578
- return ae({
2581
+ return ie({
2579
2582
  context: this.getContext(),
2580
2583
  ref: f(e),
2581
2584
  tagName: t
2582
2585
  });
2583
2586
  }
2584
2587
  async getChildren(e, t) {
2585
- return E({
2588
+ return D({
2586
2589
  context: this.getContext(),
2587
2590
  ref: f(e),
2588
2591
  tagName: t
2589
2592
  });
2590
2593
  }
2591
2594
  async getRecordsByTagName(e) {
2592
- return ie({
2595
+ return re({
2593
2596
  context: this.getContext(),
2594
2597
  tagName: e
2595
2598
  });
@@ -2641,13 +2644,13 @@ var St = class {
2641
2644
  });
2642
2645
  }
2643
2646
  async findAncestors(e) {
2644
- return e ? D({
2647
+ return e ? O({
2645
2648
  context: this.getContext(),
2646
2649
  ref: f(e)
2647
2650
  }) : [];
2648
2651
  }
2649
2652
  async findByAttributes(e) {
2650
- return O({
2653
+ return ae({
2651
2654
  context: this.getContext(),
2652
2655
  tagName: e.tagName,
2653
2656
  attributes: e.attributes
@@ -2676,7 +2679,7 @@ var St = class {
2676
2679
  return this.store.name;
2677
2680
  }
2678
2681
  async getRoot() {
2679
- let e = await T({
2682
+ let e = await E({
2680
2683
  context: this.context,
2681
2684
  ref: { tagName: this.dialecteConfig.rootElementName }
2682
2685
  });
@@ -2686,34 +2689,34 @@ var St = class {
2686
2689
  }), e;
2687
2690
  }
2688
2691
  async getRecord(e) {
2689
- return T({
2692
+ return E({
2690
2693
  context: this.context,
2691
2694
  ref: f(e)
2692
2695
  });
2693
2696
  }
2694
2697
  async getRecords(e) {
2695
2698
  let t = e.map((e) => f(e));
2696
- return re({
2699
+ return ne({
2697
2700
  context: this.context,
2698
2701
  refs: t
2699
2702
  });
2700
2703
  }
2701
2704
  async getChild(e, t) {
2702
- if (e) return ae({
2705
+ if (e) return ie({
2703
2706
  context: this.context,
2704
2707
  ref: f(e),
2705
2708
  tagName: t
2706
2709
  });
2707
2710
  }
2708
2711
  async getChildren(e, t) {
2709
- return e ? E({
2712
+ return e ? D({
2710
2713
  context: this.context,
2711
2714
  ref: f(e),
2712
2715
  tagName: t
2713
2716
  }) : [];
2714
2717
  }
2715
2718
  async getRecordsByTagName(e) {
2716
- return ie({
2719
+ return re({
2717
2720
  context: this.context,
2718
2721
  tagName: e
2719
2722
  });
@@ -2735,7 +2738,7 @@ var St = class {
2735
2738
  });
2736
2739
  }
2737
2740
  async findAncestors(e, t) {
2738
- return e ? D({
2741
+ return e ? O({
2739
2742
  context: this.context,
2740
2743
  ref: f(e),
2741
2744
  options: t
@@ -2780,7 +2783,7 @@ var St = class {
2780
2783
  });
2781
2784
  }
2782
2785
  async findByAttributes(e) {
2783
- return O({
2786
+ return ae({
2784
2787
  context: this.context,
2785
2788
  ...e
2786
2789
  });
@@ -2843,7 +2846,7 @@ function wt(e) {
2843
2846
  //#endregion
2844
2847
  //#region src/document/transaction/create/create.ts
2845
2848
  async function H(e) {
2846
- let { dialecteConfig: t, hooks: n, context: r, query: a, parentRef: o, params: s } = e, { id: c, tagName: u, attributes: d, namespace: f, value: p } = s, m = await T({
2849
+ let { dialecteConfig: t, hooks: n, context: r, query: a, parentRef: o, params: s } = e, { id: c, tagName: u, attributes: d, namespace: f, value: p } = s, m = await E({
2847
2850
  context: r,
2848
2851
  ref: o
2849
2852
  });
@@ -2956,7 +2959,7 @@ async function Et(e) {
2956
2959
  //#endregion
2957
2960
  //#region src/document/transaction/delete/delete.ts
2958
2961
  async function Dt(e) {
2959
- let { hooks: t, context: n, query: r, ref: a } = e, o = await T({
2962
+ let { hooks: t, context: n, query: r, ref: a } = e, o = await E({
2960
2963
  context: n,
2961
2964
  ref: a
2962
2965
  });
@@ -2981,7 +2984,7 @@ async function Dt(e) {
2981
2984
  status: "deleted",
2982
2985
  record: o
2983
2986
  });
2984
- let s = await T({
2987
+ let s = await E({
2985
2988
  context: n,
2986
2989
  ref: f(o.parent)
2987
2990
  });
@@ -3003,7 +3006,7 @@ async function Dt(e) {
3003
3006
  async function Ot(e) {
3004
3007
  let { context: t, record: n } = e;
3005
3008
  for (let e of n.children) {
3006
- let n = await T({
3009
+ let n = await E({
3007
3010
  context: t,
3008
3011
  ref: f(e)
3009
3012
  });
@@ -3020,25 +3023,15 @@ async function Ot(e) {
3020
3023
  //#endregion
3021
3024
  //#region src/document/transaction/ensure/ensure.ts
3022
3025
  async function kt(e) {
3023
- let { dialecteConfig: t, hooks: n, context: r, query: i, parentRef: a, params: o } = e, s = At(o.attributes);
3024
- if (s) {
3025
- let [e] = await O({
3026
- context: r,
3027
- tagName: o.tagName,
3028
- attributes: s
3029
- });
3030
- if (e) return e;
3031
- } else {
3032
- let e = await T({
3033
- context: r,
3034
- ref: {
3035
- tagName: o.tagName,
3036
- id: o.id
3037
- }
3038
- });
3039
- if (e) return e;
3040
- }
3041
- return H({
3026
+ let { dialecteConfig: t, hooks: n, context: r, query: i, parentRef: a, params: o } = e, s = At(o.attributes), c = await D({
3027
+ context: r,
3028
+ ref: a,
3029
+ tagName: o.tagName
3030
+ });
3031
+ return (s ? c.find((e) => k({
3032
+ record: e,
3033
+ attributeFilter: s
3034
+ })) : o.id === void 0 ? c[0] : c.find((e) => e.id === o.id)) || H({
3042
3035
  dialecteConfig: t,
3043
3036
  hooks: n,
3044
3037
  context: r,
@@ -3048,13 +3041,14 @@ async function kt(e) {
3048
3041
  });
3049
3042
  }
3050
3043
  function At(e) {
3044
+ if (!e) return;
3051
3045
  let t = Array.isArray(e) ? Object.fromEntries(e.map((e) => [e.name, e.value])) : e;
3052
3046
  return Object.values(t).some((e) => e !== void 0 && e !== "") ? t : void 0;
3053
3047
  }
3054
3048
  //#endregion
3055
3049
  //#region src/document/transaction/update/update.ts
3056
3050
  async function jt(e) {
3057
- let { dialecteConfig: t, hooks: n, context: r, query: a, ref: o, params: s } = e, { attributes: c, value: d } = s, f = await T({
3051
+ let { dialecteConfig: t, hooks: n, context: r, query: a, ref: o, params: s } = e, { attributes: c, value: d } = s, f = await E({
3058
3052
  context: r,
3059
3053
  ref: o
3060
3054
  });
@@ -3331,15 +3325,7 @@ function Lt(e) {
3331
3325
  //#region src/document/document.ts
3332
3326
  var Rt = class {
3333
3327
  constructor(e, t, n, r, i, a) {
3334
- this.state = {
3335
- loading: !1,
3336
- error: null,
3337
- progress: null,
3338
- history: [],
3339
- lastUpdate: null
3340
- }, this.activeTransactions = 0, this.store = e, this.config = t, this.documentId = n, this.hooks = i, this.extensionsRegistry = r, this.channel = a, this.channel.onmessage = (e) => {
3341
- e.data?.type === "commit" && e.data.documentId === this.documentId && (this.state.lastUpdate = e.data.timestamp ?? Date.now());
3342
- };
3328
+ this.activeTransactions = 0, this.store = e, this.config = t, this.documentId = n, this.hooks = i, this.extensionsRegistry = r, this.state = a.state, this.channelName = a.channelName, this.notify = a.notify;
3343
3329
  }
3344
3330
  withQueryExtensions(e) {
3345
3331
  let t = C(this.extensionsRegistry?.query, e);
@@ -3371,10 +3357,10 @@ var Rt = class {
3371
3357
  let r = this.withAllExtensions(this.createTransaction());
3372
3358
  try {
3373
3359
  let t = await e(r);
3374
- return await r.commit(), this.channel.postMessage({
3360
+ return await r.commit(), this.notify({
3375
3361
  type: "commit",
3376
3362
  documentId: this.documentId,
3377
- timestamp: Date.now()
3363
+ timestamp: this.state.lastUpdate ?? Date.now()
3378
3364
  }), r.clearStagedOperations(), r.clearRecordCache(), r.clearCumulativeCloneMappings(), this.state.history.push({
3379
3365
  method: "commit",
3380
3366
  message: n?.label ?? "Changes committed",
@@ -3414,10 +3400,10 @@ var Rt = class {
3414
3400
  if (!o) {
3415
3401
  o = !0, this.activeTransactions++, this.state.loading = !0;
3416
3402
  try {
3417
- await r.commit(), this.channel.postMessage({
3403
+ await r.commit(), this.notify({
3418
3404
  type: "commit",
3419
3405
  documentId: this.documentId,
3420
- timestamp: Date.now()
3406
+ timestamp: this.state.lastUpdate ?? Date.now()
3421
3407
  }), r.clearStagedOperations(), r.clearRecordCache(), r.clearCumulativeCloneMappings(), this.state.history.push({
3422
3408
  method: "commit",
3423
3409
  message: n?.label ?? "Changes committed",
@@ -3595,43 +3581,43 @@ var Y = {
3595
3581
  return r[e];
3596
3582
  }), te = new Set(w.map(function(e) {
3597
3583
  return r[e];
3598
- })), ne = null;
3599
- function T(e) {
3600
- return ne = /* @__PURE__ */ new WeakMap(), e = function e(t) {
3584
+ })), T = null;
3585
+ function E(e) {
3586
+ return T = /* @__PURE__ */ new WeakMap(), e = function e(t) {
3601
3587
  if (!t || typeof t != "object") return t;
3602
- var n = ne.get(t);
3588
+ var n = T.get(t);
3603
3589
  if (n) return n;
3604
3590
  if (a(t)) {
3605
- n = [], ne.set(t, n);
3591
+ n = [], T.set(t, n);
3606
3592
  for (var r = 0, i = t.length; r < i; ++r) n.push(e(t[r]));
3607
3593
  } else if (te.has(t.constructor)) n = t;
3608
3594
  else {
3609
3595
  var o, c = s(t);
3610
- for (o in n = c === Object.prototype ? {} : Object.create(c), ne.set(t, n), t) l(t, o) && (n[o] = e(t[o]));
3596
+ for (o in n = c === Object.prototype ? {} : Object.create(c), T.set(t, n), t) l(t, o) && (n[o] = e(t[o]));
3611
3597
  }
3612
3598
  return n;
3613
- }(e), ne = null, e;
3599
+ }(e), T = null, e;
3614
3600
  }
3615
- var re = {}.toString;
3616
- function ie(e) {
3617
- return re.call(e).slice(8, -1);
3601
+ var ne = {}.toString;
3602
+ function re(e) {
3603
+ return ne.call(e).slice(8, -1);
3618
3604
  }
3619
- var ae = typeof Symbol < "u" ? Symbol.iterator : "@@iterator", E = typeof ae == "symbol" ? function(e) {
3605
+ var ie = typeof Symbol < "u" ? Symbol.iterator : "@@iterator", D = typeof ie == "symbol" ? function(e) {
3620
3606
  var t;
3621
- return e != null && (t = e[ae]) && t.apply(e);
3607
+ return e != null && (t = e[ie]) && t.apply(e);
3622
3608
  } : function() {
3623
3609
  return null;
3624
3610
  };
3625
- function D(e, t) {
3611
+ function O(e, t) {
3626
3612
  return t = e.indexOf(t), 0 <= t && e.splice(t, 1), 0 <= t;
3627
3613
  }
3628
- var O = {};
3614
+ var ae = {};
3629
3615
  function k(e) {
3630
3616
  var t, n, r, i;
3631
3617
  if (arguments.length === 1) {
3632
3618
  if (a(e)) return e.slice();
3633
- if (this === O && typeof e == "string") return [e];
3634
- if (i = E(e)) {
3619
+ if (this === ae && typeof e == "string") return [e];
3620
+ if (i = D(e)) {
3635
3621
  for (n = []; !(r = i.next()).done;) n.push(r.value);
3636
3622
  return n;
3637
3623
  }
@@ -4183,7 +4169,7 @@ var Y = {
4183
4169
  return typeof e != "string" || /\./.test(e) ? function(e) {
4184
4170
  return e;
4185
4171
  } : function(t) {
4186
- return t[e] === void 0 && e in t && delete (t = T(t))[e], t;
4172
+ return t[e] === void 0 && e in t && delete (t = E(t))[e], t;
4187
4173
  };
4188
4174
  }
4189
4175
  function xt() {
@@ -4214,7 +4200,7 @@ var Y = {
4214
4200
  }
4215
4201
  function St(e) {
4216
4202
  var t = typeof e;
4217
- return t == "object" ? ArrayBuffer.isView(e) ? "binary" : (e = ie(e), e === "ArrayBuffer" ? "binary" : e) : t;
4203
+ return t == "object" ? ArrayBuffer.isView(e) ? "binary" : (e = re(e), e === "ArrayBuffer" ? "binary" : e) : t;
4218
4204
  }
4219
4205
  function Ct(e) {
4220
4206
  return e instanceof Uint8Array ? e : ArrayBuffer.isView(e) ? new Uint8Array(e.buffer, e.byteOffset, e.byteLength) : new Uint8Array(e);
@@ -4919,7 +4905,7 @@ var Y = {
4919
4905
  var g = [], v = [], y = u ? [] : null, b = _ ? m : [];
4920
4906
  if (!_) for (var x = 0; x < l; ++x) {
4921
4907
  var S = h[x], C = {
4922
- value: T(S),
4908
+ value: E(S),
4923
4909
  primKey: t[o + x]
4924
4910
  };
4925
4911
  !1 !== s.call(C, C.value, C) && (C.value == null ? b.push(t[o + x]) : u || B(d(S), d(C.value)) === 0 ? (v.push(C.value), u && y.push(t[o + x])) : (b.push(t[o + x]), g.push(C.value)));
@@ -5131,19 +5117,19 @@ var Y = {
5131
5117
  return e === t[0];
5132
5118
  }, [e], "");
5133
5119
  }, J.prototype.anyOfIgnoreCase = function() {
5134
- var e = k.apply(O, arguments);
5120
+ var e = k.apply(ae, arguments);
5135
5121
  return e.length === 0 ? G(this) : Bt(this, function(e, t) {
5136
5122
  return t.indexOf(e) !== -1;
5137
5123
  }, e, "");
5138
5124
  }, J.prototype.startsWithAnyOfIgnoreCase = function() {
5139
- var e = k.apply(O, arguments);
5125
+ var e = k.apply(ae, arguments);
5140
5126
  return e.length === 0 ? G(this) : Bt(this, function(e, t) {
5141
5127
  return t.some(function(t) {
5142
5128
  return e.indexOf(t) === 0;
5143
5129
  });
5144
5130
  }, e, dt);
5145
5131
  }, J.prototype.anyOf = function() {
5146
- var e = this, t = k.apply(O, arguments), n = this._cmp;
5132
+ var e = this, t = k.apply(ae, arguments), n = this._cmp;
5147
5133
  try {
5148
5134
  t.sort(n);
5149
5135
  } catch {
@@ -5169,7 +5155,7 @@ var Y = {
5169
5155
  includeUppers: !1
5170
5156
  });
5171
5157
  }, J.prototype.noneOf = function() {
5172
- var e = k.apply(O, arguments);
5158
+ var e = k.apply(ae, arguments);
5173
5159
  if (e.length === 0) return new this.Collection(this);
5174
5160
  try {
5175
5161
  e.sort(this._ascending);
@@ -5227,7 +5213,7 @@ var Y = {
5227
5213
  }), !1);
5228
5214
  }), e;
5229
5215
  }, J.prototype.startsWithAnyOf = function() {
5230
- var e = k.apply(O, arguments);
5216
+ var e = k.apply(ae, arguments);
5231
5217
  return e.every(function(e) {
5232
5218
  return typeof e == "string";
5233
5219
  }) ? e.length === 0 ? G(this) : this.inAnyRange(e.map(function(e) {
@@ -5969,7 +5955,7 @@ var Y = {
5969
5955
  var t = Tn(this).next(e).value;
5970
5956
  return t && B(t.from, e) <= 0 && 0 <= B(t.to, e);
5971
5957
  }
5972
- })[ae] = function() {
5958
+ })[ie] = function() {
5973
5959
  return Tn(this);
5974
5960
  }, j));
5975
5961
  var An = {}, jn = {}, Mn = !1;
@@ -6195,7 +6181,7 @@ var Y = {
6195
6181
  function Bn(e, t, n, r) {
6196
6182
  return n ||= {}, r ||= "", i(e).forEach(function(i) {
6197
6183
  var a, o, s;
6198
- l(t, i) ? (a = e[i], o = t[i], typeof a == "object" && typeof o == "object" && a && o ? (s = ie(a)) === ie(o) ? s === "Object" ? Bn(a, o, n, r + i + ".") : a !== o && (n[r + i] = t[i]) : n[r + i] = t[i] : a !== o && (n[r + i] = t[i])) : n[r + i] = void 0;
6184
+ l(t, i) ? (a = e[i], o = t[i], typeof a == "object" && typeof o == "object" && a && o ? (s = re(a)) === re(o) ? s === "Object" ? Bn(a, o, n, r + i + ".") : a !== o && (n[r + i] = t[i]) : n[r + i] = t[i] : a !== o && (n[r + i] = t[i])) : n[r + i] = void 0;
6199
6185
  }), i(t).forEach(function(i) {
6200
6186
  l(e, i) || (n[r + i] = t[i]);
6201
6187
  }), n;
@@ -6303,7 +6289,7 @@ var Y = {
6303
6289
  function Un(e, t, n) {
6304
6290
  try {
6305
6291
  if (!t || t.keys.length < e.length) return null;
6306
- for (var r = [], i = 0, a = 0; i < t.keys.length && a < e.length; ++i) B(t.keys[i], e[a]) === 0 && (r.push(n ? T(t.values[i]) : t.values[i]), ++a);
6292
+ for (var r = [], i = 0, a = 0; i < t.keys.length && a < e.length; ++i) B(t.keys[i], e[a]) === 0 && (r.push(n ? E(t.values[i]) : t.values[i]), ++a);
6307
6293
  return r.length === e.length ? r : null;
6308
6294
  } catch {
6309
6295
  return null;
@@ -6322,7 +6308,7 @@ var Y = {
6322
6308
  return t ? I.resolve(t) : r.getMany(e).then(function(t) {
6323
6309
  return e.trans._cache = {
6324
6310
  keys: e.keys,
6325
- values: e.cache === "clone" ? T(t) : t
6311
+ values: e.cache === "clone" ? E(t) : t
6326
6312
  }, t;
6327
6313
  });
6328
6314
  },
@@ -6546,7 +6532,7 @@ var Y = {
6546
6532
  e.subscribers.add(n), r.addEventListener("abort", function() {
6547
6533
  var r, i;
6548
6534
  e.subscribers.delete(n), e.subscribers.size === 0 && (r = e, i = t, setTimeout(function() {
6549
- r.subscribers.size === 0 && D(i, r);
6535
+ r.subscribers.size === 0 && O(i, r);
6550
6536
  }, 3e3));
6551
6537
  });
6552
6538
  }
@@ -6568,16 +6554,16 @@ var Y = {
6568
6554
  var f = e.table(u), p = d.optimisticOps.filter(function(e) {
6569
6555
  return e.trans === s;
6570
6556
  });
6571
- if (s._explicit && i && s.mutatedParts) for (var m = 0, h = Object.values(d.queries.query); m < h.length; m++) for (var g = 0, _ = (b = h[m]).slice(); g < _.length; g++) kn((x = _[g]).obsSet, s.mutatedParts) && (D(b, x), x.subscribers.forEach(function(e) {
6557
+ if (s._explicit && i && s.mutatedParts) for (var m = 0, h = Object.values(d.queries.query); m < h.length; m++) for (var g = 0, _ = (b = h[m]).slice(); g < _.length; g++) kn((x = _[g]).obsSet, s.mutatedParts) && (O(b, x), x.subscribers.forEach(function(e) {
6572
6558
  return o.add(e);
6573
6559
  }));
6574
6560
  else if (0 < p.length) {
6575
6561
  d.optimisticOps = d.optimisticOps.filter(function(e) {
6576
6562
  return e.trans !== s;
6577
6563
  });
6578
- for (var v = 0, y = Object.values(d.queries.query); v < y.length; v++) for (var b, x, S, C = 0, ee = (b = y[v]).slice(); C < ee.length; C++) (x = ee[C]).res != null && s.mutatedParts && (i && !x.dirty ? (S = Object.isFrozen(x.res), S = Xn(x.res, x.req, p, f, x, S), x.dirty ? (D(b, x), x.subscribers.forEach(function(e) {
6564
+ for (var v = 0, y = Object.values(d.queries.query); v < y.length; v++) for (var b, x, S, C = 0, ee = (b = y[v]).slice(); C < ee.length; C++) (x = ee[C]).res != null && s.mutatedParts && (i && !x.dirty ? (S = Object.isFrozen(x.res), S = Xn(x.res, x.req, p, f, x, S), x.dirty ? (O(b, x), x.subscribers.forEach(function(e) {
6579
6565
  return o.add(e);
6580
- })) : S !== x.res && (x.res = S, x.promise = I.resolve({ result: S }))) : (x.dirty && D(b, x), x.subscribers.forEach(function(e) {
6566
+ })) : S !== x.res && (x.res = S, x.promise = I.resolve({ result: S }))) : (x.dirty && O(b, x), x.subscribers.forEach(function(e) {
6581
6567
  return o.add(e);
6582
6568
  })));
6583
6569
  }
@@ -6600,13 +6586,13 @@ var Y = {
6600
6586
  return s ? (o = i.mutate(e), e.type !== "add" && e.type !== "put" || !(50 <= e.values.length || Vn(a, e).some(function(e) {
6601
6587
  return e == null;
6602
6588
  })) ? (s.optimisticOps.push(e), e.mutatedParts && Nn(e.mutatedParts), o.then(function(t) {
6603
- 0 < t.numFailures && (D(s.optimisticOps, e), (t = Jn(0, e, t)) && s.optimisticOps.push(t), e.mutatedParts && Nn(e.mutatedParts));
6589
+ 0 < t.numFailures && (O(s.optimisticOps, e), (t = Jn(0, e, t)) && s.optimisticOps.push(t), e.mutatedParts && Nn(e.mutatedParts));
6604
6590
  }), o.catch(function() {
6605
- D(s.optimisticOps, e), e.mutatedParts && Nn(e.mutatedParts);
6591
+ O(s.optimisticOps, e), e.mutatedParts && Nn(e.mutatedParts);
6606
6592
  })) : o.then(function(n) {
6607
6593
  var r = Jn(0, t(t({}, e), { values: e.values.map(function(e, r) {
6608
6594
  var i;
6609
- return n.failures[r] ? e : (e = (i = a.keyPath) != null && i.includes(".") ? T(e) : t({}, e), x(e, a.keyPath, n.results[r]), e);
6595
+ return n.failures[r] ? e : (e = (i = a.keyPath) != null && i.includes(".") ? E(e) : t({}, e), x(e, a.keyPath, n.results[r]), e);
6610
6596
  }) }), n);
6611
6597
  s.optimisticOps.push(r), queueMicrotask(function() {
6612
6598
  return e.mutatedParts && Nn(e.mutatedParts);
@@ -6664,10 +6650,10 @@ var Y = {
6664
6650
  if (l && (l.res = n), t) {
6665
6651
  for (var r = 0, i = n.length; r < i; ++r) Object.freeze(n[r]);
6666
6652
  Object.freeze(n);
6667
- } else e.result = T(n);
6653
+ } else e.result = E(n);
6668
6654
  return e;
6669
6655
  }).catch(function(e) {
6670
- return d && l && D(d, l), Promise.reject(e);
6656
+ return d && l && O(d, l), Promise.reject(e);
6671
6657
  }), l = {
6672
6658
  obsSet: e.obsSet,
6673
6659
  promise: a,
@@ -7184,7 +7170,7 @@ var Y = {
7184
7170
  });
7185
7171
  },
7186
7172
  shallowClone: S,
7187
- deepClone: T,
7173
+ deepClone: E,
7188
7174
  getObjectDiff: Bn,
7189
7175
  cmp: B,
7190
7176
  asap: y,
@@ -7439,6 +7425,16 @@ var on = {
7439
7425
  i.length > 0 && await r.bulkAdd(i), a.length > 0 && await r.bulkPut(a.map((e) => e.after)), o.length > 0 && await r.bulkDelete(o.map((e) => e.id)), await this.setHead(e, t);
7440
7426
  });
7441
7427
  }
7428
+ async getHistoryStatus(e) {
7429
+ let t = await this.getHead(e), n = await this.db.table(W).where({
7430
+ documentId: e,
7431
+ sequenceNumber: t + 1
7432
+ }).first();
7433
+ return {
7434
+ canUndo: t > 0,
7435
+ canRedo: n !== void 0
7436
+ };
7437
+ }
7442
7438
  async getChangeLog(e) {
7443
7439
  return this.db.table(W).where({ documentId: e }).sortBy("sequenceNumber");
7444
7440
  }
@@ -7653,6 +7649,13 @@ var un = class {
7653
7649
  for (let e of o) r.delete(e.id);
7654
7650
  this.heads.set(e, t);
7655
7651
  }
7652
+ async getHistoryStatus(e) {
7653
+ let t = this.heads.get(e) ?? 0, n = this.changelog.get(e) ?? [];
7654
+ return {
7655
+ canUndo: t > 0,
7656
+ canRedo: n.some((e) => e.sequenceNumber === t + 1)
7657
+ };
7658
+ }
7656
7659
  async getChangeLog(e) {
7657
7660
  return this.changelog.get(e) ?? [];
7658
7661
  }
@@ -7862,11 +7865,20 @@ var vn = class {
7862
7865
  detail: "Call project.open(name) before accessing project properties."
7863
7866
  }), this._store;
7864
7867
  }
7865
- get channel() {
7866
- return i(this._channel !== void 0, {
7868
+ get postChannel() {
7869
+ return i(this._postChannel !== void 0, {
7867
7870
  key: "PROJECT_NOT_OPENED",
7868
7871
  detail: "Call project.open(name) before accessing project properties."
7869
- }), this._channel;
7872
+ }), this._postChannel;
7873
+ }
7874
+ get channelName() {
7875
+ return `dialecte::project::${this.name}`;
7876
+ }
7877
+ createChannel() {
7878
+ return new BroadcastChannel(this.channelName);
7879
+ }
7880
+ notify(e) {
7881
+ this.postChannel.postMessage(e);
7870
7882
  }
7871
7883
  constructor(e) {
7872
7884
  this.state = {
@@ -7880,21 +7892,40 @@ var vn = class {
7880
7892
  }) : void 0;
7881
7893
  }
7882
7894
  async open(e) {
7883
- this._name = e, this._channel = new BroadcastChannel(`dialecte::project::${e}`), this._channel.onmessage = (e) => {
7884
- let { type: t } = e.data ?? {};
7885
- (t === "init-empty-document" || t === "document-removed" || t === "document-imported") && this.refreshState();
7886
- };
7895
+ this._name = e, this._postChannel = new BroadcastChannel(this.channelName), this._listenChannel = new BroadcastChannel(this.channelName), this._listenChannel.addEventListener("message", (e) => {
7896
+ this.onChannelMessage(e.data);
7897
+ });
7887
7898
  let t = dn(e, this.storage, this.configs[this.defaultConfigKey]);
7888
7899
  await t.open(), this._store = t;
7889
7900
  let n = await t.getDocuments();
7890
7901
  for (let e of n) this.state.documents.set(e.id, mn(e));
7891
- return this;
7902
+ return await Promise.all(n.map((e) => this.refreshHistoryStatus(e.id))), this;
7903
+ }
7904
+ onChannelMessage(e) {
7905
+ switch (e?.type) {
7906
+ case "init-empty-document":
7907
+ case "document-removed":
7908
+ case "document-imported":
7909
+ this.refreshState().catch(() => {});
7910
+ break;
7911
+ case "commit": {
7912
+ let t = this.state.documents.get(e.documentId);
7913
+ t && (t.lastUpdate = e.timestamp ?? Date.now()), this.refreshHistoryStatus(e.documentId).catch(() => {});
7914
+ break;
7915
+ }
7916
+ }
7917
+ }
7918
+ async refreshHistoryStatus(e) {
7919
+ let t = this.state.documents.get(e);
7920
+ if (!t) return;
7921
+ let { canUndo: n, canRedo: r } = await this.store.getHistoryStatus(e);
7922
+ t.canUndo = n, t.canRedo = r;
7892
7923
  }
7893
7924
  close() {
7894
- this.channel.close(), this.store.close();
7925
+ this.postChannel.close(), this._listenChannel?.close(), this.store.close();
7895
7926
  }
7896
7927
  async destroy() {
7897
- this.channel.close(), await this.store.destroy(), this.state.documents.clear();
7928
+ this.postChannel.close(), this._listenChannel?.close(), await this.store.destroy(), this.state.documents.clear();
7898
7929
  }
7899
7930
  async initEmptyDocument(e) {
7900
7931
  let t = await _n({
@@ -7904,15 +7935,17 @@ var vn = class {
7904
7935
  options: e,
7905
7936
  hooks: this.hooks
7906
7937
  });
7907
- return this.state.documents.set(t.documentId, t.documentState), this.channel.postMessage({
7938
+ return this.state.documents.set(t.documentId, t.documentState), this.notify({
7908
7939
  type: "init-empty-document",
7909
- documentId: t.documentId
7940
+ documentId: t.documentId,
7941
+ timestamp: Date.now()
7910
7942
  }), t.documentId;
7911
7943
  }
7912
7944
  async removeDocument(e) {
7913
- await this.store.removeDocument(e), this.state.documents.delete(e), this.channel.postMessage({
7945
+ await this.store.removeDocument(e), this.state.documents.delete(e), this.notify({
7914
7946
  type: "document-removed",
7915
- documentId: e
7947
+ documentId: e,
7948
+ timestamp: Date.now()
7916
7949
  });
7917
7950
  }
7918
7951
  async import(e, t) {
@@ -7924,9 +7957,10 @@ var vn = class {
7924
7957
  options: t,
7925
7958
  hooks: this.hooks
7926
7959
  })));
7927
- for (let e of n) this.state.documents.set(e.documentId, e.documentState), this.channel.postMessage({
7960
+ for (let e of n) this.state.documents.set(e.documentId, e.documentState), this.notify({
7928
7961
  type: "document-imported",
7929
- documentId: e.documentId
7962
+ documentId: e.documentId,
7963
+ timestamp: Date.now()
7930
7964
  });
7931
7965
  return n.map(({ documentId: e, recordCount: t }) => ({
7932
7966
  documentId: e,
@@ -7956,30 +7990,40 @@ var vn = class {
7956
7990
  detail: `Document "${e}" not registered in project "${this.name}"`
7957
7991
  });
7958
7992
  let n = this.configs[t.record.configKey];
7959
- return new Rt(this.store, n, e, this.mergedExtensions, this.hooks, this.channel);
7993
+ return new Rt(this.store, n, e, this.mergedExtensions, this.hooks, {
7994
+ state: t,
7995
+ channelName: this.channelName,
7996
+ notify: (e) => this.notify(e)
7997
+ });
7960
7998
  }
7961
7999
  getDocumentConfig(e) {
7962
8000
  let t = this.state.documents.get(e);
7963
8001
  if (t) return this.configs[t.record.configKey];
7964
8002
  }
7965
8003
  async undo(e) {
7966
- i(this.state.documents.get(e), {
8004
+ let t = this.state.documents.get(e);
8005
+ i(t, {
7967
8006
  key: "DOCUMENT_NOT_REGISTERED",
7968
8007
  detail: `Document "${e}" not registered in project "${this.name}"`
7969
- }), await this.store.undo(e), this.channel.postMessage({
8008
+ }), await this.store.undo(e);
8009
+ let n = Date.now();
8010
+ t.lastUpdate = n, await this.refreshHistoryStatus(e), this.notify({
7970
8011
  type: "commit",
7971
8012
  documentId: e,
7972
- timestamp: Date.now()
8013
+ timestamp: n
7973
8014
  });
7974
8015
  }
7975
8016
  async redo(e) {
7976
- i(this.state.documents.get(e), {
8017
+ let t = this.state.documents.get(e);
8018
+ i(t, {
7977
8019
  key: "DOCUMENT_NOT_REGISTERED",
7978
8020
  detail: `Document "${e}" not registered in project "${this.name}"`
7979
- }), await this.store.redo(e), this.channel.postMessage({
8021
+ }), await this.store.redo(e);
8022
+ let n = Date.now();
8023
+ t.lastUpdate = n, await this.refreshHistoryStatus(e), this.notify({
7980
8024
  type: "commit",
7981
8025
  documentId: e,
7982
- timestamp: Date.now()
8026
+ timestamp: n
7983
8027
  });
7984
8028
  }
7985
8029
  async addBlob(e, t, n = []) {
@@ -7992,10 +8036,11 @@ var vn = class {
7992
8036
  createdAt: Date.now(),
7993
8037
  attachedTo: n
7994
8038
  };
7995
- return await this.store.addBlob(r, t), this.channel.postMessage({
8039
+ return await this.store.addBlob(r, t), this.notify({
7996
8040
  type: "blob-added",
7997
8041
  blobId: r.id,
7998
- documentId: e
8042
+ documentId: e,
8043
+ timestamp: Date.now()
7999
8044
  }), r.id;
8000
8045
  }
8001
8046
  async getBlob(e) {
@@ -8018,23 +8063,26 @@ var vn = class {
8018
8063
  return this.store.getStandaloneBlobs();
8019
8064
  }
8020
8065
  async attachBlob(e, t) {
8021
- await this.store.attachBlob(e, t), this.channel.postMessage({
8066
+ await this.store.attachBlob(e, t), this.notify({
8022
8067
  type: "blob-attached",
8023
8068
  blobId: e,
8024
- ref: t
8069
+ ref: t,
8070
+ timestamp: Date.now()
8025
8071
  });
8026
8072
  }
8027
8073
  async detachBlob(e, t) {
8028
- await this.store.detachBlob(e, t), this.channel.postMessage({
8074
+ await this.store.detachBlob(e, t), this.notify({
8029
8075
  type: "blob-detached",
8030
8076
  blobId: e,
8031
- ref: t
8077
+ ref: t,
8078
+ timestamp: Date.now()
8032
8079
  });
8033
8080
  }
8034
8081
  async removeBlob(e) {
8035
- await this.store.removeBlob(e), this.channel.postMessage({
8082
+ await this.store.removeBlob(e), this.notify({
8036
8083
  type: "blob-removed",
8037
- blobId: e
8084
+ blobId: e,
8085
+ timestamp: Date.now()
8038
8086
  });
8039
8087
  }
8040
8088
  async queryFirst(e) {
@@ -8060,4 +8108,4 @@ var vn = class {
8060
8108
  }
8061
8109
  };
8062
8110
  //#endregion
8063
- export { oe as $, bt as A, Ie as B, K as C, Pt as D, U as E, He as F, xe as G, we as H, Ve as I, ye as J, be as K, Be as L, et as M, Qe as N, Ct as O, Ue as P, ge as Q, ze as R, J as S, Lt as T, P as U, Pe as V, Ce as W, ve as X, _e as Y, N as Z, Vt as _, hn as a, ie as at, G as b, dn as c, ne as ct, Wt as d, S as dt, O as et, Gt as f, q as g, Ut as h, mn as i, ae as it, ht as j, St as k, un as l, te as lt, Y as m, _n as n, D as nt, pn as o, re as ot, Ht as p, Se as q, gn as r, E as rt, fn as s, T as st, vn as t, k as tt, sn as u, C as ut, W as v, Rt as w, Bt as x, zt as y, Re as z };
8111
+ export { oe as $, bt as A, Ie as B, K as C, Pt as D, U as E, He as F, xe as G, we as H, Ve as I, ye as J, be as K, Be as L, et as M, Qe as N, Ct as O, Ue as P, ge as Q, ze as R, J as S, Lt as T, P as U, Pe as V, Ce as W, ve as X, _e as Y, N as Z, Vt as _, hn as a, re as at, G as b, dn as c, T as ct, Wt as d, S as dt, ae as et, Gt as f, q as g, Ut as h, mn as i, ie as it, ht as j, St as k, un as l, te as lt, Y as m, _n as n, O as nt, pn as o, ne as ot, Ht as p, Se as q, gn as r, D as rt, fn as s, E as st, vn as t, k as tt, sn as u, C as ut, W as v, Rt as w, Bt as x, zt as y, Re as z };