@verdant-web/store 4.2.0 → 4.4.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 (46) hide show
  1. package/dist/bundle/index.js +9 -7
  2. package/dist/bundle/index.js.map +4 -4
  3. package/dist/esm/__tests__/entities.test.js +438 -5
  4. package/dist/esm/__tests__/entities.test.js.map +1 -1
  5. package/dist/esm/__tests__/fixtures/testStorage.d.ts +1 -0
  6. package/dist/esm/__tests__/fixtures/testStorage.js +1 -1
  7. package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
  8. package/dist/esm/client/Client.js +2 -1
  9. package/dist/esm/client/Client.js.map +1 -1
  10. package/dist/esm/entities/Entity.d.ts +22 -3
  11. package/dist/esm/entities/Entity.js +210 -38
  12. package/dist/esm/entities/Entity.js.map +1 -1
  13. package/dist/esm/entities/Entity.test.js +4 -3
  14. package/dist/esm/entities/Entity.test.js.map +1 -1
  15. package/dist/esm/entities/EntityCache.d.ts +6 -4
  16. package/dist/esm/entities/EntityCache.js +18 -7
  17. package/dist/esm/entities/EntityCache.js.map +1 -1
  18. package/dist/esm/entities/EntityMetadata.d.ts +8 -2
  19. package/dist/esm/entities/EntityMetadata.js +52 -4
  20. package/dist/esm/entities/EntityMetadata.js.map +1 -1
  21. package/dist/esm/entities/EntityStore.d.ts +4 -4
  22. package/dist/esm/entities/EntityStore.js +8 -10
  23. package/dist/esm/entities/EntityStore.js.map +1 -1
  24. package/dist/esm/entities/OperationBatcher.d.ts +1 -1
  25. package/dist/esm/entities/OperationBatcher.js +2 -0
  26. package/dist/esm/entities/OperationBatcher.js.map +1 -1
  27. package/dist/esm/files/EntityFile.d.ts +6 -1
  28. package/dist/esm/files/EntityFile.js +11 -4
  29. package/dist/esm/files/EntityFile.js.map +1 -1
  30. package/dist/esm/files/FileManager.d.ts +3 -1
  31. package/dist/esm/files/FileManager.js +2 -2
  32. package/dist/esm/files/FileManager.js.map +1 -1
  33. package/dist/esm/utils/versions.js +1 -1
  34. package/package.json +2 -2
  35. package/src/__tests__/entities.test.ts +471 -5
  36. package/src/__tests__/fixtures/testStorage.ts +1 -1
  37. package/src/client/Client.ts +6 -1
  38. package/src/entities/Entity.test.ts +8 -7
  39. package/src/entities/Entity.ts +239 -29
  40. package/src/entities/EntityCache.ts +20 -10
  41. package/src/entities/EntityMetadata.ts +69 -9
  42. package/src/entities/EntityStore.ts +15 -12
  43. package/src/entities/OperationBatcher.ts +16 -2
  44. package/src/files/EntityFile.ts +16 -3
  45. package/src/files/FileManager.ts +7 -3
  46. package/src/utils/versions.ts +1 -1
@@ -11,8 +11,8 @@ import {
11
11
  operationSupersedes,
12
12
  } from '@verdant-web/common';
13
13
  import { Context } from '../context/context.js';
14
- import type { EntityStore } from './EntityStore.js';
15
14
  import { Entity } from './Entity.js';
15
+ import type { EntityStore } from './EntityStore.js';
16
16
 
17
17
  const DEFAULT_BATCH_KEY = '@@default';
18
18
 
@@ -257,7 +257,11 @@ export class OperationBatcher {
257
257
  return Promise.all(this.batcher.flushAll());
258
258
  };
259
259
 
