@verdant-web/store 3.6.4 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/bundle/index.js +12 -12
  2. package/dist/bundle/index.js.map +3 -3
  3. package/dist/esm/DocumentManager.d.ts +1 -0
  4. package/dist/esm/DocumentManager.js +8 -3
  5. package/dist/esm/DocumentManager.js.map +1 -1
  6. package/dist/esm/__tests__/batching.test.js +67 -1
  7. package/dist/esm/__tests__/batching.test.js.map +1 -1
  8. package/dist/esm/__tests__/documents.test.js +16 -0
  9. package/dist/esm/__tests__/documents.test.js.map +1 -1
  10. package/dist/esm/__tests__/fixtures/testStorage.d.ts +3 -1
  11. package/dist/esm/__tests__/fixtures/testStorage.js +3 -2
  12. package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
  13. package/dist/esm/client/Client.d.ts +22 -1
  14. package/dist/esm/client/Client.js.map +1 -1
  15. package/dist/esm/client/ClientDescriptor.d.ts +7 -1
  16. package/dist/esm/client/ClientDescriptor.js +1 -0
  17. package/dist/esm/client/ClientDescriptor.js.map +1 -1
  18. package/dist/esm/entities/Entity.d.ts +16 -2
  19. package/dist/esm/entities/Entity.js +21 -4
  20. package/dist/esm/entities/Entity.js.map +1 -1
  21. package/dist/esm/entities/Entity.test.js +23 -2
  22. package/dist/esm/entities/Entity.test.js.map +1 -1
  23. package/dist/esm/entities/EntityMetadata.d.ts +2 -0
  24. package/dist/esm/entities/EntityMetadata.js +9 -1
  25. package/dist/esm/entities/EntityMetadata.js.map +1 -1
  26. package/dist/esm/entities/EntityStore.d.ts +1 -2
  27. package/dist/esm/entities/EntityStore.js +9 -7
  28. package/dist/esm/entities/EntityStore.js.map +1 -1
  29. package/dist/esm/entities/OperationBatcher.d.ts +25 -0
  30. package/dist/esm/entities/OperationBatcher.js +31 -3
  31. package/dist/esm/entities/OperationBatcher.js.map +1 -1
  32. package/dist/esm/entities/types.d.ts +7 -1
  33. package/dist/esm/metadata/Metadata.d.ts +3 -1
  34. package/dist/esm/metadata/Metadata.js +8 -1
  35. package/dist/esm/metadata/Metadata.js.map +1 -1
  36. package/dist/esm/queries/BaseQuery.d.ts +3 -0
  37. package/dist/esm/queries/BaseQuery.js +45 -11
  38. package/dist/esm/queries/BaseQuery.js.map +1 -1
  39. package/dist/esm/sync/Sync.d.ts +8 -2
  40. package/dist/esm/sync/Sync.js +6 -3
  41. package/dist/esm/sync/Sync.js.map +1 -1
  42. package/package.json +4 -3
  43. package/src/DocumentManager.ts +11 -2
  44. package/src/__tests__/batching.test.ts +78 -0
  45. package/src/__tests__/documents.test.ts +19 -0
  46. package/src/__tests__/fixtures/testStorage.ts +10 -1
  47. package/src/client/Client.ts +14 -1
  48. package/src/client/ClientDescriptor.ts +9 -0
  49. package/src/entities/Entity.test.ts +30 -2
  50. package/src/entities/Entity.ts +57 -10
  51. package/src/entities/EntityMetadata.ts +13 -3
  52. package/src/entities/EntityStore.ts +9 -9
  53. package/src/entities/OperationBatcher.ts +69 -2
  54. package/src/entities/types.ts +18 -1
  55. package/src/metadata/Metadata.ts +13 -0
  56. package/src/queries/BaseQuery.ts +50 -13
  57. package/src/sync/Sync.ts +12 -2
@@ -475,11 +475,15 @@ export class Entity<
475
475
  }
476
476
  };
477
477
 
