tinybase 1.0.4 → 1.0.5

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.
@@ -110,6 +110,7 @@ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
110
110
  const storeListenerIds = mapNew();
111
111
  const getStore = () => store;
112
112
  const getThingIds = () => mapKeys(tableIds);
113
+ const forEachThing = (cb) => mapForEach(things, cb);
113
114
  const hasThing = (id) => collHas(things, id);
114
115
  const getTableId = (id) => mapGet(tableIds, id);
115
116
  const getThing = (id) => mapGet(things, id);
@@ -200,6 +201,7 @@ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
200
201
  return [
201
202
  getStore,
202
203
  getThingIds,
204
+ forEachThing,
203
205
  hasThing,
204
206
  getTableId,
205
207
  getThing,
@@ -439,6 +441,8 @@ const createCheckpoints = getCreateFunction((store) => {
439
441
  };
440
442
  const getStore = () => store;
441
443
  const getCheckpointIds = () => [[...backwardIds], currentId, [...forwardIds]];
444
+ const forEachCheckpoint = (checkpointCallback) =>
445
+ mapForEach(labels, checkpointCallback);
442
446
  const hasCheckpoint = (checkpointId) => collHas(deltas, checkpointId);
443
447
  const getCheckpoint = (checkpointId) => mapGet(labels, checkpointId);
444
448
  const goBackward = () => {
@@ -495,6 +499,7 @@ const createCheckpoints = getCreateFunction((store) => {
495
499
  setCheckpoint,
496
500
  getStore,
497
501
  getCheckpointIds,
502
+ forEachCheckpoint,
498
503
  hasCheckpoint,
499
504
  getCheckpoint,
500
505
  goBackward,
@@ -518,6 +523,7 @@ const createIndexes = getCreateFunction((store) => {
518
523
  const [
519
524
  getStore,
520
525
  getIndexIds,
526
+ forEachIndexImpl,
521
527
  hasIndex,
522
528
  getTableId,
523
529
  getIndex,
@@ -627,6 +633,26 @@ const createIndexes = getCreateFunction((store) => {
627
633
  );
628
634
  return indexes;
629
635
  };
636
+ const forEachIndex = (indexCallback) =>
637
+ forEachIndexImpl((indexId, slices) =>
638
+ indexCallback(indexId, (sliceCallback) =>
639
+ forEachSliceImpl(indexId, sliceCallback, slices),
640
+ ),
641
+ );
642
+ const forEachSlice = (indexId, sliceCallback) =>
643
+ forEachSliceImpl(indexId, sliceCallback, getIndex(indexId));
644
+ const forEachSliceImpl = (indexId, sliceCallback, slices) => {
645
+ const tableId = getTableId(indexId);
646
+ collForEach(slices, (rowIds, sliceId) =>
647
+ sliceCallback(sliceId, (rowCallback) =>
648
+ collForEach(rowIds, (rowId) =>
649
+ rowCallback(rowId, (cellCallback) =>
650
+ store.forEachCell(tableId, rowId, cellCallback),
651
+ ),
652
+ ),
653
+ ),
654
+ );
655
+ };
630
656
  const delIndexDefinition = (indexId) => {
631
657
  delDefinition(indexId);
632
658
  return indexes;
@@ -651,6 +677,8 @@ const createIndexes = getCreateFunction((store) => {
651
677
  delIndexDefinition,
652
678
  getStore,
653
679
  getIndexIds,
680
+ forEachIndex,
681
+ forEachSlice,
654
682
  hasIndex,
655
683
  hasSlice,
656
684
  getTableId,
@@ -710,6 +738,7 @@ const createMetrics = getCreateFunction((store) => {
710
738
  const [
711
739
  getStore,
712
740
  getMetricIds,
741
+ forEachMetric,
713
742
  hasMetric,
714
743
  getTableId,
715
744
  getMetric,
@@ -795,6 +824,7 @@ const createMetrics = getCreateFunction((store) => {
795
824
  delMetricDefinition,
796
825
  getStore,
797
826
  getMetricIds,
827
+ forEachMetric,
798
828
  hasMetric,
799
829
  getTableId,
800
830
  getMetric,
@@ -987,6 +1017,7 @@ const createRelationships = getCreateFunction((store) => {
987
1017
  const [
988
1018
  getStore,
989
1019
  getRelationshipIds,
1020
+ forEachRelationshipImpl,
990
1021
  hasRelationship,
991
1022
  getLocalTableId,
992
1023
  getRelationship,
@@ -1101,6 +1132,12 @@ const createRelationships = getCreateFunction((store) => {
1101
1132
  );
1102
1133
  return relationships;
1103
1134
  };
1135
+ const forEachRelationship = (relationshipCallback) =>
1136
+ forEachRelationshipImpl((relationshipId) =>
1137
+ relationshipCallback(relationshipId, (rowCallback) =>
1138
+ store.forEachRow(getLocalTableId(relationshipId), rowCallback),
1139
+ ),
1140
+ );
1104
1141
  const delRelationshipDefinition = (relationshipId) => {
1105
1142
  mapSet(remoteTableIds, relationshipId);
1106
1143
  delDefinition(relationshipId);
@@ -1141,6 +1178,7 @@ const createRelationships = getCreateFunction((store) => {
1141
1178
  delRelationshipDefinition,
1142
1179
  getStore,
1143
1180
  getRelationshipIds,
1181
+ forEachRelationship,
1144
1182
  hasRelationship,
1145
1183
  getLocalTableId,
1146
1184
  getRemoteTableId,
package/lib/indexes.d.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  * @module indexes
12
12
  */
13
13
 
14
- import {GetCell, Store} from './store.d';
14
+ import {GetCell, RowCallback, Store} from './store.d';
15
15
  import {Id, IdOrNull, Ids, SortKey} from './common.d';
16
16
 
17
17
  /**
@@ -43,6 +43,42 @@ export type Index = {[sliceId: Id]: Slice};
43
43
  */
44
44
  export type Slice = Ids;
45
45
 
46
+ /**
47
+ * The IndexCallback type describes a function that takes an Index's Id and a
48
+ * callback to loop over each Slice within it.
49
+ *
50
+ * A IndexCallback is provided when using the forEachIndex method, so that you
51
+ * can do something based on every Index in the Indexes object. See that method
52
+ * for specific examples.
53
+ *
54
+ * @param indexId The Id of the Index that the callback can operate on.
55
+ * @param forEachRow A function that will let you iterate over the Slice objects
56
+ * in this Index.
57
+ * @category Callback
58
+ */
59
+ export type IndexCallback = (
60
+ indexId: Id,
61
+ forEachSlice: (sliceCallback: SliceCallback) => void,
62
+ ) => void;
63
+
64
+ /**
65
+ * The SliceCallback type describes a function that takes a Slice's Id and a
66
+ * callback to loop over each Row within it.
67
+ *
68
+ * A SliceCallback is provided when using the forEachSlice method, so that you
69
+ * can do something based on every Slice in an Index. See that method for
70
+ * specific examples.
71
+ *
72
+ * @param sliceId The Id of the Slice that the callback can operate on.
73
+ * @param forEachRow A function that will let you iterate over the Row objects
74
+ * in this Slice.
75
+ * @category Callback
76
+ */
77
+ export type SliceCallback = (
78
+ sliceId: Id,
79
+ forEachRow: (rowCallback: RowCallback) => void,
80
+ ) => void;
81
+
46
82
  /**
47
83
  * The SliceIdsListener type describes a function that is used to listen to
48
84
  * changes to the Slice Ids in an Index.
@@ -362,6 +398,83 @@ export interface Indexes {
362
398
  */
363
399
  getIndexIds(): Ids;
364
400
 
401
+ /**
402
+ * The forEachIndex method takes a function that it will then call for each
403
+ * Index in a specified Indexes object.
404
+ *
405
+ * This method is useful for iterating over the structure of the Indexes
406
+ * object in a functional style. The `indexCallback` parameter is a
407
+ * IndexCallback function that will be called with the Id of each Index, and
408
+ * with a function that can then be used to iterate over each Slice of the
409
+ * Index, should you wish.
410
+ *
411
+ * @param indexCallback The function that should be called for every Index.
412
+ * @example
413
+ * This example iterates over each Index in an Indexes object, and lists each
414
+ * Slice Id within them.
415
+ *
416
+ * ```js
417
+ * const store = createStore().setTable('pets', {
418
+ * fido: {species: 'dog', color: 'brown'},
419
+ * felix: {species: 'cat', color: 'black'},
420
+ * cujo: {species: 'dog', color: 'black'},
421
+ * });
422
+ * const indexes = createIndexes(store)
423
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
424
+ * .setIndexDefinition('byColor', 'pets', 'color');
425
+ *
426
+ * indexes.forEachIndex((indexId, forEachSlice) => {
427
+ * console.log(indexId);
428
+ * forEachSlice((sliceId) => console.log(`- ${sliceId}`));
429
+ * });
430
+ * // -> 'bySpecies'
431
+ * // -> '- dog'
432
+ * // -> '- cat'
433
+ * // -> 'byColor'
434
+ * // -> '- brown'
435
+ * // -> '- black'
436
+ * ```
437
+ * @category Iterator
438
+ */
439
+ forEachIndex(indexCallback: IndexCallback): void;
440
+
441
+ /**
442
+ * The forEachSlice method takes a function that it will then call for each
443
+ * Slice in a specified Index.
444
+ *
445
+ * This method is useful for iterating over the Slice structure of the Index
446
+ * in a functional style. The `rowCallback` parameter is a RowCallback
447
+ * function that will be called with the Id and value of each Row in the
448
+ * Slice.
449
+ *
450
+ * @param indexId The Id of the Index to iterate over.
451
+ * @param sliceCallback The function that should be called for every Slice.
452
+ * @example
453
+ * This example iterates over each Row in a Slice, and lists its Id.
454
+ *
455
+ * ```js
456
+ * const store = createStore().setTable('pets', {
457
+ * fido: {species: 'dog'},
458
+ * felix: {species: 'cat'},
459
+ * cujo: {species: 'dog'},
460
+ * });
461
+ * const indexes = createIndexes(store);
462
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
463
+ *
464
+ * indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => {
465
+ * console.log(sliceId);
466
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
467
+ * });
468
+ * // -> 'dog'
469
+ * // -> '- fido'
470
+ * // -> '- cujo'
471
+ * // -> 'cat'
472
+ * // -> '- felix'
473
+ * ```
474
+ * @category Iterator
475
+ */
476
+ forEachSlice(indexId: Id, sliceCallback: SliceCallback): void;
477
+
365
478
  /**
366
479
  * The hasIndex method returns a boolean indicating whether a given Index
367
480
  * exists in the Indexes object.
package/lib/indexes.js CHANGED
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),s=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),n=(e,t)=>e.sort(t),o=(e,t)=>e.forEach(t),r=e=>e.length,d=e=>0==r(e),c=e=>e.slice(1),l=e=>null==e,i=(e,t,s)=>l(e)?s?.():t(e),a=(e,t)=>e?.has(t)??!1,u=e=>l(e)||0==(e=>e.size)(e),g=e=>e.clear(),I=(e,t)=>e?.forEach(t),h=(e,t)=>e?.delete(t),f=e=>new Map(e),w=e=>[...e?.keys()??[]],S=(e,t)=>e?.get(t),p=(e,t)=>I(e,((e,s)=>t(s,e))),L=(e,t,s)=>l(s)?(h(e,t),e):e?.set(t,s),v=(e,t,s,n)=>(a(e,t)||(n?.(s),e.set(t,s)),S(e,t)),x=e=>new Set(e),R=(e,t)=>e?.add(t),b=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),y=(e,t)=>e<t?-1:1,T=(e,t,s)=>r(s)<2?R(d(s)?e:v(e,s[0],x()),t):T(v(e,s[0],f()),t,c(s)),k=e=>{const t=(s,n,...r)=>i(s,(s=>d(r)?e(s,n):o([r[0],null],(e=>t(S(s,e),n,...c(r))))));return t},z=Object.freeze,D=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((e=>{const t=f(),d=f(),[c,v,D,E,M,j,C,O,W]=((e,t,s)=>{const n=e.hasRow,r=f(),d=f(),c=f(),u=f(),h=f(),v=t=>i(S(h,t),(s=>{I(s,e.delListener),L(h,t)})),R=e=>{L(r,e),L(d,e),L(c,e),L(u,e),v(e)};return[()=>e,()=>w(r),e=>a(d,e),e=>S(r,e),e=>S(d,e),(e,t)=>L(d,e,t),(i,w,R,b,y)=>{const T=f(),k=f();L(r,i,w),a(d,i)||(L(d,i,t()),L(c,i,f()),L(u,i,f()));const z=S(c,i),D=S(u,i),E=t=>{const o=s=>e.getCell(w,t,s),r=S(z,t),d=n(w,t)?s(b(o,t)):void 0;if(r!=d&&L(T,t,[r,d]),!l(y)){const e=S(D,t),s=n(w,t)?y(o,t):void 0;e!=s&&L(k,t,s)}},M=e=>{R((()=>{I(T,(([,e],t)=>L(z,t,e))),I(k,((e,t)=>L(D,t,e)))}),T,k,z,D,e),g(T),g(k)};p(z,E),e.hasTable(w)&&o(e.getRowIds(w),(e=>{a(z,e)||E(e)})),M(!0),v(i),L(h,i,x([e.addRowListener(w,null,((e,t,s)=>E(s))),e.addTableListener(w,(()=>M()))]))},R,()=>p(h,R)]})(e,f,(e=>l(e)?"":e+"")),[m,q,A]=(e=>{let t,s=0;const n=[],d=f();return[(o,r,c=[])=>{t??=e();const l=n.pop()??""+s++;return L(d,l,[o,r,c]),T(r,l,c),l},(e,s=[],...n)=>k(I)(e,(e=>i(S(d,e),(([e])=>e(t,...s,...n)))),...s),e=>i(S(d,e),(([,t,s])=>(k(h)(t,e,...s),L(d,e),r(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>i(S(d,e),(([e,,d])=>{const c=(...i)=>{const a=r(i);a==r(d)?e(t,...i,...n(i)):l(d[a])?o(s[a](...i),(e=>c(...i,e))):c(...i,d[a])};c()}))]})((()=>B)),B={setIndexDefinition:(e,o,r,c,g,w=y)=>{const v=l(g)?void 0:([e],[t])=>g(e,t);return C(e,o,((o,r,g,b,y,T)=>{let k=0;const z=x(),D=x(),E=M(e);if(I(r,(([e,t],s)=>{l(e)||(R(z,e),i(S(E,e),(t=>{h(t,s),u(t)&&(L(E,e),k=1)}))),l(t)||(R(z,t),a(E,t)||(L(E,t,x()),k=1),R(S(E,t),s),l(c)||R(D,t))})),o(),u(y)||(T?p(E,(e=>R(D,e))):p(g,(e=>i(S(b,e),(e=>R(D,e))))),I(D,(e=>{const t=(t,s)=>w(S(y,t),S(y,s),e),o=[...S(E,e)];s(o,t)||(L(E,e,x(n(o,t))),R(z,e))}))),(k||T)&&!l(v)){const t=[...E];s(t,v)||(j(e,f(n(t,v))),k=1)}k&&q(t,[e]),I(z,(t=>q(d,[e,t])))}),b(r),i(c,b)),B},delIndexDefinition:e=>(O(e),B),getStore:c,getIndexIds:v,hasIndex:D,hasSlice:(e,t)=>a(M(e),t),getTableId:E,getSliceIds:e=>w(M(e)),getSliceRowIds:(e,t)=>[...S(M(e),t)?.values()??[]],addSliceIdsListener:(e,s)=>m(s,t,[e]),addSliceRowIdsListener:(e,t,s)=>m(s,d,[e,t]),delListener:e=>(A(e),B),destroy:W,getListenerStats:()=>({})};return z(B)}));export{D as createIndexes};
1
+ const e=e=>typeof e,t=e(""),s=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),n=(e,t)=>e.sort(t),o=(e,t)=>e.forEach(t),r=e=>e.length,c=e=>0==r(e),d=e=>e.slice(1),l=e=>null==e,i=(e,t,s)=>l(e)?s?.():t(e),a=(e,t)=>e?.has(t)??!1,h=e=>l(e)||0==(e=>e.size)(e),u=e=>e.clear(),I=(e,t)=>e?.forEach(t),f=(e,t)=>e?.delete(t),g=e=>new Map(e),S=e=>[...e?.keys()??[]],w=(e,t)=>e?.get(t),p=(e,t)=>I(e,((e,s)=>t(s,e))),L=(e,t,s)=>l(s)?(f(e,t),e):e?.set(t,s),x=(e,t,s,n)=>(a(e,t)||(n?.(s),e.set(t,s)),w(e,t)),v=e=>new Set(e),E=(e,t)=>e?.add(t),R=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),b=(e,t)=>e<t?-1:1,y=(e,t,s)=>r(s)<2?E(c(s)?e:x(e,s[0],v()),t):y(x(e,s[0],g()),t,d(s)),T=e=>{const t=(s,n,...r)=>i(s,(s=>c(r)?e(s,n):o([r[0],null],(e=>t(w(s,e),n,...d(r))))));return t},k=Object.freeze,z=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((e=>{const t=g(),c=g(),[d,x,z,C,D,M,j,O,W,m]=((e,t,s)=>{const n=e.hasRow,r=g(),c=g(),d=g(),h=g(),f=g(),x=t=>i(w(f,t),(s=>{I(s,e.delListener),L(f,t)})),E=e=>{L(r,e),L(c,e),L(d,e),L(h,e),x(e)};return[()=>e,()=>S(r),e=>p(c,e),e=>a(c,e),e=>w(r,e),e=>w(c,e),(e,t)=>L(c,e,t),(i,S,E,R,b)=>{const y=g(),T=g();L(r,i,S),a(c,i)||(L(c,i,t()),L(d,i,g()),L(h,i,g()));const k=w(d,i),z=w(h,i),C=t=>{const o=s=>e.getCell(S,t,s),r=w(k,t),c=n(S,t)?s(R(o,t)):void 0;if(r!=c&&L(y,t,[r,c]),!l(b)){const e=w(z,t),s=n(S,t)?b(o,t):void 0;e!=s&&L(T,t,s)}},D=e=>{E((()=>{I(y,(([,e],t)=>L(k,t,e))),I(T,((e,t)=>L(z,t,e)))}),y,T,k,z,e),u(y),u(T)};p(k,C),e.hasTable(S)&&o(e.getRowIds(S),(e=>{a(k,e)||C(e)})),D(!0),x(i),L(f,i,v([e.addRowListener(S,null,((e,t,s)=>C(s))),e.addTableListener(S,(()=>D()))]))},E,()=>p(f,E)]})(e,g,(e=>l(e)?"":e+"")),[q,A,B]=(e=>{let t,s=0;const n=[],c=g();return[(o,r,d=[])=>{t??=e();const l=n.pop()??""+s++;return L(c,l,[o,r,d]),y(r,l,d),l},(e,s=[],...n)=>T(I)(e,(e=>i(w(c,e),(([e])=>e(t,...s,...n)))),...s),e=>i(w(c,e),(([,t,s])=>(T(f)(t,e,...s),L(c,e),r(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>i(w(c,e),(([e,,c])=>{const d=(...i)=>{const a=r(i);a==r(c)?e(t,...i,...n(i)):l(c[a])?o(s[a](...i),(e=>d(...i,e))):d(...i,c[a])};d()}))]})((()=>G)),F=(t,s,n)=>{const o=D(t);I(n,((t,n)=>s(n,(s=>I(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},G={setIndexDefinition:(e,o,r,d,u,S=b)=>{const x=l(u)?void 0:([e],[t])=>u(e,t);return O(e,o,((o,r,u,R,b,y)=>{let T=0;const k=v(),z=v(),C=M(e);if(I(r,(([e,t],s)=>{l(e)||(E(k,e),i(w(C,e),(t=>{f(t,s),h(t)&&(L(C,e),T=1)}))),l(t)||(E(k,t),a(C,t)||(L(C,t,v()),T=1),E(w(C,t),s),l(d)||E(z,t))})),o(),h(b)||(y?p(C,(e=>E(z,e))):p(u,(e=>i(w(R,e),(e=>E(z,e))))),I(z,(e=>{const t=(t,s)=>S(w(b,t),w(b,s),e),o=[...w(C,e)];s(o,t)||(L(C,e,v(n(o,t))),E(k,e))}))),(T||y)&&!l(x)){const t=[...C];s(t,x)||(j(e,g(n(t,x))),T=1)}T&&A(t,[e]),I(k,(t=>A(c,[e,t])))}),R(r),i(d,R)),G},delIndexDefinition:e=>(W(e),G),getStore:d,getIndexIds:x,forEachIndex:e=>z(((t,s)=>e(t,(e=>F(t,e,s))))),forEachSlice:(e,t)=>F(e,t,M(e)),hasIndex:C,hasSlice:(e,t)=>a(M(e),t),getTableId:D,getSliceIds:e=>S(M(e)),getSliceRowIds:(e,t)=>[...w(M(e),t)?.values()??[]],addSliceIdsListener:(e,s)=>q(s,t,[e]),addSliceRowIdsListener:(e,t,s)=>q(s,c,[e,t]),delListener:e=>(B(e),G),destroy:m,getListenerStats:()=>({})};return k(G)}));export{z as createIndexes};
package/lib/indexes.js.gz CHANGED
Binary file
package/lib/metrics.d.ts CHANGED
@@ -22,6 +22,20 @@ import {Id, IdOrNull, Ids} from './common.d';
22
22
  */
23
23
  export type Metric = number;
24
24
 
25
+ /**
26
+ * The MetricCallback type describes a function that takes a Metric's Id and a
27
+ * callback to loop over each Row within it.
28
+ *
29
+ * A MetricCallback is provided when using the forEachMetric method, so that you
30
+ * can do something based on every Metric in the Metrics object. See that method
31
+ * for specific examples.
32
+ *
33
+ * @param metricId The Id of the Metric that the callback can operate on.
34
+ * @param metric The value of the Metric.
35
+ * @category Callback
36
+ */
37
+ export type MetricCallback = (metricId: Id, metric?: Metric) => void;
38
+
25
39
  /**
26
40
  * The Aggregate type describes a custom function that takes an array or numbers
27
41
  * and returns an aggregate that is used as a Metric.
@@ -478,6 +492,38 @@ export interface Metrics {
478
492
  */
479
493
  getMetricIds(): Ids;
480
494
 
495
+ /**
496
+ * The forEachMetric method takes a function that it will then call for each
497
+ * Metric in the Metrics object.
498
+ *
499
+ * This method is useful for iterating over all the Metrics in a functional
500
+ * style. The `metricCallback` parameter is a MetricCallback function that
501
+ * will be called with the Id of each Metric and its value.
502
+ *
503
+ * @param metricCallback The function that should be called for every Metric.
504
+ * @example
505
+ * This example iterates over each Metric in a Metrics object.
506
+ *
507
+ * ```js
508
+ * const store = createStore().setTable('species', {
509
+ * dog: {price: 5},
510
+ * cat: {price: 4},
511
+ * worm: {price: 1},
512
+ * });
513
+ * const metrics = createMetrics(store)
514
+ * .setMetricDefinition('highestPrice', 'species', 'max', 'price')
515
+ * .setMetricDefinition('lowestPrice', 'species', 'min', 'price');
516
+ *
517
+ * metrics.forEachMetric((metricId, metric) => {
518
+ * console.log([metricId, metric]);
519
+ * });
520
+ * // -> ['highestPrice', 5]
521
+ * // -> ['lowestPrice', 1]
522
+ * ```
523
+ * @category Iterator
524
+ */
525
+ forEachMetric(metricCallback: MetricCallback): void;
526
+
481
527
  /**
482
528
  * The hasMetric method returns a boolean indicating whether a given Metric
483
529
  * exists in the Metrics object, and has a value.
package/lib/metrics.js CHANGED
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),s=e(e),n=(e,t)=>e.forEach(t),o=e=>c(e,((e,t)=>e+t),0),i=e=>e.length,r=e=>0==i(e),c=(e,t,s)=>e.reduce(t,s),a=e=>e.slice(1),d=Math.max,l=Math.min,u=isFinite,v=e=>null==e,g=(e,t,s)=>v(e)?s?.():t(e),h=()=>{},M=e=>e.size,f=(e,t)=>e?.has(t)??!1,p=e=>e.clear(),m=(e,t)=>e?.forEach(t),w=(e,t)=>e?.delete(t),L=e=>new Map(e),b=(e,t)=>e?.get(t),x=(e,t)=>m(e,((e,s)=>t(s,e))),y=(e,t,s)=>v(s)?(w(e,t),e):e?.set(t,s),I=(e,t,s,n)=>(f(e,t)||(n?.(s),e.set(t,s)),b(e,t)),R=e=>new Set(e),S=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),T=(e,t,s)=>i(s)<2?((e,t)=>e?.add(t))(r(s)?e:I(e,s[0],R()),t):T(I(e,s[0],L()),t,a(s)),k=e=>{const t=(s,o,...i)=>g(s,(s=>r(i)?e(s,o):n([i[0],null],(e=>t(b(s,e),o,...a(i))))));return t},z=Object.freeze,D=L([["avg",[(e,t)=>o(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>d(...e),(e,t)=>d(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:d(t,e)]],["min",[e=>l(...e),(e,t)=>l(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:l(t,e)]],["sum",[e=>o(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),E=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((t=>{const o=L(),[r,c,a,d,l,I,E,N,j]=((e,t,s)=>{const o=e.hasRow,i=L(),r=L(),c=L(),a=L(),d=L(),l=t=>g(b(d,t),(s=>{m(s,e.delListener),y(d,t)})),u=e=>{y(i,e),y(r,e),y(c,e),y(a,e),l(e)};return[()=>e,()=>[...i?.keys()??[]],e=>f(r,e),e=>b(i,e),e=>b(r,e),(e,t)=>y(r,e,t),(u,g,h,M,w)=>{const I=L(),S=L();y(i,u,g),f(r,u)||(y(r,u,t()),y(c,u,L()),y(a,u,L()));const T=b(c,u),k=b(a,u),z=t=>{const n=s=>e.getCell(g,t,s),i=b(T,t),r=o(g,t)?s(M(n,t)):void 0;if(i!=r&&y(I,t,[i,r]),!v(w)){const e=b(k,t),s=o(g,t)?w(n,t):void 0;e!=s&&y(S,t,s)}},D=e=>{h((()=>{m(I,(([,e],t)=>y(T,t,e))),m(S,((e,t)=>y(k,t,e)))}),I,S,T,k,e),p(I),p(S)};x(T,z),e.hasTable(g)&&n(e.getRowIds(g),(e=>{f(T,e)||z(e)})),D(!0),l(u),y(d,u,R([e.addRowListener(g,null,((e,t,s)=>z(s))),e.addTableListener(g,(()=>D()))]))},u,()=>x(d,u)]})(t,h,(e=>isNaN(e)||v(e)||!0===e||!1===e||""===e?void 0:1*e)),[C,F,O]=(e=>{let t,s=0;const o=[],r=L();return[(n,i,c=[])=>{t??=e();const a=o.pop()??""+s++;return y(r,a,[n,i,c]),T(i,a,c),a},(e,s=[],...n)=>k(m)(e,(e=>g(b(r,e),(([e])=>e(t,...s,...n)))),...s),e=>g(b(r,e),(([,t,s])=>(k(w)(t,e,...s),y(r,e),i(o)<1e3&&o.push(e),s)),(()=>[])),(e,s,o)=>g(b(r,e),(([e,,r])=>{const c=(...a)=>{const d=i(a);d==i(r)?e(t,...a,...o(a)):v(r[d])?n(s[d](...a),(e=>c(...a,e))):c(...a,r[d])};c()}))]})((()=>W)),W={setMetricDefinition:(t,n,i,r,c,a,d)=>{const g=e(i)==s?[i,c,a,d]:b(D,i)??b(D,"sum");return E(t,n,((e,s,n,i,r,c)=>{let a=l(t),d=M(i);const[h,f,p,w]=g;var L;c=c||v(a),m(s,(([e,t])=>{c||(a=v(e)?f?.(a,t,d++):v(t)?p?.(a,e,d--):w?.(a,t,e,d)),c=c||v(a)})),e(),v(L=i)||0==M(L)?a=void 0:c&&(a=h((e=>[...e?.values()??[]])(i),M(i))),u(a)||(a=void 0);const b=l(t);a!=b&&(I(t,a),F(o,[t],a,b))}),S(r,1)),W},delMetricDefinition:e=>(N(e),W),getStore:r,getMetricIds:c,hasMetric:a,getTableId:d,getMetric:l,addMetricListener:(e,t)=>C(t,o,[e]),delListener:e=>(O(e),W),destroy:j,getListenerStats:()=>({})};return z(W)}));export{E as createMetrics};
1
+ const e=e=>typeof e,t=e(""),s=e(e),n=(e,t)=>e.forEach(t),o=e=>c(e,((e,t)=>e+t),0),i=e=>e.length,r=e=>0==i(e),c=(e,t,s)=>e.reduce(t,s),a=e=>e.slice(1),d=Math.max,l=Math.min,u=isFinite,h=e=>null==e,v=(e,t,s)=>h(e)?s?.():t(e),g=()=>{},M=e=>e.size,f=(e,t)=>e?.has(t)??!1,p=e=>e.clear(),m=(e,t)=>e?.forEach(t),w=(e,t)=>e?.delete(t),L=e=>new Map(e),b=(e,t)=>e?.get(t),x=(e,t)=>m(e,((e,s)=>t(s,e))),y=(e,t,s)=>h(s)?(w(e,t),e):e?.set(t,s),E=(e,t,s,n)=>(f(e,t)||(n?.(s),e.set(t,s)),b(e,t)),I=e=>new Set(e),R=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),S=(e,t,s)=>i(s)<2?((e,t)=>e?.add(t))(r(s)?e:E(e,s[0],I()),t):S(E(e,s[0],L()),t,a(s)),T=e=>{const t=(s,o,...i)=>v(s,(s=>r(i)?e(s,o):n([i[0],null],(e=>t(b(s,e),o,...a(i))))));return t},k=Object.freeze,z=L([["avg",[(e,t)=>o(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>d(...e),(e,t)=>d(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:d(t,e)]],["min",[e=>l(...e),(e,t)=>l(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:l(t,e)]],["sum",[e=>o(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),D=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((t=>{const o=L(),[r,c,a,d,l,E,D,N,j,C]=((e,t,s)=>{const o=e.hasRow,i=L(),r=L(),c=L(),a=L(),d=L(),l=t=>v(b(d,t),(s=>{m(s,e.delListener),y(d,t)})),u=e=>{y(i,e),y(r,e),y(c,e),y(a,e),l(e)};return[()=>e,()=>[...i?.keys()??[]],e=>x(r,e),e=>f(r,e),e=>b(i,e),e=>b(r,e),(e,t)=>y(r,e,t),(u,v,g,M,w)=>{const E=L(),R=L();y(i,u,v),f(r,u)||(y(r,u,t()),y(c,u,L()),y(a,u,L()));const S=b(c,u),T=b(a,u),k=t=>{const n=s=>e.getCell(v,t,s),i=b(S,t),r=o(v,t)?s(M(n,t)):void 0;if(i!=r&&y(E,t,[i,r]),!h(w)){const e=b(T,t),s=o(v,t)?w(n,t):void 0;e!=s&&y(R,t,s)}},z=e=>{g((()=>{m(E,(([,e],t)=>y(S,t,e))),m(R,((e,t)=>y(T,t,e)))}),E,R,S,T,e),p(E),p(R)};x(S,k),e.hasTable(v)&&n(e.getRowIds(v),(e=>{f(S,e)||k(e)})),z(!0),l(u),y(d,u,I([e.addRowListener(v,null,((e,t,s)=>k(s))),e.addTableListener(v,(()=>z()))]))},u,()=>x(d,u)]})(t,g,(e=>isNaN(e)||h(e)||!0===e||!1===e||""===e?void 0:1*e)),[F,O,W]=(e=>{let t,s=0;const o=[],r=L();return[(n,i,c=[])=>{t??=e();const a=o.pop()??""+s++;return y(r,a,[n,i,c]),S(i,a,c),a},(e,s=[],...n)=>T(m)(e,(e=>v(b(r,e),(([e])=>e(t,...s,...n)))),...s),e=>v(b(r,e),(([,t,s])=>(T(w)(t,e,...s),y(r,e),i(o)<1e3&&o.push(e),s)),(()=>[])),(e,s,o)=>v(b(r,e),(([e,,r])=>{const c=(...a)=>{const d=i(a);d==i(r)?e(t,...a,...o(a)):h(r[d])?n(s[d](...a),(e=>c(...a,e))):c(...a,r[d])};c()}))]})((()=>q)),q={setMetricDefinition:(t,n,i,r,c,a,d)=>{const l=e(i)==s?[i,c,a,d]:b(z,i)??b(z,"sum");return N(t,n,((e,s,n,i,r,c)=>{let a=E(t),d=M(i);const[v,g,f,p]=l;var w;c=c||h(a),m(s,(([e,t])=>{c||(a=h(e)?g?.(a,t,d++):h(t)?f?.(a,e,d--):p?.(a,t,e,d)),c=c||h(a)})),e(),h(w=i)||0==M(w)?a=void 0:c&&(a=v((e=>[...e?.values()??[]])(i),M(i))),u(a)||(a=void 0);const L=E(t);a!=L&&(D(t,a),O(o,[t],a,L))}),R(r,1)),q},delMetricDefinition:e=>(j(e),q),getStore:r,getMetricIds:c,forEachMetric:a,hasMetric:d,getTableId:l,getMetric:E,addMetricListener:(e,t)=>F(t,o,[e]),delListener:e=>(W(e),q),destroy:C,getListenerStats:()=>({})};return k(q)}));export{D as createMetrics};
package/lib/metrics.js.gz CHANGED
Binary file
@@ -11,7 +11,7 @@
11
11
  * @module relationships
12
12
  */
13
13
 
14
- import {GetCell, Store} from './store.d';
14
+ import {GetCell, RowCallback, Store} from './store.d';
15
15
  import {Id, IdOrNull, Ids} from './common.d';
16
16
 
17
17
  /**
@@ -39,6 +39,25 @@ export type Relationship = {
39
39
  linkedRowIds: {[firstRowId: Id]: Ids};
40
40
  };
41
41
 
42
+ /**
43
+ * The RelationshipCallback type describes a function that takes a
44
+ * Relationship's Id and a callback to loop over each local Row within it.
45
+ *
46
+ * A RelationshipCallback is provided when using the forEachRelationship method,
47
+ * so that you can do something based on every Relationship in the Relationships
48
+ * object. See that method for specific examples.
49
+ *
50
+ * @param relationshipId The Id of the Relationship that the callback can
51
+ * operate on.
52
+ * @param forEachRow A function that will let you iterate over the local Row
53
+ * objects in this Relationship.
54
+ * @category Callback
55
+ */
56
+ export type RelationshipCallback = (
57
+ relationshipId: Id,
58
+ forEachRow: (rowCallback: RowCallback) => void,
59
+ ) => void;
60
+
42
61
  /**
43
62
  * The RemoteRowIdListener type describes a function that is used to listen to
44
63
  * changes to the remote Row Id end of a Relationship.
@@ -405,6 +424,49 @@ export interface Relationships {
405
424
  */
406
425
  getRelationshipIds(): Ids;
407
426
 
427
+ /**
428
+ * The forEachRelationship method takes a function that it will then call for
429
+ * each Relationship in a specified Relationships object.
430
+ *
431
+ * This method is useful for iterating over the structure of the Relationships
432
+ * object in a functional style. The `relationshipCallback` parameter is a
433
+ * RelationshipCallback function that will be called with the Id of each
434
+ * Relationship, and with a function that can then be used to iterate over
435
+ * each local Row involved in the Relationship.
436
+ *
437
+ * @param relationshipCallback The function that should be called for every
438
+ * Relationship.
439
+ * @example
440
+ * This example iterates over each Relationship in a Relationships object, and
441
+ * lists each Row Id within them.
442
+ *
443
+ * ```js
444
+ * const store = createStore().setTable('pets', {
445
+ * fido: {species: 'dog', next: 'felix'},
446
+ * felix: {species: 'cat', next: 'cujo'},
447
+ * cujo: {species: 'dog'},
448
+ * });
449
+ * const relationships = createRelationships(store)
450
+ * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
451
+ * .setRelationshipDefinition('petSequence', 'pets', 'pets', 'next');
452
+ *
453
+ * relationships.forEachRelationship((relationshipId, forEachRow) => {
454
+ * console.log(relationshipId);
455
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
456
+ * });
457
+ * // -> 'petSpecies'
458
+ * // -> '- fido'
459
+ * // -> '- felix'
460
+ * // -> '- cujo'
461
+ * // -> 'petSequence'
462
+ * // -> '- fido'
463
+ * // -> '- felix'
464
+ * // -> '- cujo'
465
+ * ```
466
+ * @category Iterator
467
+ */
468
+ forEachRelationship(relationshipCallback: RelationshipCallback): void;
469
+
408
470
  /**
409
471
  * The hasRelationship method returns a boolean indicating whether a given
410
472
  * Relationship exists in the Relationships object.
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),s=(e,t)=>e.forEach(t),n=e=>e.length,o=e=>0==n(e),i=e=>e.slice(1),r=e=>null==e,d=(e,t,s)=>r(e)?s?.():t(e),a=(e,t)=>e?.has(t)??!1,l=e=>[...e?.values()??[]],c=e=>e.clear(),R=(e,t)=>e?.forEach(t),g=(e,t)=>e?.delete(t),h=e=>new Map(e),u=(e,t)=>e?.get(t),L=(e,t)=>R(e,((e,s)=>t(s,e))),w=(e,t,s)=>r(s)?(g(e,t),e):e?.set(t,s),f=(e,t,s,n)=>(a(e,t)||(n?.(s),e.set(t,s)),u(e,t)),p=e=>new Set(e),I=(e,t)=>e?.add(t),b=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),v=(e,t,s)=>n(s)<2?I(o(s)?e:f(e,s[0],p()),t):v(f(e,s[0],h()),t,i(s)),k=e=>{const t=(n,r,...a)=>d(n,(n=>o(a)?e(n,r):s([a[0],null],(e=>t(u(n,e),r,...i(a))))));return t},T=Object.freeze,m=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((e=>{const t=h(),o=h(),i=h(),f=h(),[m,y,S,z,D,E,M,j,x]=((e,t,n)=>{const o=e.hasRow,i=h(),l=h(),g=h(),f=h(),I=h(),b=t=>d(u(I,t),(s=>{R(s,e.delListener),w(I,t)})),v=e=>{w(i,e),w(l,e),w(g,e),w(f,e),b(e)};return[()=>e,()=>[...i?.keys()??[]],e=>a(l,e),e=>u(i,e),e=>u(l,e),(e,t)=>w(l,e,t),(d,v,k,T,m)=>{const y=h(),S=h();w(i,d,v),a(l,d)||(w(l,d,t()),w(g,d,h()),w(f,d,h()));const z=u(g,d),D=u(f,d),E=t=>{const s=s=>e.getCell(v,t,s),i=u(z,t),d=o(v,t)?n(T(s,t)):void 0;if(i!=d&&w(y,t,[i,d]),!r(m)){const e=u(D,t),n=o(v,t)?m(s,t):void 0;e!=n&&w(S,t,n)}},M=e=>{k((()=>{R(y,(([,e],t)=>w(z,t,e))),R(S,((e,t)=>w(D,t,e)))}),y,S,z,D,e),c(y),c(S)};L(z,E),e.hasTable(v)&&s(e.getRowIds(v),(e=>{a(z,e)||E(e)})),M(!0),b(d),w(I,d,p([e.addRowListener(v,null,((e,t,s)=>E(s))),e.addTableListener(v,(()=>M()))]))},v,()=>L(I,v)]})(e,(()=>[h(),h(),h(),h()]),(e=>r(e)?void 0:e+"")),[C,O,W]=(e=>{let t,o=0;const i=[],a=h();return[(s,n,r=[])=>{t??=e();const d=i.pop()??""+o++;return w(a,d,[s,n,r]),v(n,d,r),d},(e,s=[],...n)=>k(R)(e,(e=>d(u(a,e),(([e])=>e(t,...s,...n)))),...s),e=>d(u(a,e),(([,t,s])=>(k(g)(t,e,...s),w(a,e),n(i)<1e3&&i.push(e),s)),(()=>[])),(e,o,i)=>d(u(a,e),(([e,,d])=>{const a=(...l)=>{const c=n(l);c==n(d)?e(t,...l,...i(l)):r(d[c])?s(o[c](...l),(e=>a(...l,e))):a(...l,d[c])};a()}))]})((()=>F)),q=(e,t,s)=>d(D(e),(([n,,o])=>{if(!a(o,t)){const i=p();if(z(e)!=B(e))I(i,t);else{let e=t;for(;!r(e)&&!a(i,e);)I(i,e),e=u(n,e)}if(s)return i;w(o,t,i)}return u(o,t)})),A=(e,t)=>d(D(e),(([,,e])=>w(e,t))),B=e=>u(t,e),F={setRelationshipDefinition:(e,s,n,l)=>(w(t,e,n),M(e,s,((t,s)=>{const n=p(),l=p(),c=p(),[h,b]=D(e);R(s,(([t,s],o)=>{r(t)||(I(l,t),d(u(b,t),(e=>{var s;g(e,o),(r(s=e)||0==(e=>e.size)(s))&&w(b,t)}))),r(s)||(I(l,s),a(b,s)||w(b,s,p()),I(u(b,s),o)),I(n,o),w(h,o,s),L(u(f,e),(t=>{a(q(e,t),o)&&I(c,t)}))})),t(),R(n,(t=>O(o,[e,t]))),R(l,(t=>O(i,[e,t]))),R(c,(t=>{A(e,t),O(f,[e,t])}))}),b(l)),F),delRelationshipDefinition:e=>(w(t,e),j(e),F),getStore:m,getRelationshipIds:y,hasRelationship:S,getLocalTableId:z,getRemoteTableId:B,getRemoteRowId:(e,t)=>u(D(e)?.[0],t),getLocalRowIds:(e,t)=>l(u(D(e)?.[1],t)),getLinkedRowIds:(e,t)=>r(D(e))?[t]:l(q(e,t,!0)),addRemoteRowIdListener:(e,t,s)=>C(s,o,[e,t]),addLocalRowIdsListener:(e,t,s)=>C(s,i,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(q(e,t),C(s,f,[e,t])),delListener:e=>(A(...W(e)),F),destroy:x,getListenerStats:()=>({})};return T(F)}));export{m as createRelationships};
1
+ const e=e=>typeof e,t=e(""),o=(e,t)=>e.forEach(t),s=e=>e.length,n=e=>0==s(e),i=e=>e.slice(1),r=e=>null==e,a=(e,t,o)=>r(e)?o?.():t(e),d=(e,t)=>e?.has(t)??!1,l=e=>[...e?.values()??[]],c=e=>e.clear(),R=(e,t)=>e?.forEach(t),h=(e,t)=>e?.delete(t),f=e=>new Map(e),g=(e,t)=>e?.get(t),u=(e,t)=>R(e,((e,o)=>t(o,e))),w=(e,t,o)=>r(o)?(h(e,t),e):e?.set(t,o),L=(e,t,o,s)=>(d(e,t)||(s?.(o),e.set(t,o)),g(e,t)),p=e=>new Set(e),I=(e,t)=>e?.add(t),b=(o,s)=>e(o)==t?e=>e(o):o??(()=>s??""),v=(e,t,o)=>s(o)<2?I(n(o)?e:L(e,o[0],p()),t):v(L(e,o[0],f()),t,i(o)),k=e=>{const t=(s,r,...d)=>a(s,(s=>n(d)?e(s,r):o([d[0],null],(e=>t(g(s,e),r,...i(d))))));return t},E=Object.freeze,T=(e=>{const t=new WeakMap;return o=>(t.has(o)||t.set(o,e(o)),t.get(o))})((e=>{const t=f(),n=f(),i=f(),L=f(),[T,m,y,S,z,D,M,j,x,C]=((e,t,s)=>{const n=e.hasRow,i=f(),l=f(),h=f(),L=f(),I=f(),b=t=>a(g(I,t),(o=>{R(o,e.delListener),w(I,t)})),v=e=>{w(i,e),w(l,e),w(h,e),w(L,e),b(e)};return[()=>e,()=>[...i?.keys()??[]],e=>u(l,e),e=>d(l,e),e=>g(i,e),e=>g(l,e),(e,t)=>w(l,e,t),(a,v,k,E,T)=>{const m=f(),y=f();w(i,a,v),d(l,a)||(w(l,a,t()),w(h,a,f()),w(L,a,f()));const S=g(h,a),z=g(L,a),D=t=>{const o=o=>e.getCell(v,t,o),i=g(S,t),a=n(v,t)?s(E(o,t)):void 0;if(i!=a&&w(m,t,[i,a]),!r(T)){const e=g(z,t),s=n(v,t)?T(o,t):void 0;e!=s&&w(y,t,s)}},M=e=>{k((()=>{R(m,(([,e],t)=>w(S,t,e))),R(y,((e,t)=>w(z,t,e)))}),m,y,S,z,e),c(m),c(y)};u(S,D),e.hasTable(v)&&o(e.getRowIds(v),(e=>{d(S,e)||D(e)})),M(!0),b(a),w(I,a,p([e.addRowListener(v,null,((e,t,o)=>D(o))),e.addTableListener(v,(()=>M()))]))},v,()=>u(I,v)]})(e,(()=>[f(),f(),f(),f()]),(e=>r(e)?void 0:e+"")),[O,W,q]=(e=>{let t,n=0;const i=[],d=f();return[(o,s,r=[])=>{t??=e();const a=i.pop()??""+n++;return w(d,a,[o,s,r]),v(s,a,r),a},(e,o=[],...s)=>k(R)(e,(e=>a(g(d,e),(([e])=>e(t,...o,...s)))),...o),e=>a(g(d,e),(([,t,o])=>(k(h)(t,e,...o),w(d,e),s(i)<1e3&&i.push(e),o)),(()=>[])),(e,n,i)=>a(g(d,e),(([e,,a])=>{const d=(...l)=>{const c=s(l);c==s(a)?e(t,...l,...i(l)):r(a[c])?o(n[c](...l),(e=>d(...l,e))):d(...l,a[c])};d()}))]})((()=>G)),A=(e,t,o)=>a(D(e),(([s,,n])=>{if(!d(n,t)){const i=p();if(z(e)!=F(e))I(i,t);else{let e=t;for(;!r(e)&&!d(i,e);)I(i,e),e=g(s,e)}if(o)return i;w(n,t,i)}return g(n,t)})),B=(e,t)=>a(D(e),(([,,e])=>w(e,t))),F=e=>g(t,e),G={setRelationshipDefinition:(e,o,s,l)=>(w(t,e,s),j(e,o,((t,o)=>{const s=p(),l=p(),c=p(),[f,b]=D(e);R(o,(([t,o],n)=>{r(t)||(I(l,t),a(g(b,t),(e=>{var o;h(e,n),(r(o=e)||0==(e=>e.size)(o))&&w(b,t)}))),r(o)||(I(l,o),d(b,o)||w(b,o,p()),I(g(b,o),n)),I(s,n),w(f,n,o),u(g(L,e),(t=>{d(A(e,t),n)&&I(c,t)}))})),t(),R(s,(t=>W(n,[e,t]))),R(l,(t=>W(i,[e,t]))),R(c,(t=>{B(e,t),W(L,[e,t])}))}),b(l)),G),delRelationshipDefinition:e=>(w(t,e),x(e),G),getStore:T,getRelationshipIds:m,forEachRelationship:t=>y((o=>t(o,(t=>e.forEachRow(z(o),t))))),hasRelationship:S,getLocalTableId:z,getRemoteTableId:F,getRemoteRowId:(e,t)=>g(D(e)?.[0],t),getLocalRowIds:(e,t)=>l(g(D(e)?.[1],t)),getLinkedRowIds:(e,t)=>r(D(e))?[t]:l(A(e,t,!0)),addRemoteRowIdListener:(e,t,o)=>O(o,n,[e,t]),addLocalRowIdsListener:(e,t,o)=>O(o,i,[e,t]),addLinkedRowIdsListener:(e,t,o)=>(A(e,t),O(o,L,[e,t])),delListener:e=>(B(...q(e)),G),destroy:C,getListenerStats:()=>({})};return E(G)}));export{T as createRelationships};
Binary file
package/lib/store.d.ts CHANGED
@@ -89,7 +89,7 @@ export type Row = {[cellId: Id]: Cell};
89
89
  export type Cell = string | number | boolean;
90
90
 
91
91
  /**
92
- * The TableCallback type describes a function that takes a Tables's Id and a
92
+ * The TableCallback type describes a function that takes a Table's Id and a
93
93
  * callback to loop over each Row within it.
94
94
  *
95
95
  * A TableCallback is provided when using the forEachTable method, so that you
@@ -1631,7 +1631,7 @@ export interface Store {
1631
1631
  *
1632
1632
  * This method is useful for iterating over the Table structure of the Store
1633
1633
  * in a functional style. The `tableCallback` parameter is a TableCallback
1634
- * function that will called with the Id of each Table, and with a function
1634
+ * function that will be called with the Id of each Table, and with a function
1635
1635
  * that can then be used to iterate over each Row of the Table, should you
1636
1636
  * wish.
1637
1637
  *
@@ -1664,9 +1664,10 @@ export interface Store {
1664
1664
  *
1665
1665
  * This method is useful for iterating over the Row structure of the Table in
1666
1666
  * a functional style. The `rowCallback` parameter is a RowCallback function
1667
- * that will called with the Id of each Row, and with a function that can then
1668
- * be used to iterate over each Cell of the Row, should you wish.
1667
+ * that will be called with the Id of each Row, and with a function that can
1668
+ * then be used to iterate over each Cell of the Row, should you wish.
1669
1669
  *
1670
+ * @param tableId The Id of the Table to iterate over.
1670
1671
  * @param rowCallback The function that should be called for every Row.
1671
1672
  * @example
1672
1673
  * This example iterates over each Row in a Table, and lists each Cell Id
@@ -1698,8 +1699,10 @@ export interface Store {
1698
1699
  *
1699
1700
  * This method is useful for iterating over the Cell structure of the Row in a
1700
1701
  * functional style. The `cellCallback` parameter is a CellCallback function
1701
- * that will called with the Id and value of each Cell.
1702
+ * that will be called with the Id and value of each Cell.
1702
1703
  *
1704
+ * @param tableId The Id of the Table containing the Row to iterate over.
1705
+ * @param rowId The Id of the Row to iterate over.
1703
1706
  * @param cellCallback The function that should be called for every Cell.
1704
1707
  * @example
1705
1708
  * This example iterates over each Cell in a Row, and lists its value.
package/lib/tinybase.js CHANGED
@@ -1 +1 @@
1
- import{promises as e,watch as t}from"fs";const s=e=>typeof e,n=s(""),o=s(!0),r=s(0),a=s(s),i=(e,t)=>e.includes(t),d=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),l=(e,t)=>e.sort(t),c=(e,t)=>e.forEach(t),u=e=>f(e,((e,t)=>e+t),0),g=e=>e.length,h=e=>0==g(e),f=(e,t,s)=>e.reduce(t,s),L=e=>e.slice(1),p=e=>JSON.stringify(e,((e,t)=>S(t,Map)?f([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),w=JSON.parse,v=Math.max,I=Math.min,y=isFinite,S=(e,t)=>e instanceof t,R=e=>null==e,T=(e,t,s)=>R(e)?s?.():t(e),b=e=>e==n||e==o,C=e=>s(e)==a,m=()=>{},k=e=>e.size,M=(e,t)=>e?.has(t)??!1,A=e=>R(e)||0==k(e),E=e=>[...e?.values()??[]],x=e=>e.clear(),D=(e,t)=>e?.forEach(t),J=(e,t)=>e?.delete(t),F=e=>new Map(e),z=(e=F)=>[e(),e()],N=e=>[...e?.keys()??[]],O=(e,t)=>e?.get(t),j=(e,t)=>D(e,((e,s)=>t(s,e))),P=(e,t,s)=>R(s)?(J(e,t),e):e?.set(t,s),B=(e,t,s,n)=>(M(e,t)||(n?.(s),e.set(t,s)),O(e,t)),H=(e,t)=>{const s={},n=t??(e=>e);return D(e,((e,t)=>s[t]=n(e))),s},W=(e,t)=>{const s=F(),n=t??(e=>e);return D(e,((e,t)=>s.set(t,n(e)))),s},q=e=>new Set(e),G=(e,t)=>e?.add(t),K=(e,t,s)=>{const n=e.hasRow,o=F(),r=F(),a=F(),i=F(),d=F(),l=t=>T(O(d,t),(s=>{D(s,e.delListener),P(d,t)})),u=e=>{P(o,e),P(r,e),P(a,e),P(i,e),l(e)};return[()=>e,()=>N(o),e=>M(r,e),e=>O(o,e),e=>O(r,e),(e,t)=>P(r,e,t),(u,g,h,f,L)=>{const p=F(),w=F();P(o,u,g),M(r,u)||(P(r,u,t()),P(a,u,F()),P(i,u,F()));const v=O(a,u),I=O(i,u),y=t=>{const o=s=>e.getCell(g,t,s),r=O(v,t),a=n(g,t)?s(f(o,t)):void 0;if(r!=a&&P(p,t,[r,a]),!R(L)){const e=O(I,t),s=n(g,t)?L(o,t):void 0;e!=s&&P(w,t,s)}},S=e=>{h((()=>{D(p,(([,e],t)=>P(v,t,e))),D(w,((e,t)=>P(I,t,e)))}),p,w,v,I,e),x(p),x(w)};j(v,y),e.hasTable(g)&&c(e.getRowIds(g),(e=>{M(v,e)||y(e)})),S(!0),l(u),P(d,u,q([e.addRowListener(g,null,((e,t,s)=>y(s))),e.addTableListener(g,(()=>S()))]))},u,()=>j(d,u)]},Q=(e,t)=>s(e)==n?t=>t(e):e??(()=>t??""),U=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},V=(e,t,s)=>g(s)<2?G(h(s)?e:B(e,s[0],q()),t):V(B(e,s[0],F()),t,L(s)),X=e=>{const t=(s,n,...o)=>T(s,(s=>h(o)?e(s,n):c([o[0],null],(e=>t(O(s,e),n,...L(o))))));return t},Y=e=>{let t,s=0;const n=[],o=F();return[(r,a,i=[])=>{t??=e();const d=n.pop()??""+s++;return P(o,d,[r,a,i]),V(a,d,i),d},(e,s=[],...n)=>X(D)(e,(e=>T(O(o,e),(([e])=>e(t,...s,...n)))),...s),e=>T(O(o,e),(([,t,s])=>(X(J)(t,e,...s),P(o,e),g(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>T(O(o,e),(([e,,o])=>{const r=(...a)=>{const i=g(a);i==g(o)?e(t,...a,...n(a)):R(o[i])?c(s[i](...a),(e=>r(...a,e))):r(...a,o[i])};r()}))]},Z=Object,$=Z.keys,_=Z.isFrozen,ee=Z.freeze,te=(e,t)=>!R(((e,t)=>T(e,(e=>e[t])))(e,t)),se=(e,t)=>delete e[t],ne=(e,t)=>c(Z.entries(e),(([e,s])=>t(s,e))),oe=U((e=>{let t,s,n,o=100,r=F(),a=1;const d=q(),l=F(),[u,f,L]=Y((()=>H)),p=F(),w=F(),v=[],I=[],y=(t,s)=>{a=0,e.transaction((()=>D(O(p,s),((s,n)=>D(s,((s,o)=>D(s,((s,r)=>R(s[t])?e.delCell(n,o,r,!0):e.setCell(n,o,r,s[t]))))))))),a=1},S=e=>{P(p,e),P(w,e),f(l,[e])},b=(e,t)=>c(((e,t)=>e.splice(0,t))(e,t??g(e)),S),C=()=>b(v,g(v)-o),m=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(a){T(t,(()=>{v.push(t),C(),b(I),t=void 0,n=1}));const e=B(r,s,F()),a=B(e,o,F()),c=B(a,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&A(P(a,i))&&A(P(e,o))&&A(P(r,s))&&(t=v.pop(),n=1),J()}})),k=(e="")=>(R(t)&&(t=""+s++,P(p,t,r),N(t,e),r=F(),n=1),t),E=()=>{h(v)||(I.unshift(k()),y(0,t),t=v.pop(),n=1)},x=()=>{h(I)||(v.push(t),t=I.shift(),y(1,t),n=1)},J=()=>{n&&(f(d),n=0)},z=e=>{const t=k(e);return J(),t},N=(e,t)=>(j(e)&&O(w,e)!==t&&(P(w,e,t),f(l,[e])),H),j=e=>M(p,e),H={setSize:e=>(o=e,C(),H),addCheckpoint:z,setCheckpoint:N,getStore:()=>e,getCheckpointIds:()=>[[...v],t,[...I]],hasCheckpoint:j,getCheckpoint:e=>O(w,e),goBackward:()=>(E(),J(),H),goForward:()=>(x(),J(),H),goTo:e=>{const s=i(v,e)?E:i(I,e)?x:null;for(;!R(s)&&e!=t;)s();return J(),H},addCheckpointIdsListener:e=>u(e,d),addCheckpointListener:(e,t)=>u(t,l,[e]),delListener:e=>(L(e),H),clear:()=>(b(v),b(I),R(t)||S(t),t=void 0,s=0,z(),H),destroy:()=>{e.delListener(m)},getListenerStats:()=>({})};return ee(H.clear())})),re=(e,t)=>e<t?-1:1,ae=U((e=>{const t=F(),s=F(),[n,o,r,a,i,c,u,g,h]=K(e,F,(e=>R(e)?"":e+"")),[f,L,p]=Y((()=>w)),w={setIndexDefinition:(e,n,o,r,a,g=re)=>{const h=R(a)?void 0:([e],[t])=>a(e,t);return u(e,n,((n,o,a,u,f,p)=>{let w=0;const v=q(),I=q(),y=i(e);if(D(o,(([e,t],s)=>{R(e)||(G(v,e),T(O(y,e),(t=>{J(t,s),A(t)&&(P(y,e),w=1)}))),R(t)||(G(v,t),M(y,t)||(P(y,t,q()),w=1),G(O(y,t),s),R(r)||G(I,t))})),n(),A(f)||(p?j(y,(e=>G(I,e))):j(a,(e=>T(O(u,e),(e=>G(I,e))))),D(I,(e=>{const t=(t,s)=>g(O(f,t),O(f,s),e),s=[...O(y,e)];d(s,t)||(P(y,e,q(l(s,t))),G(v,e))}))),(w||p)&&!R(h)){const t=[...y];d(t,h)||(c(e,F(l(t,h))),w=1)}w&&L(t,[e]),D(v,(t=>L(s,[e,t])))}),Q(o),T(r,Q)),w},delIndexDefinition:e=>(g(e),w),getStore:n,getIndexIds:o,hasIndex:r,hasSlice:(e,t)=>M(i(e),t),getTableId:a,getSliceIds:e=>N(i(e)),getSliceRowIds:(e,t)=>E(O(i(e),t)),addSliceIdsListener:(e,s)=>f(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>f(n,s,[e,t]),delListener:e=>(p(e),w),destroy:h,getListenerStats:()=>({})};return ee(w)})),ie=F([["avg",[(e,t)=>u(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>v(...e),(e,t)=>v(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:v(t,e)]],["min",[e=>I(...e),(e,t)=>I(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:I(t,e)]],["sum",[e=>u(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),de=U((e=>{const t=F(),[s,n,o,r,a,i,d,l,c]=K(e,m,(e=>isNaN(e)||R(e)||!0===e||!1===e||""===e?void 0:1*e)),[u,g,h]=Y((()=>f)),f={setMetricDefinition:(e,s,n,o,r,l,c)=>{const u=C(n)?[n,r,l,c]:O(ie,n)??O(ie,"sum");return d(e,s,((s,n,o,r,d,l)=>{let c=a(e),h=k(r);const[f,L,p,w]=u;l=l||R(c),D(n,(([e,t])=>{l||(c=R(e)?L?.(c,t,h++):R(t)?p?.(c,e,h--):w?.(c,t,e,h)),l=l||R(c)})),s(),A(r)?c=void 0:l&&(c=f(E(r),k(r))),y(c)||(c=void 0);const v=a(e);c!=v&&(i(e,c),g(t,[e],c,v))}),Q(o,1)),f},delMetricDefinition:e=>(l(e),f),getStore:s,getMetricIds:n,hasMetric:o,getTableId:r,getMetric:a,addMetricListener:(e,s)=>u(s,t,[e]),delListener:e=>(h(e),f),destroy:c,getListenerStats:()=>({})};return ee(f)})),le=(e,t,s,n,o)=>{let r,a=0;const i={load:async s=>{if(2!=a){a=1;const n=await t();R(n)||""==n?e.setTables(s):e.setJson(n),a=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=a&&(a=2,await s(e.getJson()),a=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),r=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(T(r,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return ee(i)},ce=globalThis.window,ue=(e,t,s)=>{let n;return le(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},ce.addEventListener("storage",n)}),(()=>{ce.removeEventListener("storage",n),n=void 0}))},ge=(e,t)=>ue(e,t,localStorage),he=(e,t)=>ue(e,t,sessionStorage),fe=(s,n)=>{let o;return le(s,(async()=>{try{return await e.readFile(n,"utf8")}catch{}}),(async t=>{try{await e.writeFile(n,t,"utf8")}catch{}}),(e=>{o=t(n,e)}),(()=>{o?.close(),o=void 0}))},Le=e=>e.headers.get("ETag"),pe=(e,t,s,n)=>{let o,r;return le(e,(async()=>{const e=await fetch(t);return r=Le(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=Le(s);R(r)||R(n)||n==r||(r=n,e())}),1e3*n)}),(()=>{T(o,clearInterval),o=void 0}))},we=U((e=>{const t=F(),s=F(),n=F(),o=F(),[r,a,i,d,l,c,u,g,h]=K(e,(()=>[F(),F(),F(),F()]),(e=>R(e)?void 0:e+"")),[f,L,p]=Y((()=>y)),w=(e,t,s)=>T(l(e),(([n,,o])=>{if(!M(o,t)){const r=q();if(d(e)!=I(e))G(r,t);else{let e=t;for(;!R(e)&&!M(r,e);)G(r,e),e=O(n,e)}if(s)return r;P(o,t,r)}return O(o,t)})),v=(e,t)=>T(l(e),(([,,e])=>P(e,t))),I=e=>O(t,e),y={setRelationshipDefinition:(e,r,a,i)=>(P(t,e,a),u(e,r,((t,r)=>{const a=q(),i=q(),d=q(),[c,u]=l(e);D(r,(([t,s],n)=>{R(t)||(G(i,t),T(O(u,t),(e=>{J(e,n),A(e)&&P(u,t)}))),R(s)||(G(i,s),M(u,s)||P(u,s,q()),G(O(u,s),n)),G(a,n),P(c,n,s),j(O(o,e),(t=>{M(w(e,t),n)&&G(d,t)}))})),t(),D(a,(t=>L(s,[e,t]))),D(i,(t=>L(n,[e,t]))),D(d,(t=>{v(e,t),L(o,[e,t])}))}),Q(i)),y),delRelationshipDefinition:e=>(P(t,e),g(e),y),getStore:r,getRelationshipIds:a,hasRelationship:i,getLocalTableId:d,getRemoteTableId:I,getRemoteRowId:(e,t)=>O(l(e)?.[0],t),getLocalRowIds:(e,t)=>E(O(l(e)?.[1],t)),getLinkedRowIds:(e,t)=>R(l(e))?[t]:E(w(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>f(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>f(s,n,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(w(e,t),f(s,o,[e,t])),delListener:e=>(v(...p(e)),y),destroy:h,getListenerStats:()=>({})};return ee(y)})),ve=(e,t,s,n=P)=>{const o=(r=N(e),a=e=>!te(t,e),r.filter(a));var r,a;return c($(t),(n=>s(e,n,t[n]))),c(o,(t=>n(e,t))),e},Ie=e=>{const t=s(e);return b(t)||t==r&&y(e)?t:void 0},ye=(e,t)=>!(R(e)||!(e=>S(e,Z)&&e.constructor==Z)(e)||_(e))&&(ne(e,((s,n)=>{t(s,n)||se(e,n)})),!(e=>h($(e)))(e)),Se=(e,t,s)=>P(e,t,O(e,t)==-s?void 0:s),Re=()=>{let e,t=0,s=0;const n=F(),o=F(),a=F(),d=F(),l=F(),u=F(),g=F(),h=z(q),f=z(q),L=z(),v=z(),I=z(),y=z(),S=z(),[m,k,E,J]=Y((()=>Ae)),G=(t,s)=>(!e||M(l,s))&&ye(t,(e=>K(s,e))),K=(e,t,s)=>ye(s?t:U(t,e),((s,n)=>T(Q(e,n,s),(e=>(t[n]=e,!0)),(()=>!1)))),Q=(t,s,n)=>e?T(O(O(l,t),s),(e=>Ie(n)!=e.type?e.default:n)):R(Ie(n))?void 0:n,U=(e,t)=>(T(O(u,t),(t=>ne(t,((t,s)=>{te(e,s)||(e[s]=t)})))),e),V=e=>ve(l,e,((e,t,s)=>{const n={};ve(B(l,t,F()),s,((e,t,s)=>{P(e,t,s),T(s.default,(e=>n[t]=e))})),P(u,t,n)}),((e,t)=>{P(l,t),P(u,t)})),X=e=>ve(g,e,((e,t,s)=>Z(t,s)),((e,t)=>de(t))),Z=(e,t)=>ve(B(g,e,F(),(()=>ue(e,1))),t,((t,s,n)=>$(e,t,s,n)),((t,s)=>le(e,t,s))),$=(e,t,s,n,o)=>ve(B(t,s,F(),(()=>ge(e,s,1))),n,((t,n,o)=>_(e,s,t,n,o)),((n,r)=>ce(e,t,s,n,r,o))),_=(e,t,s,n,o)=>{M(s,n)||he(e,t,n,1);const r=O(s,n);o!==r&&(fe(e,t,n,r),P(s,n,o))},oe=(e,t,s)=>Me((()=>$(e,ie(e),t,s))),re=(e,t,s,n,o)=>T(O(t,s),(t=>_(e,s,t,n,o)),(()=>$(e,t,s,U({[n]:o},e)))),ae=e=>{const s=""+t++;return M(e,s)?ae(e):s},ie=e=>O(g,e)??Z(e,{}),de=e=>Z(e,{}),le=(e,t,s)=>$(e,t,s,{},!0),ce=(e,t,s,n,o,r)=>{const a=O(u,e)?.[o];if(!R(a)&&!r)return _(e,s,n,o,a);const i=t=>{fe(e,s,t,O(n,t)),he(e,s,t,-1),P(n,t)};R(a)?i(o):j(n,i),A(n)&&(ge(e,s,-1),A(P(t,s))&&(ue(e,-1),P(g,e)))},ue=(e,t)=>Se(n,e,t),ge=(e,t,s)=>Se(B(o,e,F()),t,s),he=(e,t,s,n)=>Se(B(B(a,e,F()),t,F()),s,n),fe=(e,t,s,n)=>B(B(B(d,e,F()),t,F()),s,n),Le=(e,t,s)=>{const n=O(O(d,e),t),o=Ce(e,t,s);return M(n,s)?[!0,O(n,s),o]:[!1,o,o]},pe=e=>{const t=A(y[e])&&A(v[e])&&A(f[e]),s=A(S[e])&&A(I[e])&&A(L[e])&&A(h[e]);if(t&&s)return;const r=e?[W(n),W(o,W),W(a,(e=>W(e,W))),W(d,(e=>W(e,W)))]:[n,o,a,d];if(t||(D(r[2],((t,s)=>D(t,((t,n)=>{A(t)||k(y[e],[s,n])})))),D(r[1],((t,s)=>{A(t)||k(v[e],[s])})),A(r[0])||k(f[e])),!s){let t;D(r[3],((s,n)=>{let o;D(s,((s,r)=>{let a;D(s,((s,i)=>{const d=Ce(n,r,i);d!==s&&(k(S[e],[n,r,i],d,s,Le),t=o=a=1)})),a&&k(I[e],[n,r],Le)})),o&&k(L[e],[n],Le)})),t&&k(h[e],[],Le)}},we=()=>H(g,(e=>H(e,H))),Re=()=>N(g),Te=e=>N(O(g,e)),be=(e,t)=>N(O(O(g,e),t)),Ce=(e,t,s)=>O(O(O(g,e),t),s),me=e=>((e=>ye(e,G))(e)&&Me((()=>X(e))),Ae),ke=()=>(Me((()=>X({}))),Ae),Me=e=>{if(-1==s)return;s++;const t=e();return s--,0==s&&(s=1,pe(1),s=-1,pe(0),s=0,c([d,n,o,a],x)),t},Ae={getTables:we,getTableIds:Re,getTable:e=>H(O(g,e),H),getRowIds:Te,getRow:(e,t)=>H(O(O(g,e),t)),getCellIds:be,getCell:Ce,hasTables:()=>!A(g),hasTable:e=>M(g,e),hasRow:(e,t)=>M(O(g,e),t),hasCell:(e,t,s)=>M(O(O(g,e),t),s),getJson:()=>p(g),getSchemaJson:()=>p(l),setTables:me,setTable:(e,t)=>(G(t,e)&&Me((()=>Z(e,t))),Ae),setRow:(e,t,s)=>(K(e,s)&&oe(e,t,s),Ae),addRow:(e,t)=>{let s;return K(e,t)&&(s=ae(O(g,e)),oe(e,s,t)),s},setPartialRow:(e,t,s)=>(K(e,s,1)&&Me((()=>{const n=ie(e);ne(s,((s,o)=>re(e,n,t,o,s)))})),Ae),setCell:(e,t,s,n)=>(T(Q(e,s,C(n)?n(Ce(e,t,s)):n),(n=>Me((()=>re(e,ie(e),t,s,n))))),Ae),setJson:e=>{try{"{}"===e?ke():me(w(e))}catch{}return Ae},setSchema:t=>{if((e=(e=>ye(e,(e=>ye(e,(e=>{if(!ye(e,((e,t)=>i(["type","default"],t))))return!1;const t=e.type;return!(!b(t)&&t!=r||(Ie(e.default)!=t&&se(e,"default"),0))})))))(t))&&(V(t),!A(g))){const e=we();ke(),me(e)}return Ae},delTables:ke,delTable:e=>(M(g,e)&&Me((()=>de(e))),Ae),delRow:(e,t)=>(T(O(g,e),(s=>{M(s,t)&&Me((()=>le(e,s,t)))})),Ae),delCell:(e,t,s,n)=>(T(O(g,e),(o=>T(O(o,t),(r=>{M(r,s)&&Me((()=>ce(e,o,t,r,s,n)))})))),Ae),delSchema:()=>(V({}),e=!1,Ae),transaction:Me,forEachTable:e=>D(g,((t,s)=>e(s,(e=>D(t,((t,s)=>e(s,(e=>j(t,e))))))))),forEachRow:(e,t)=>D(O(g,e),((e,s)=>t(s,(t=>j(e,t))))),forEachCell:(e,t,s)=>j(O(O(g,e),t),s),addTablesListener:(e,t)=>m(e,h[t?1:0]),addTableIdsListener:(e,t)=>m(e,f[t?1:0]),addTableListener:(e,t,s)=>m(t,L[s?1:0],[e]),addRowIdsListener:(e,t,s)=>m(t,v[s?1:0],[e]),addRowListener:(e,t,s,n)=>m(s,I[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>m(s,y[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>m(n,S[o?1:0],[e,t,s]),callListener:e=>(J(e,[Re,Te,be],(e=>R(e[2])?[]:[,,].fill(Ce(...e)))),Ae),delListener:e=>(E(e),Ae),getListenerStats:()=>({})};return ee(Ae)};export{oe as createCheckpoints,le as createCustomPersister,fe as createFilePersister,ae as createIndexes,ge as createLocalPersister,de as createMetrics,we as createRelationships,pe as createRemotePersister,he as createSessionPersister,Re as createStore,re as defaultSorter};
1
+ import{promises as e,watch as t}from"fs";const s=e=>typeof e,n=s(""),o=s(!0),r=s(0),a=s(s),i=(e,t)=>e.includes(t),d=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),l=(e,t)=>e.sort(t),c=(e,t)=>e.forEach(t),u=e=>f(e,((e,t)=>e+t),0),h=e=>e.length,g=e=>0==h(e),f=(e,t,s)=>e.reduce(t,s),L=e=>e.slice(1),p=e=>JSON.stringify(e,((e,t)=>y(t,Map)?f([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),w=JSON.parse,v=Math.max,I=Math.min,S=isFinite,y=(e,t)=>e instanceof t,R=e=>null==e,T=(e,t,s)=>R(e)?s?.():t(e),b=e=>e==n||e==o,C=e=>s(e)==a,m=()=>{},E=e=>e.size,k=(e,t)=>e?.has(t)??!1,M=e=>R(e)||0==E(e),A=e=>[...e?.values()??[]],x=e=>e.clear(),D=(e,t)=>e?.forEach(t),J=(e,t)=>e?.delete(t),F=e=>new Map(e),z=(e=F)=>[e(),e()],N=e=>[...e?.keys()??[]],O=(e,t)=>e?.get(t),j=(e,t)=>D(e,((e,s)=>t(s,e))),P=(e,t,s)=>R(s)?(J(e,t),e):e?.set(t,s),B=(e,t,s,n)=>(k(e,t)||(n?.(s),e.set(t,s)),O(e,t)),H=(e,t)=>{const s={},n=t??(e=>e);return D(e,((e,t)=>s[t]=n(e))),s},W=(e,t)=>{const s=F(),n=t??(e=>e);return D(e,((e,t)=>s.set(t,n(e)))),s},q=e=>new Set(e),G=(e,t)=>e?.add(t),K=(e,t,s)=>{const n=e.hasRow,o=F(),r=F(),a=F(),i=F(),d=F(),l=t=>T(O(d,t),(s=>{D(s,e.delListener),P(d,t)})),u=e=>{P(o,e),P(r,e),P(a,e),P(i,e),l(e)};return[()=>e,()=>N(o),e=>j(r,e),e=>k(r,e),e=>O(o,e),e=>O(r,e),(e,t)=>P(r,e,t),(u,h,g,f,L)=>{const p=F(),w=F();P(o,u,h),k(r,u)||(P(r,u,t()),P(a,u,F()),P(i,u,F()));const v=O(a,u),I=O(i,u),S=t=>{const o=s=>e.getCell(h,t,s),r=O(v,t),a=n(h,t)?s(f(o,t)):void 0;if(r!=a&&P(p,t,[r,a]),!R(L)){const e=O(I,t),s=n(h,t)?L(o,t):void 0;e!=s&&P(w,t,s)}},y=e=>{g((()=>{D(p,(([,e],t)=>P(v,t,e))),D(w,((e,t)=>P(I,t,e)))}),p,w,v,I,e),x(p),x(w)};j(v,S),e.hasTable(h)&&c(e.getRowIds(h),(e=>{k(v,e)||S(e)})),y(!0),l(u),P(d,u,q([e.addRowListener(h,null,((e,t,s)=>S(s))),e.addTableListener(h,(()=>y()))]))},u,()=>j(d,u)]},Q=(e,t)=>s(e)==n?t=>t(e):e??(()=>t??""),U=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},V=(e,t,s)=>h(s)<2?G(g(s)?e:B(e,s[0],q()),t):V(B(e,s[0],F()),t,L(s)),X=e=>{const t=(s,n,...o)=>T(s,(s=>g(o)?e(s,n):c([o[0],null],(e=>t(O(s,e),n,...L(o))))));return t},Y=e=>{let t,s=0;const n=[],o=F();return[(r,a,i=[])=>{t??=e();const d=n.pop()??""+s++;return P(o,d,[r,a,i]),V(a,d,i),d},(e,s=[],...n)=>X(D)(e,(e=>T(O(o,e),(([e])=>e(t,...s,...n)))),...s),e=>T(O(o,e),(([,t,s])=>(X(J)(t,e,...s),P(o,e),h(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>T(O(o,e),(([e,,o])=>{const r=(...a)=>{const i=h(a);i==h(o)?e(t,...a,...n(a)):R(o[i])?c(s[i](...a),(e=>r(...a,e))):r(...a,o[i])};r()}))]},Z=Object,$=Z.keys,_=Z.isFrozen,ee=Z.freeze,te=(e,t)=>!R(((e,t)=>T(e,(e=>e[t])))(e,t)),se=(e,t)=>delete e[t],ne=(e,t)=>c(Z.entries(e),(([e,s])=>t(s,e))),oe=U((e=>{let t,s,n,o=100,r=F(),a=1;const d=q(),l=F(),[u,f,L]=Y((()=>W)),p=F(),w=F(),v=[],I=[],S=(t,s)=>{a=0,e.transaction((()=>D(O(p,s),((s,n)=>D(s,((s,o)=>D(s,((s,r)=>R(s[t])?e.delCell(n,o,r,!0):e.setCell(n,o,r,s[t]))))))))),a=1},y=e=>{P(p,e),P(w,e),f(l,[e])},b=(e,t)=>c(((e,t)=>e.splice(0,t))(e,t??h(e)),y),C=()=>b(v,h(v)-o),m=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(a){T(t,(()=>{v.push(t),C(),b(I),t=void 0,n=1}));const e=B(r,s,F()),a=B(e,o,F()),c=B(a,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&M(P(a,i))&&M(P(e,o))&&M(P(r,s))&&(t=v.pop(),n=1),J()}})),E=(e="")=>(R(t)&&(t=""+s++,P(p,t,r),N(t,e),r=F(),n=1),t),A=()=>{g(v)||(I.unshift(E()),S(0,t),t=v.pop(),n=1)},x=()=>{g(I)||(v.push(t),t=I.shift(),S(1,t),n=1)},J=()=>{n&&(f(d),n=0)},z=e=>{const t=E(e);return J(),t},N=(e,t)=>(H(e)&&O(w,e)!==t&&(P(w,e,t),f(l,[e])),W),H=e=>k(p,e),W={setSize:e=>(o=e,C(),W),addCheckpoint:z,setCheckpoint:N,getStore:()=>e,getCheckpointIds:()=>[[...v],t,[...I]],forEachCheckpoint:e=>j(w,e),hasCheckpoint:H,getCheckpoint:e=>O(w,e),goBackward:()=>(A(),J(),W),goForward:()=>(x(),J(),W),goTo:e=>{const s=i(v,e)?A:i(I,e)?x:null;for(;!R(s)&&e!=t;)s();return J(),W},addCheckpointIdsListener:e=>u(e,d),addCheckpointListener:(e,t)=>u(t,l,[e]),delListener:e=>(L(e),W),clear:()=>(b(v),b(I),R(t)||y(t),t=void 0,s=0,z(),W),destroy:()=>{e.delListener(m)},getListenerStats:()=>({})};return ee(W.clear())})),re=(e,t)=>e<t?-1:1,ae=U((e=>{const t=F(),s=F(),[n,o,r,a,i,c,u,h,g,f]=K(e,F,(e=>R(e)?"":e+"")),[L,p,w]=Y((()=>I)),v=(t,s,n)=>{const o=i(t);D(n,((t,n)=>s(n,(s=>D(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},I={setIndexDefinition:(e,n,o,r,a,i=re)=>{const g=R(a)?void 0:([e],[t])=>a(e,t);return h(e,n,((n,o,a,h,f,L)=>{let w=0;const v=q(),I=q(),S=c(e);if(D(o,(([e,t],s)=>{R(e)||(G(v,e),T(O(S,e),(t=>{J(t,s),M(t)&&(P(S,e),w=1)}))),R(t)||(G(v,t),k(S,t)||(P(S,t,q()),w=1),G(O(S,t),s),R(r)||G(I,t))})),n(),M(f)||(L?j(S,(e=>G(I,e))):j(a,(e=>T(O(h,e),(e=>G(I,e))))),D(I,(e=>{const t=(t,s)=>i(O(f,t),O(f,s),e),s=[...O(S,e)];d(s,t)||(P(S,e,q(l(s,t))),G(v,e))}))),(w||L)&&!R(g)){const t=[...S];d(t,g)||(u(e,F(l(t,g))),w=1)}w&&p(t,[e]),D(v,(t=>p(s,[e,t])))}),Q(o),T(r,Q)),I},delIndexDefinition:e=>(g(e),I),getStore:n,getIndexIds:o,forEachIndex:e=>r(((t,s)=>e(t,(e=>v(t,e,s))))),forEachSlice:(e,t)=>v(e,t,c(e)),hasIndex:a,hasSlice:(e,t)=>k(c(e),t),getTableId:i,getSliceIds:e=>N(c(e)),getSliceRowIds:(e,t)=>A(O(c(e),t)),addSliceIdsListener:(e,s)=>L(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>L(n,s,[e,t]),delListener:e=>(w(e),I),destroy:f,getListenerStats:()=>({})};return ee(I)})),ie=F([["avg",[(e,t)=>u(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>v(...e),(e,t)=>v(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:v(t,e)]],["min",[e=>I(...e),(e,t)=>I(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:I(t,e)]],["sum",[e=>u(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),de=U((e=>{const t=F(),[s,n,o,r,a,i,d,l,c,u]=K(e,m,(e=>isNaN(e)||R(e)||!0===e||!1===e||""===e?void 0:1*e)),[h,g,f]=Y((()=>L)),L={setMetricDefinition:(e,s,n,o,r,a,c)=>{const u=C(n)?[n,r,a,c]:O(ie,n)??O(ie,"sum");return l(e,s,((s,n,o,r,a,l)=>{let c=i(e),h=E(r);const[f,L,p,w]=u;l=l||R(c),D(n,(([e,t])=>{l||(c=R(e)?L?.(c,t,h++):R(t)?p?.(c,e,h--):w?.(c,t,e,h)),l=l||R(c)})),s(),M(r)?c=void 0:l&&(c=f(A(r),E(r))),S(c)||(c=void 0);const v=i(e);c!=v&&(d(e,c),g(t,[e],c,v))}),Q(o,1)),L},delMetricDefinition:e=>(c(e),L),getStore:s,getMetricIds:n,forEachMetric:o,hasMetric:r,getTableId:a,getMetric:i,addMetricListener:(e,s)=>h(s,t,[e]),delListener:e=>(f(e),L),destroy:u,getListenerStats:()=>({})};return ee(L)})),le=(e,t,s,n,o)=>{let r,a=0;const i={load:async s=>{if(2!=a){a=1;const n=await t();R(n)||""==n?e.setTables(s):e.setJson(n),a=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=a&&(a=2,await s(e.getJson()),a=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),r=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(T(r,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return ee(i)},ce=globalThis.window,ue=(e,t,s)=>{let n;return le(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},ce.addEventListener("storage",n)}),(()=>{ce.removeEventListener("storage",n),n=void 0}))},he=(e,t)=>ue(e,t,localStorage),ge=(e,t)=>ue(e,t,sessionStorage),fe=(s,n)=>{let o;return le(s,(async()=>{try{return await e.readFile(n,"utf8")}catch{}}),(async t=>{try{await e.writeFile(n,t,"utf8")}catch{}}),(e=>{o=t(n,e)}),(()=>{o?.close(),o=void 0}))},Le=e=>e.headers.get("ETag"),pe=(e,t,s,n)=>{let o,r;return le(e,(async()=>{const e=await fetch(t);return r=Le(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=Le(s);R(r)||R(n)||n==r||(r=n,e())}),1e3*n)}),(()=>{T(o,clearInterval),o=void 0}))},we=U((e=>{const t=F(),s=F(),n=F(),o=F(),[r,a,i,d,l,c,u,h,g,f]=K(e,(()=>[F(),F(),F(),F()]),(e=>R(e)?void 0:e+"")),[L,p,w]=Y((()=>y)),v=(e,t,s)=>T(c(e),(([n,,o])=>{if(!k(o,t)){const r=q();if(l(e)!=S(e))G(r,t);else{let e=t;for(;!R(e)&&!k(r,e);)G(r,e),e=O(n,e)}if(s)return r;P(o,t,r)}return O(o,t)})),I=(e,t)=>T(c(e),(([,,e])=>P(e,t))),S=e=>O(t,e),y={setRelationshipDefinition:(e,r,a,i)=>(P(t,e,a),h(e,r,((t,r)=>{const a=q(),i=q(),d=q(),[l,u]=c(e);D(r,(([t,s],n)=>{R(t)||(G(i,t),T(O(u,t),(e=>{J(e,n),M(e)&&P(u,t)}))),R(s)||(G(i,s),k(u,s)||P(u,s,q()),G(O(u,s),n)),G(a,n),P(l,n,s),j(O(o,e),(t=>{k(v(e,t),n)&&G(d,t)}))})),t(),D(a,(t=>p(s,[e,t]))),D(i,(t=>p(n,[e,t]))),D(d,(t=>{I(e,t),p(o,[e,t])}))}),Q(i)),y),delRelationshipDefinition:e=>(P(t,e),g(e),y),getStore:r,getRelationshipIds:a,forEachRelationship:t=>i((s=>t(s,(t=>e.forEachRow(l(s),t))))),hasRelationship:d,getLocalTableId:l,getRemoteTableId:S,getRemoteRowId:(e,t)=>O(c(e)?.[0],t),getLocalRowIds:(e,t)=>A(O(c(e)?.[1],t)),getLinkedRowIds:(e,t)=>R(c(e))?[t]:A(v(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>L(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>L(s,n,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(v(e,t),L(s,o,[e,t])),delListener:e=>(I(...w(e)),y),destroy:f,getListenerStats:()=>({})};return ee(y)})),ve=(e,t,s,n=P)=>{const o=(r=N(e),a=e=>!te(t,e),r.filter(a));var r,a;return c($(t),(n=>s(e,n,t[n]))),c(o,(t=>n(e,t))),e},Ie=e=>{const t=s(e);return b(t)||t==r&&S(e)?t:void 0},Se=(e,t)=>!(R(e)||!(e=>y(e,Z)&&e.constructor==Z)(e)||_(e))&&(ne(e,((s,n)=>{t(s,n)||se(e,n)})),!(e=>g($(e)))(e)),ye=(e,t,s)=>P(e,t,O(e,t)==-s?void 0:s),Re=()=>{let e,t=0,s=0;const n=F(),o=F(),a=F(),d=F(),l=F(),u=F(),h=F(),g=z(q),f=z(q),L=z(),v=z(),I=z(),S=z(),y=z(),[m,E,A,J]=Y((()=>Me)),G=(t,s)=>(!e||k(l,s))&&Se(t,(e=>K(s,e))),K=(e,t,s)=>Se(s?t:U(t,e),((s,n)=>T(Q(e,n,s),(e=>(t[n]=e,!0)),(()=>!1)))),Q=(t,s,n)=>e?T(O(O(l,t),s),(e=>Ie(n)!=e.type?e.default:n)):R(Ie(n))?void 0:n,U=(e,t)=>(T(O(u,t),(t=>ne(t,((t,s)=>{te(e,s)||(e[s]=t)})))),e),V=e=>ve(l,e,((e,t,s)=>{const n={};ve(B(l,t,F()),s,((e,t,s)=>{P(e,t,s),T(s.default,(e=>n[t]=e))})),P(u,t,n)}),((e,t)=>{P(l,t),P(u,t)})),X=e=>ve(h,e,((e,t,s)=>Z(t,s)),((e,t)=>de(t))),Z=(e,t)=>ve(B(h,e,F(),(()=>ue(e,1))),t,((t,s,n)=>$(e,t,s,n)),((t,s)=>le(e,t,s))),$=(e,t,s,n,o)=>ve(B(t,s,F(),(()=>he(e,s,1))),n,((t,n,o)=>_(e,s,t,n,o)),((n,r)=>ce(e,t,s,n,r,o))),_=(e,t,s,n,o)=>{k(s,n)||ge(e,t,n,1);const r=O(s,n);o!==r&&(fe(e,t,n,r),P(s,n,o))},oe=(e,t,s)=>ke((()=>$(e,ie(e),t,s))),re=(e,t,s,n,o)=>T(O(t,s),(t=>_(e,s,t,n,o)),(()=>$(e,t,s,U({[n]:o},e)))),ae=e=>{const s=""+t++;return k(e,s)?ae(e):s},ie=e=>O(h,e)??Z(e,{}),de=e=>Z(e,{}),le=(e,t,s)=>$(e,t,s,{},!0),ce=(e,t,s,n,o,r)=>{const a=O(u,e)?.[o];if(!R(a)&&!r)return _(e,s,n,o,a);const i=t=>{fe(e,s,t,O(n,t)),ge(e,s,t,-1),P(n,t)};R(a)?i(o):j(n,i),M(n)&&(he(e,s,-1),M(P(t,s))&&(ue(e,-1),P(h,e)))},ue=(e,t)=>ye(n,e,t),he=(e,t,s)=>ye(B(o,e,F()),t,s),ge=(e,t,s,n)=>ye(B(B(a,e,F()),t,F()),s,n),fe=(e,t,s,n)=>B(B(B(d,e,F()),t,F()),s,n),Le=(e,t,s)=>{const n=O(O(d,e),t),o=Ce(e,t,s);return k(n,s)?[!0,O(n,s),o]:[!1,o,o]},pe=e=>{const t=M(S[e])&&M(v[e])&&M(f[e]),s=M(y[e])&&M(I[e])&&M(L[e])&&M(g[e]);if(t&&s)return;const r=e?[W(n),W(o,W),W(a,(e=>W(e,W))),W(d,(e=>W(e,W)))]:[n,o,a,d];if(t||(D(r[2],((t,s)=>D(t,((t,n)=>{M(t)||E(S[e],[s,n])})))),D(r[1],((t,s)=>{M(t)||E(v[e],[s])})),M(r[0])||E(f[e])),!s){let t;D(r[3],((s,n)=>{let o;D(s,((s,r)=>{let a;D(s,((s,i)=>{const d=Ce(n,r,i);d!==s&&(E(y[e],[n,r,i],d,s,Le),t=o=a=1)})),a&&E(I[e],[n,r],Le)})),o&&E(L[e],[n],Le)})),t&&E(g[e],[],Le)}},we=()=>H(h,(e=>H(e,H))),Re=()=>N(h),Te=e=>N(O(h,e)),be=(e,t)=>N(O(O(h,e),t)),Ce=(e,t,s)=>O(O(O(h,e),t),s),me=e=>((e=>Se(e,G))(e)&&ke((()=>X(e))),Me),Ee=()=>(ke((()=>X({}))),Me),ke=e=>{if(-1==s)return;s++;const t=e();return s--,0==s&&(s=1,pe(1),s=-1,pe(0),s=0,c([d,n,o,a],x)),t},Me={getTables:we,getTableIds:Re,getTable:e=>H(O(h,e),H),getRowIds:Te,getRow:(e,t)=>H(O(O(h,e),t)),getCellIds:be,getCell:Ce,hasTables:()=>!M(h),hasTable:e=>k(h,e),hasRow:(e,t)=>k(O(h,e),t),hasCell:(e,t,s)=>k(O(O(h,e),t),s),getJson:()=>p(h),getSchemaJson:()=>p(l),setTables:me,setTable:(e,t)=>(G(t,e)&&ke((()=>Z(e,t))),Me),setRow:(e,t,s)=>(K(e,s)&&oe(e,t,s),Me),addRow:(e,t)=>{let s;return K(e,t)&&(s=ae(O(h,e)),oe(e,s,t)),s},setPartialRow:(e,t,s)=>(K(e,s,1)&&ke((()=>{const n=ie(e);ne(s,((s,o)=>re(e,n,t,o,s)))})),Me),setCell:(e,t,s,n)=>(T(Q(e,s,C(n)?n(Ce(e,t,s)):n),(n=>ke((()=>re(e,ie(e),t,s,n))))),Me),setJson:e=>{try{"{}"===e?Ee():me(w(e))}catch{}return Me},setSchema:t=>{if((e=(e=>Se(e,(e=>Se(e,(e=>{if(!Se(e,((e,t)=>i(["type","default"],t))))return!1;const t=e.type;return!(!b(t)&&t!=r||(Ie(e.default)!=t&&se(e,"default"),0))})))))(t))&&(V(t),!M(h))){const e=we();Ee(),me(e)}return Me},delTables:Ee,delTable:e=>(k(h,e)&&ke((()=>de(e))),Me),delRow:(e,t)=>(T(O(h,e),(s=>{k(s,t)&&ke((()=>le(e,s,t)))})),Me),delCell:(e,t,s,n)=>(T(O(h,e),(o=>T(O(o,t),(r=>{k(r,s)&&ke((()=>ce(e,o,t,r,s,n)))})))),Me),delSchema:()=>(V({}),e=!1,Me),transaction:ke,forEachTable:e=>D(h,((t,s)=>e(s,(e=>D(t,((t,s)=>e(s,(e=>j(t,e))))))))),forEachRow:(e,t)=>D(O(h,e),((e,s)=>t(s,(t=>j(e,t))))),forEachCell:(e,t,s)=>j(O(O(h,e),t),s),addTablesListener:(e,t)=>m(e,g[t?1:0]),addTableIdsListener:(e,t)=>m(e,f[t?1:0]),addTableListener:(e,t,s)=>m(t,L[s?1:0],[e]),addRowIdsListener:(e,t,s)=>m(t,v[s?1:0],[e]),addRowListener:(e,t,s,n)=>m(s,I[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>m(s,S[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>m(n,y[o?1:0],[e,t,s]),callListener:e=>(J(e,[Re,Te,be],(e=>R(e[2])?[]:[,,].fill(Ce(...e)))),Me),delListener:e=>(A(e),Me),getListenerStats:()=>({})};return ee(Me)};export{oe as createCheckpoints,le as createCustomPersister,fe as createFilePersister,ae as createIndexes,he as createLocalPersister,de as createMetrics,we as createRelationships,pe as createRemotePersister,ge as createSessionPersister,Re as createStore,re as defaultSorter};
Binary file
@@ -1 +1 @@
1
- var e,t;e=this,t=function(e){"use strict";const t=(e,t)=>e.includes(t),n=(e,t)=>e.forEach(t),o=e=>e.length,s=e=>0==o(e),i=e=>e.slice(1),r=e=>null==e,l=(e,t,n)=>r(e)?n?.():t(e),c=(e,t)=>e?.has(t)??!1,d=e=>r(e)||0==(e=>e.size)(e),p=(e,t)=>e?.forEach(t),a=(e,t)=>e?.delete(t),u=e=>new Map(e),h=(e,t)=>e?.get(t),f=(e,t,n)=>r(n)?(a(e,t),e):e?.set(t,n),g=(e,t,n,o)=>(c(e,t)||(o?.(n),e.set(t,n)),h(e,t)),C=e=>new Set(e),k=(e,t,n)=>o(n)<2?((e,t)=>e?.add(t))(s(n)?e:g(e,n[0],C()),t):k(g(e,n[0],u()),t,i(n)),y=e=>{const t=(o,r,...c)=>l(o,(o=>s(c)?e(o,r):n([c[0],null],(e=>t(h(o,e),r,...i(c))))));return t},v=Object.freeze,L=(e=>{const t=new WeakMap;return n=>(t.has(n)||t.set(n,e(n)),t.get(n))})((e=>{let i,L,b,w=100,S=u(),T=1;const j=C(),x=u(),[z,M,m]=(e=>{let t,s=0;const i=[],c=u();return[(n,o,r=[])=>{t??=e();const l=i.pop()??""+s++;return f(c,l,[n,o,r]),k(o,l,r),l},(e,n=[],...o)=>y(p)(e,(e=>l(h(c,e),(([e])=>e(t,...n,...o)))),...n),e=>l(h(c,e),(([,t,n])=>(y(a)(t,e,...n),f(c,e),o(i)<1e3&&i.push(e),n)),(()=>[])),(e,s,i)=>l(h(c,e),(([e,,l])=>{const c=(...d)=>{const p=o(d);p==o(l)?e(t,...d,...i(d)):r(l[p])?n(s[p](...d),(e=>c(...d,e))):c(...d,l[p])};c()}))]})((()=>Q)),B=u(),E=u(),I=[],O=[],_=(t,n)=>{T=0,e.transaction((()=>p(h(B,n),((n,o)=>p(n,((n,s)=>p(n,((n,i)=>r(n[t])?e.delCell(o,s,i,!0):e.setCell(o,s,i,n[t]))))))))),T=1},F=e=>{f(B,e),f(E,e),M(x,[e])},P=(e,t)=>n(((e,t)=>e.splice(0,t))(e,t??o(e)),F),W=()=>P(I,o(I)-w),q=e.addCellListener(null,null,null,((e,t,n,o,s,r)=>{if(T){l(i,(()=>{I.push(i),W(),P(O),i=void 0,b=1}));const e=g(S,t,u()),c=g(e,n,u()),p=g(c,o,[void 0,void 0],(e=>e[0]=r));p[1]=s,p[0]===p[1]&&d(f(c,o))&&d(f(e,n))&&d(f(S,t))&&(i=I.pop(),b=1),H()}})),A=(e="")=>(r(i)&&(i=""+L++,f(B,i,S),K(i,e),S=u(),b=1),i),D=()=>{s(I)||(O.unshift(A()),_(0,i),i=I.pop(),b=1)},G=()=>{s(O)||(I.push(i),i=O.shift(),_(1,i),b=1)},H=()=>{b&&(M(j),b=0)},J=e=>{const t=A(e);return H(),t},K=(e,t)=>(N(e)&&h(E,e)!==t&&(f(E,e,t),M(x,[e])),Q),N=e=>c(B,e),Q={setSize:e=>(w=e,W(),Q),addCheckpoint:J,setCheckpoint:K,getStore:()=>e,getCheckpointIds:()=>[[...I],i,[...O]],hasCheckpoint:N,getCheckpoint:e=>h(E,e),goBackward:()=>(D(),H(),Q),goForward:()=>(G(),H(),Q),goTo:e=>{const n=t(I,e)?D:t(O,e)?G:null;for(;!r(n)&&e!=i;)n();return H(),Q},addCheckpointIdsListener:e=>z(e,j),addCheckpointListener:(e,t)=>z(t,x,[e]),delListener:e=>(m(e),Q),clear:()=>(P(I),P(O),r(i)||F(i),i=void 0,L=0,J(),Q),destroy:()=>{e.delListener(q)},getListenerStats:()=>({})};return v(Q.clear())}));e.createCheckpoints=L,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBaseCheckpoints={});
1
+ var e,t;e=this,t=function(e){"use strict";const t=(e,t)=>e.includes(t),n=(e,t)=>e.forEach(t),o=e=>e.length,s=e=>0==o(e),r=e=>e.slice(1),i=e=>null==e,c=(e,t,n)=>i(e)?n?.():t(e),l=(e,t)=>e?.has(t)??!1,d=e=>i(e)||0==(e=>e.size)(e),a=(e,t)=>e?.forEach(t),p=(e,t)=>e?.delete(t),u=e=>new Map(e),h=(e,t)=>e?.get(t),f=(e,t,n)=>i(n)?(p(e,t),e):e?.set(t,n),C=(e,t,n,o)=>(l(e,t)||(o?.(n),e.set(t,n)),h(e,t)),g=e=>new Set(e),k=(e,t,n)=>o(n)<2?((e,t)=>e?.add(t))(s(n)?e:C(e,n[0],g()),t):k(C(e,n[0],u()),t,r(n)),v=e=>{const t=(o,i,...l)=>c(o,(o=>s(l)?e(o,i):n([l[0],null],(e=>t(h(o,e),i,...r(l))))));return t},y=Object.freeze,L=(e=>{const t=new WeakMap;return n=>(t.has(n)||t.set(n,e(n)),t.get(n))})((e=>{let r,L,b,w=100,S=u(),T=1;const j=g(),x=u(),[z,E,M]=(e=>{let t,s=0;const r=[],l=u();return[(n,o,i=[])=>{t??=e();const c=r.pop()??""+s++;return f(l,c,[n,o,i]),k(o,c,i),c},(e,n=[],...o)=>v(a)(e,(e=>c(h(l,e),(([e])=>e(t,...n,...o)))),...n),e=>c(h(l,e),(([,t,n])=>(v(p)(t,e,...n),f(l,e),o(r)<1e3&&r.push(e),n)),(()=>[])),(e,s,r)=>c(h(l,e),(([e,,c])=>{const l=(...d)=>{const a=o(d);a==o(c)?e(t,...d,...r(d)):i(c[a])?n(s[a](...d),(e=>l(...d,e))):l(...d,c[a])};l()}))]})((()=>Q)),m=u(),B=u(),I=[],O=[],_=(t,n)=>{T=0,e.transaction((()=>a(h(m,n),((n,o)=>a(n,((n,s)=>a(n,((n,r)=>i(n[t])?e.delCell(o,s,r,!0):e.setCell(o,s,r,n[t]))))))))),T=1},F=e=>{f(m,e),f(B,e),E(x,[e])},P=(e,t)=>n(((e,t)=>e.splice(0,t))(e,t??o(e)),F),W=()=>P(I,o(I)-w),q=e.addCellListener(null,null,null,((e,t,n,o,s,i)=>{if(T){c(r,(()=>{I.push(r),W(),P(O),r=void 0,b=1}));const e=C(S,t,u()),l=C(e,n,u()),a=C(l,o,[void 0,void 0],(e=>e[0]=i));a[1]=s,a[0]===a[1]&&d(f(l,o))&&d(f(e,n))&&d(f(S,t))&&(r=I.pop(),b=1),H()}})),A=(e="")=>(i(r)&&(r=""+L++,f(m,r,S),K(r,e),S=u(),b=1),r),D=()=>{s(I)||(O.unshift(A()),_(0,r),r=I.pop(),b=1)},G=()=>{s(O)||(I.push(r),r=O.shift(),_(1,r),b=1)},H=()=>{b&&(E(j),b=0)},J=e=>{const t=A(e);return H(),t},K=(e,t)=>(N(e)&&h(B,e)!==t&&(f(B,e,t),E(x,[e])),Q),N=e=>l(m,e),Q={setSize:e=>(w=e,W(),Q),addCheckpoint:J,setCheckpoint:K,getStore:()=>e,getCheckpointIds:()=>[[...I],r,[...O]],forEachCheckpoint:e=>{return t=e,a(B,((e,n)=>t(n,e)));var t},hasCheckpoint:N,getCheckpoint:e=>h(B,e),goBackward:()=>(D(),H(),Q),goForward:()=>(G(),H(),Q),goTo:e=>{const n=t(I,e)?D:t(O,e)?G:null;for(;!i(n)&&e!=r;)n();return H(),Q},addCheckpointIdsListener:e=>z(e,j),addCheckpointListener:(e,t)=>z(t,x,[e]),delListener:e=>(M(e),Q),clear:()=>(P(I),P(O),i(r)||F(r),r=void 0,L=0,J(),Q),destroy:()=>{e.delListener(q)},getListenerStats:()=>({})};return y(Q.clear())}));e.createCheckpoints=L,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBaseCheckpoints={});
Binary file