260
- private createUndo = async (data: { ops: Operation[]; source?: Entity }) => {
260
+ private createUndo = async (data: {
261
+ ops: Operation[];
262
+ source?: Entity;
263
+ isRedo?: boolean;
264
+ }) => {
261
265
  // this can't be done on-demand because we rely on the current
262
266
  // state of the entities to calculate the inverse operations.
263
267
  const inverseOps = await this.getInverseOperations(data);
@@ -268,11 +272,21 @@ export class OperationBatcher {
268
272
  const redo = await this.createUndo({
269
273
  ops: inverseOps,
270
274
  source: data.source,
275
+ isRedo: true,
271
276
  });
272
277
  // set time to now for all undo operations, they're happening now.
273
278
  for (const op of inverseOps) {
274
279
  op.timestamp = this.ctx.time.now;
275
280
  }
281
+
282
+ this.ctx.log(
283
+ 'debug',
284
+ data.isRedo ? 'Redo' : 'Undo',
285
+ inverseOps,
286
+ '\n was \n',
287
+ data.ops,
288
+ );
289
+
276
290
  await this.commitOperations(
277
291
  inverseOps,
278
292
  // undos should not generate their own undo operations
@@ -1,5 +1,6 @@
1
1
  import { EventSubscriber, FileData } from '@verdant-web/common';
2
2
  import { Context } from '../context/context.js';
3
+ import { Entity } from '../entities/Entity.js';
3
4
 
4
5
  export type EntityFileEvents = {
5
6
  change: () => void;
@@ -8,6 +9,9 @@ export type EntityFileEvents = {
8
9
  export const UPDATE = Symbol('entity-file-update');
9
10
  export const MARK_FAILED = Symbol('entity-file-mark-failed');
10
11
 
12
+ // this one goes on Entity
13
+ export const CHILD_FILE_CHANGED = Symbol('child-file-changed');
14
+
11
15
  export type EntityFileSnapshot = {
12
16
  id: string;
13
17
  url?: string | null;
@@ -27,19 +31,23 @@ export class EntityFile extends EventSubscriber<EntityFileEvents> {
27
31
  private _uploaded = false;
28
32
  private ctx: Context;
29
33
  private unsubscribes: (() => void)[] = [];
34
+ private parent: Entity;
30
35
 
31
36
  constructor(
32
37
  public readonly id: string,
33
38
  {
34
39
  downloadRemote = false,
35
40
  ctx,
41
+ parent,
36
42
  }: {
37
43
  downloadRemote?: boolean;
38
44
  ctx: Context;
45
+ parent: Entity;
39
46
  },
40
47
  ) {
41
48
  super();
42
49
  this.ctx = ctx;
50
+ this.parent = parent;
43
51
  this._downloadRemote = downloadRemote;
44
52
 
45
53
  this.unsubscribes.push(
@@ -57,6 +65,11 @@ export class EntityFile extends EventSubscriber<EntityFileEvents> {
57
65
  return this._uploaded || this._fileData?.remote || false;
58
66
  }
59
67
 
68
+ private emitChange() {
69
+ this.parent[CHILD_FILE_CHANGED](this);
70
+ this.emit('change');
71
+ }
72
+
60
73
  [UPDATE] = (fileData: FileData) => {
61
74
  this.ctx.log('debug', 'EntityFile updated', this.id, fileData);
62
75
  this._loading = false;
@@ -69,13 +82,13 @@ export class EntityFile extends EventSubscriber<EntityFileEvents> {
69
82
  this.ctx.log('debug', 'Creating object URL for file', this.id);
70
83
  this._objectUrl = URL.createObjectURL(fileData.file);
71
84
  }
72
- this.emit('change');
85
+ this.emitChange();
73
86
  };
74
87
 
75
88
  [MARK_FAILED] = () => {
76
89
  this._failed = true;
77
90
  this._loading = false;
78
- this.emit('change');
91
+ this.emitChange();
79
92
  };
80
93
 
81
94
  private onUploaded = (data: FileData) => {
@@ -83,7 +96,7 @@ export class EntityFile extends EventSubscriber<EntityFileEvents> {
83
96
  this._fileData ??= data;
84
97
  this._uploaded = true;
85
98
  this.ctx.log('debug', 'File marked uploaded', this.id, this._fileData);
86
- this.emit('change');
99
+ this.emitChange();
87
100
  };
88
101
 
89
102
  get url(): string | null {
@@ -1,5 +1,6 @@
1
1
  import { FileData } from '@verdant-web/common';
2
2
  import { Context } from '../context/context.js';
3
+ import { Entity } from '../entities/Entity.js';
3
4
  import { Disposable } from '../internal.js';
4
5
  import { Sync } from '../sync/Sync.js';
5
6
  import { EntityFile, MARK_FAILED, UPDATE } from './EntityFile.js';
@@ -22,11 +23,11 @@ export class FileManager extends Disposable {
22
23
  );
23
24
  }
24
25
 
25
- add = async (file: FileData) => {
26
+ add = async (file: FileData, parent: Entity) => {
26
27
  // immediately cache the file
27
28
  let entityFile = this.cache.get(file.id);
28
29
  if (!entityFile) {
29
- entityFile = new EntityFile(file.id, { ctx: this.context });
30
+ entityFile = new EntityFile(file.id, { ctx: this.context, parent });
30
31
  this.cache.set(file.id, entityFile);
31
32
  }
32
33
 
@@ -44,7 +45,10 @@ export class FileManager extends Disposable {
44
45
  * Immediately returns an EntityFile to use, then either loads
45
46
  * the file from cache, local database, or the server.
46
47
  */
47
- get = (id: string, options: { downloadRemote?: boolean; ctx: Context }) => {
48
+ get = (
49
+ id: string,
50
+ options: { downloadRemote?: boolean; ctx: Context; parent: Entity },
51
+ ) => {
48
52
  if (this.cache.has(id)) {
49
53
  return this.cache.get(id)!;
50
54
  }
@@ -17,7 +17,7 @@ export function getLatestVersion(data: {
17
17
  return tsVersion;
18
18
  }
19
19
  return v;
20
- }, 0);
20
+ }, 1);
21
21
 
22
22
  return latestVersion;
23
23
  }