478
+ private invalidateCachedView = () => {
479
+ this._viewData = undefined;
480
+ this.cachedView = undefined;
481
+ };
482
+
478
483
  private change = (ev: EntityChange) => {
479
484
  if (ev.oid === this.oid) {
480
485
  // reset cached view
481
- this._viewData = undefined;
482
- this.cachedView = undefined;
486
+ this.invalidateCachedView();
483
487
  if (!this.parent) {
484
488
  this.changeRoot(ev);
485
489
  } else {
@@ -751,15 +755,42 @@ export class Entity<
751
755
  }
752
756
  };
753
757
 
754
- set = <Key extends keyof Init>(key: Key, value: Init[Key]) => {
758
+ set = (
759
+ key: any,
760
+ value: any,
761
+ options?: {
762
+ /**
763
+ * Forces the creation of a change for this set even if the value is the same
764
+ * as the current value for this key. This has an effect in situations where
765
+ * offline changes from others are interleaved with local changes; when setting
766
+ * a value to its current value (force=true), if that value were retroactively changed from
767
+ * offline changes, the set would put it back to the value specified. If the
768
+ * set is ignored because the value is the same (force=false), then retroactive
769
+ * changes will be preserved.
770
+ */
771
+ force: boolean;
772
+ },
773
+ ) => {
755
774
  assertNotSymbol(key);
756
- this.addPendingOperations(
757
- this.patchCreator.createSet(
758
- this.oid,
759
- key,
760
- this.processInputValue(value, key),
761
- ),
762
- );
775
+ if (!options?.force && this.get(key) === value) return;
776
+
777
+ if (this.isList) {
778
+ this.addPendingOperations(
779
+ this.patchCreator.createListSet(
780
+ this.oid,
781
+ key,
782
+ this.processInputValue(value, key),
783
+ ),
784
+ );
785
+ } else {
786
+ this.addPendingOperations(
787
+ this.patchCreator.createSet(
788
+ this.oid,
789
+ key,
790
+ this.processInputValue(value, key),
791
+ ),
792
+ );
793
+ }
763
794
  };
764
795
 
765
796
  /**
@@ -998,6 +1029,17 @@ export class Entity<
998
1029
  this.view.forEach(callback);
999
1030
  };
1000
1031
 
1032
+ reduce = <U>(
1033
+ callback: (
1034
+ previousValue: U,
1035
+ currentValue: ListItemValue<KeyValue>,
1036
+ index: number,
1037
+ ) => U,
1038
+ initialValue: U,
1039
+ ): U => {
1040
+ return this.view.reduce(callback, initialValue);
1041
+ };
1042
+
1001
1043
  some = (predicate: (value: ListItemValue<KeyValue>) => boolean): boolean => {
1002
1044
  return this.view.some(predicate);
1003
1045
  };
@@ -1034,6 +1076,11 @@ export class Entity<
1034
1076
  return this.metadataFamily.get(oid).computeView(type === 'confirmed');
1035
1077
  };
1036
1078
  __getFamilyOids__ = () => this.metadataFamily.getAllOids();
1079
+
1080
+ __discardPendingOperation__ = (operation: Operation) => {
1081
+ this.metadataFamily.discardPendingOperation(operation);
1082
+ this.invalidateCachedView();
1083
+ };
1037
1084
  }
1038
1085
 
1039
1086
  function assertNotSymbol<T>(key: T): asserts key is Exclude<T, symbol> {
@@ -176,19 +176,24 @@ export class EntityMetadata {
176
176
  // FIXME: seems inefficient
177
177
  // remove this incoming op from pending if it's in there
178
178
  const pendingPrior = this.pendingOperations.length;
179
- this.pendingOperations = this.pendingOperations.filter(
180
- (pendingOp) => op.timestamp !== pendingOp.timestamp,
181
- );
179
+ this.discardPendingOperation(op);
182
180
  totalAdded -= pendingPrior - this.pendingOperations.length;
183
181
  }
184
182
  return totalAdded;
185
183
  };
186
184
 
187
185
  addPendingOperation = (operation: Operation) => {
186
+ // check to see if new operation supersedes the previous one
188
187
  // we can assume pending ops are always newer
189
188
  this.pendingOperations.push(operation);
190
189
  };
191
190
 
191
+ discardPendingOperation = (operation: Operation) => {
192
+ this.pendingOperations = this.pendingOperations.filter(
193
+ (op) => op.timestamp !== operation.timestamp,
194
+ );
195
+ };
196
+
192
197
  private applyOperations = (
193
198
  base: any,
194
199
  deleted: boolean,
@@ -361,4 +366,9 @@ export class EntityFamilyMetadata {
361
366
  }
362
367
  return Object.values(changes);
363
368
  };
369
+
370
+ discardPendingOperation = (operation: Operation) => {
371
+ const ent = this.entities.get(operation.oid);
372
+ ent?.discardPendingOperation(operation);
373
+ };
364
374
  }
@@ -24,7 +24,6 @@ import { OperationBatcher } from './OperationBatcher.js';
24
24
  import { QueryableStorage } from '../queries/QueryableStorage.js';
25
25
  import { WeakEvent } from 'weak-event';
26
26
  import { processValueFiles } from '../files/utils.js';
27
- import { abort } from 'process';
28
27
 
29
28
  enum AbortReason {
30
29
  Reset,
@@ -197,9 +196,6 @@ export class EntityStore extends Disposable {
197
196
  });
198
197
  });
199
198
  } else {
200
- if (this.cache.has(oid)) {
201
- this.ctx.log('debug', 'Cache has', oid, ', an event should follow.');
202
- }
203
199
  event.invoke(this, {
204
200
  oid,
205
201
  baselines,
@@ -214,13 +210,12 @@ export class EntityStore extends Disposable {
214
210
  };
215
211
 
216
212
  // then, asynchronously add to the database
213
+ // this also emits messages to sync
214
+ // TODO: could messages be sent to sync before storage,
215
+ // so that realtime is lower latency? What would happen
216
+ // if the storage failed?
217
217
  await this.meta.insertData(data, abortOptions);
218
218
 
219
- // FIXME: entities hydrated here are not seeing
220
- // the operations just inserted above!!
221
- // IDEA: can we coordinate here with hydrate promises
222
- // based on affected OIDs?
223
-
224
219
  // recompute all affected documents for querying
225
220
  const entities = await Promise.all(
226
221
  allDocumentOids.map(async (oid) => {
@@ -459,6 +454,11 @@ export class EntityStore extends Disposable {
459
454
  this.batcher.addOperations(operations);
460
455
  };
461
456
 
457
+ discardPendingOperation = (operation: Operation) => {
458
+ const root = getOidRoot(operation.oid);
459
+ this.cache.get(root)?.deref()?.__discardPendingOperation__(operation);
460
+ };
461
+
462
462
  /**
463
463
  * Loads initial Entity data from storage
464
464
  */
@@ -1,10 +1,14 @@
1
1
  import {
2
2
  Batcher,
3
+ ObjectIdentifier,
3
4
  Operation,
5
+ PropertyName,
4
6
  generateId,
5
7
  getOidRoot,
6
8
  getUndoOperations,
7
9
  groupPatchesByOid,
10
+ isSuperseded,
11
+ operationSupersedes,
8
12
  } from '@verdant-web/common';
9
13
  import { Metadata } from '../metadata/Metadata.js';
10
14
  import { Context } from '../context.js';
@@ -74,6 +78,40 @@ export class OperationBatcher {
74
78
  'to storage / sync',
75
79
  );
76
80
  if (!operations.length) return;
81
+
82
+ // next block of logic computes superseding rules to eliminate
83
+ // operations which are 'overshadowed' by later ones on the same
84
+ // key.
85
+
86
+ const committed: Operation[] = [];
87
+ const supersessions: Record<
88
+ ObjectIdentifier,
89
+ Set<boolean | PropertyName>
90
+ > = {};
91
+ for (let i = operations.length - 1; i >= 0; i--) {
92
+ const op = operations[i];
93
+
94
+ // check for supersession from later operation which either
95
+ // covers the whole id (true) or this key
96
+ const existingSupersession = supersessions[op.oid];
97
+ if (existingSupersession && isSuperseded(op, existingSupersession)) {
98
+ this.entities.discardPendingOperation(op);
99
+ continue;
100
+ }
101
+
102
+ // determine if this operation supersedes others
103
+ const supersession = operationSupersedes(op);
104
+ if (supersession !== false) {
105
+ if (!supersessions[op.oid]) {
106
+ supersessions[op.oid] = new Set<boolean | PropertyName>();
107
+ }
108
+ supersessions[op.oid]!.add(supersession);
109
+ }
110
+
111
+ // add this operation to final list
112
+ committed.unshift(op);
113
+ }
114
+
77
115
  // rewrite timestamps of all operations to now - this preserves
78
116
  // the linear history of operations which are sent to the server.
79
117
  // even if multiple batches are spun up in parallel and flushed
@@ -86,10 +124,14 @@ export class OperationBatcher {
86
124
  // despite the provisional timestamp being earlier
87
125
  // NOTE: this MUST be mutating the original operation object! this timestamp
88
126
  // also serves as a unique ID for deduplication later.
89
- for (const op of operations) {
127
+
128
+ // NOTE: need to rewind back in order to set timestamps correctly.
129
+ // cannot be done in reversed loop above or timestamps would be
130
+ // in reverse order.
131
+ for (const op of committed) {
90
132
  op.timestamp = this.meta.now;
91
133
  }
92
- await this.commitOperations(operations, meta);
134
+ await this.commitOperations(committed, meta);
93
135
  };
94
136
 
95
137
  /**
@@ -148,9 +190,34 @@ export class OperationBatcher {
148
190
  max = null,
149
191
  timeout = this.defaultBatchTimeout,
150
192
  }: {
193
+ /** Allows turning off undo for this batch, making it 'permanent' */
151
194
  undoable?: boolean;
195
+ /**
196
+ * Provide a stable name to any invocation of .batch() and the changes made
197
+ * within run() will all be added to the same batch. If a batch hits the max
198
+ * limit or timeout and is flushed, the name will be reused for a new batch
199
+ * automatically. Provide a stable name to make changes from anywhere in your
200
+ * app to be grouped together in the same batch with the same limit behavior.
201
+ *
202
+ * Limit configuration provided to each invocation of .batch() with the same
203
+ * name will overwrite any other invocation's limit configuration. It's
204
+ * recommended to provide limits in one place and only provide a name
205
+ * in others.
206
+ */
152
207
  batchName?: string;
208
+ /**
209
+ * The maximum number of operations the batch will hold before flushing
210
+ * automatically. If null, the batch will not flush automatically based
211
+ * on operation count.
212
+ */
153
213
  max?: number | null;
214
+ /**
215
+ * The number of milliseconds to wait before flushing the batch automatically.
216
+ * If null, the batch will not flush automatically based on time. It is not
217
+ * recommended to set this to null, as an unflushed batch will never be written
218
+ * to storage or sync. If you do require undefined timing in a batch, make sure
219
+ * to always call .commit() on the batch yourself.
220
+ */
154
221
  timeout?: number | null;
155
222
  } = {}): OperationBatch => {
156
223
  const internalBatch = this.batcher.add({
@@ -87,7 +87,11 @@ export interface ObjectEntity<
87
87
  readonly size: number;
88
88
  entries(): [string, Exclude<Value[keyof Value], undefined>][];
89
89
  values(): Exclude<Value[keyof Value], undefined>[];
90
- set<Key extends keyof Init>(key: Key, value: Init[Key]): void;
90
+ set<Key extends keyof Init>(
91
+ key: Key,
92
+ value: Init[Key],
93
+ options?: { force?: boolean },
94
+ ): void;
91
95
  delete(key: DeletableKeys<Value>): void;
92
96
  update(
93
97
  value: DeepPartial<Init>,
@@ -136,6 +140,11 @@ export interface ListEntity<
136
140
  BaseEntity<Init, Value, Snapshot> {
137
141
  readonly isList: true;
138
142
  readonly length: number;
143
+ set(
144
+ index: number,
145
+ value: ListItemInit<Init>,
146
+ options?: { force?: boolean },
147
+ ): void;
139
148
  push(value: ListItemInit<Init>): void;
140
149
  insert(index: number, value: ListItemInit<Init>): void;
141
150
  move(from: number, to: number): void;
@@ -149,6 +158,14 @@ export interface ListEntity<
149
158
  removeFirst(item: ListItemValue<Value>): void;
150
159
  removeLast(item: ListItemValue<Value>): void;
151
160
  map<U>(callback: (value: ListItemValue<Value>, index: number) => U): U[];
161
+ reduce<U>(
162
+ callback: (
163
+ accumulator: U,
164
+ currentValue: ListItemValue<Value>,
165
+ index: number,
166
+ ) => U,
167
+ initialValue: U,
168
+ ): U;
152
169
  filter(
153
170
  callback: (value: ListItemValue<Value>, index: number) => boolean,
154
171
  ): ListItemValue<Value>[];
@@ -60,12 +60,16 @@ export class Metadata extends EventSubscriber<{
60
60
 
61
61
  private context: Omit<Context, 'documentDb' | 'getNow'>;
62
62
 
63
+ private onOperation?: (operation: Operation) => void;
64
+
63
65
  constructor({
64
66
  disableRebasing,
65
67
  context,
68
+ onOperation,
66
69
  }: {
67
70
  disableRebasing?: boolean;
68
71
  context: Omit<Context, 'documentDb' | 'getNow'>;
72
+ onOperation?: (operation: Operation) => void;
69
73
  }) {
70
74
  super();
71
75
  this.context = context;
@@ -76,6 +80,7 @@ export class Metadata extends EventSubscriber<{
76
80
  this.ackInfo = new AckInfoStore(this.db);
77
81
  this.messageCreator = new MessageCreator(this);
78
82
  this.patchCreator = new PatchCreator(() => this.now);
83
+ this.onOperation = onOperation;
79
84
  if (disableRebasing) this.disableRebasing = disableRebasing;
80
85
  }
81
86
 
@@ -312,6 +317,10 @@ export class Metadata extends EventSubscriber<{
312
317
 
313
318
  // we can now enqueue and check for rebase opportunities
314
319
  this.tryAutonomousRebase();
320
+
321
+ if (this.onOperation) {
322
+ operations.forEach((o) => this.onOperation!(o));
323
+ }
315
324
  };
316
325
 
317
326
  /**
@@ -340,6 +349,10 @@ export class Metadata extends EventSubscriber<{
340
349
 
341
350
  this.ack(operations[operations.length - 1].timestamp);
342
351
 
352
+ if (this.onOperation) {
353
+ operations.forEach((o) => this.onOperation!(o));
354
+ }
355
+
343
356
  return affectedDocumentOids;
344
357
  };
345
358
 
@@ -42,6 +42,7 @@ export abstract class BaseQuery<T> extends Disposable {
42
42
 
43
43
  readonly collection;
44
44
  readonly key;
45
+ readonly isListQuery;
45
46
 
46
47
  constructor({
47
48
  initial,
@@ -53,6 +54,7 @@ export abstract class BaseQuery<T> extends Disposable {
53
54
  super();
54
55
  this._rawValue = initial;
55
56
  this._value = initial;
57
+ this.isListQuery = Array.isArray(initial);
56
58
  this._events = new EventSubscriber<BaseQueryEvents>(
57
59
  (event: keyof BaseQueryEvents) => {
58
60
  if (event === 'change') this._allUnsubscribedHandler?.(this);
@@ -98,6 +100,19 @@ export abstract class BaseQuery<T> extends Disposable {
98
100
  return this._status;
99
101
  }
100
102
 
103
+ private set status(v: QueryStatus) {
104
+ if (this._status === v) return;
105
+ this._status = v;
106
+ this._events.emit('statusChange', this._status);
107
+ }
108
+
109
+ get hasDeleted() {
110
+ if (this.isListQuery) {
111
+ return (this._rawValue as any[]).length !== (this._value as any[]).length;
112
+ }
113
+ return !!this._rawValue && !this._value;
114
+ }
115
+
101
116
  /**
102
117
  * Subscribe to changes in the query value.
103
118
  *
@@ -138,16 +153,38 @@ export abstract class BaseQuery<T> extends Disposable {
138
153
  protected setValue = (value: T) => {
139
154
  this._rawValue = value;
140
155
  this.subscribeToDeleteAndRestore(this._rawValue);
141
- this._value = filterResultSet(value);
142
- // validate the value
143
- if (
144
- Array.isArray(this._value) &&
145
- this._value.some((v) => v.getSnapshot() === null)
146
- ) {
147
- debugger;
156
+ const filtered = filterResultSet(value);
157
+
158
+ // prevent excess change notifications by diffing
159
+ // value by identity for single-value queries,
160
+ // and by item identity for multi-value
161
+ let changed = true;
162
+ // always fire change when going from initial to ready
163
+ if (this.status === 'initializing' || this.status === 'initial') {
164
+ changed = true;
165
+ } else {
166
+ // compare values by identity, after filtering.
167
+ if (this.isListQuery) {
168
+ if (
169
+ (this._value as any[]).length === (filtered as any[]).length &&
170
+ (this._value as any[]).every((v, i) => v === (filtered as any[])[i])
171
+ ) {
172
+ changed = false;
173
+ }
174
+ } else {
175
+ if (this._value === filtered) {
176
+ changed = false;
177
+ }
178
+ }
179
+ }
180
+
181
+ this._value = filtered;
182
+
183
+ if (changed) {
184
+ this.context.log('debug', 'Query value changed', this.key);
185
+ this._events.emit('change', this._value);
148
186
  }
149
- this._status = 'ready';
150
- this._events.emit('change', this._value);
187
+ this.status = 'ready';
151
188
  };
152
189
 
153
190
  // re-applies filtering if results have changed
@@ -186,10 +223,10 @@ export abstract class BaseQuery<T> extends Disposable {
186
223
  execute = () => {
187
224
  this.context.log('debug', 'Executing query', this.key);
188
225
 
189
- if (this._status === 'initial') {
190
- this._status = 'initializing';
191
- } else if (this._status === 'ready') {
192
- this._status = 'revalidating';
226
+ if (this.status === 'initial') {
227
+ this.status = 'initializing';
228
+ } else if (this.status === 'ready') {
229
+ this.status = 'revalidating';
193
230
  }
194
231
  // no status change needed if already in a 'running' status.
195
232
 
package/src/sync/Sync.ts CHANGED
@@ -173,6 +173,11 @@ export interface ServerSyncOptions<Profile = any, Presence = any>
173
173
  * but is not yet thoroughly vetted.
174
174
  */
175
175
  useBroadcastChannel?: boolean;
176
+ /**
177
+ * Listen for outgoing messages from the client to the server.
178
+ * Not sure why you want to do this, but be careful.
179
+ */
180
+ onOutgoingMessage?: (message: ClientMessage) => void;
176
181
  }
177
182
 
178
183
  export class ServerSync<Presence = any, Profile = any>
@@ -196,6 +201,8 @@ export class ServerSync<Presence = any, Profile = any>
196
201
 
197
202
  readonly presence: PresenceManager<Profile, Presence>;
198
203
 
204
+ private onOutgoingMessage?: (message: ClientMessage) => void;
205
+
199
206
  private log;
200
207
 
201
208
  constructor(
@@ -211,6 +218,7 @@ export class ServerSync<Presence = any, Profile = any>
211
218
  presenceUpdateBatchTimeout,
212
219
  defaultProfile,
213
220
  useBroadcastChannel,
221
+ onOutgoingMessage,
214
222
  }: ServerSyncOptions<Profile, Presence>,
215
223
  {
216
224
  meta,
@@ -230,6 +238,7 @@ export class ServerSync<Presence = any, Profile = any>
230
238
  this.meta = meta;
231
239
  this.onData = onData;
232
240
  this.log = ctx.log;
241
+ this.onOutgoingMessage = onOutgoingMessage;
233
242
  this.presence = new PresenceManager({
234
243
  initialPresence,
235
244
  defaultProfile,
@@ -444,9 +453,10 @@ export class ServerSync<Presence = any, Profile = any>
444
453
  return this.pushPullSync.interval;
445
454
  }
446
455
 
447
- send = (message: ClientMessage) => {
456
+ send = async (message: ClientMessage) => {
448
457
  if (this.activeSync.status === 'active') {
449
- return this.activeSync.send(message);
458
+ await this.activeSync.send(message);
459
+ this.onOutgoingMessage?.(message);
450
460
  }
451
461
  };
452
462