@verdant-web/store 3.6.3 → 3.7.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.
- package/dist/bundle/index.js +12 -12
- package/dist/bundle/index.js.map +3 -3
- package/dist/esm/__tests__/batching.test.js +67 -1
- package/dist/esm/__tests__/batching.test.js.map +1 -1
- package/dist/esm/__tests__/documents.test.js +22 -0
- package/dist/esm/__tests__/documents.test.js.map +1 -1
- package/dist/esm/__tests__/fixtures/testStorage.d.ts +3 -1
- package/dist/esm/__tests__/fixtures/testStorage.js +3 -2
- package/dist/esm/__tests__/fixtures/testStorage.js.map +1 -1
- package/dist/esm/__tests__/mutations.test.js +40 -0
- package/dist/esm/__tests__/mutations.test.js.map +1 -1
- package/dist/esm/client/Client.d.ts +22 -1
- package/dist/esm/client/Client.js.map +1 -1
- package/dist/esm/client/ClientDescriptor.d.ts +7 -1
- package/dist/esm/client/ClientDescriptor.js +1 -0
- package/dist/esm/client/ClientDescriptor.js.map +1 -1
- package/dist/esm/entities/Entity.d.ts +40 -3
- package/dist/esm/entities/Entity.js +87 -24
- package/dist/esm/entities/Entity.js.map +1 -1
- package/dist/esm/entities/Entity.test.js +24 -2
- package/dist/esm/entities/Entity.test.js.map +1 -1
- package/dist/esm/entities/EntityMetadata.d.ts +2 -0
- package/dist/esm/entities/EntityMetadata.js +9 -1
- package/dist/esm/entities/EntityMetadata.js.map +1 -1
- package/dist/esm/entities/EntityStore.d.ts +1 -2
- package/dist/esm/entities/EntityStore.js +10 -7
- package/dist/esm/entities/EntityStore.js.map +1 -1
- package/dist/esm/entities/OperationBatcher.d.ts +25 -0
- package/dist/esm/entities/OperationBatcher.js +31 -3
- package/dist/esm/entities/OperationBatcher.js.map +1 -1
- package/dist/esm/entities/types.d.ts +18 -1
- package/dist/esm/metadata/Metadata.d.ts +3 -1
- package/dist/esm/metadata/Metadata.js +8 -1
- package/dist/esm/metadata/Metadata.js.map +1 -1
- package/dist/esm/queries/BaseQuery.d.ts +3 -0
- package/dist/esm/queries/BaseQuery.js +45 -11
- package/dist/esm/queries/BaseQuery.js.map +1 -1
- package/dist/esm/sync/Sync.d.ts +8 -2
- package/dist/esm/sync/Sync.js +6 -3
- package/dist/esm/sync/Sync.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/batching.test.ts +78 -0
- package/src/__tests__/documents.test.ts +28 -0
- package/src/__tests__/fixtures/testStorage.ts +10 -1
- package/src/__tests__/mutations.test.ts +53 -0
- package/src/client/Client.ts +14 -1
- package/src/client/ClientDescriptor.ts +9 -0
- package/src/entities/Entity.test.ts +31 -2
- package/src/entities/Entity.ts +128 -28
- package/src/entities/EntityMetadata.ts +13 -3
- package/src/entities/EntityStore.ts +10 -9
- package/src/entities/OperationBatcher.ts +69 -2
- package/src/entities/types.ts +29 -1
- package/src/metadata/Metadata.ts +13 -0
- package/src/queries/BaseQuery.ts +50 -13
- package/src/sync/Sync.ts +12 -2
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ClientMessage,
|
|
3
|
+
createMigration,
|
|
4
|
+
Operation,
|
|
5
|
+
schema,
|
|
6
|
+
} from '@verdant-web/common';
|
|
2
7
|
// @ts-ignore
|
|
3
8
|
import { IDBFactory } from 'fake-indexeddb';
|
|
4
9
|
import { ClientWithCollections, ClientDescriptor } from '../../index.js';
|
|
5
10
|
import { METADATA_VERSION_KEY } from '../../client/constants.js';
|
|
11
|
+
import { Sync } from '../../sync/Sync.js';
|
|
6
12
|
|
|
7
13
|
export const todoCollection = schema.collection({
|
|
8
14
|
name: 'todo',
|
|
@@ -91,11 +97,13 @@ export function createTestStorage({
|
|
|
91
97
|
disableRebasing = false,
|
|
92
98
|
metadataVersion,
|
|
93
99
|
log,
|
|
100
|
+
onOperation,
|
|
94
101
|
}: {
|
|
95
102
|
idb?: IDBFactory;
|
|
96
103
|
disableRebasing?: boolean;
|
|
97
104
|
metadataVersion?: number;
|
|
98
105
|
log?: (...args: any[]) => void;
|
|
106
|
+
onOperation?: (op: Operation) => void;
|
|
99
107
|
} = {}) {
|
|
100
108
|
const storage = new ClientDescriptor({
|
|
101
109
|
schema: testSchema,
|
|
@@ -105,6 +113,7 @@ export function createTestStorage({
|
|
|
105
113
|
disableRebasing,
|
|
106
114
|
log,
|
|
107
115
|
[METADATA_VERSION_KEY]: metadataVersion,
|
|
116
|
+
onOperation,
|
|
108
117
|
}).open();
|
|
109
118
|
return storage as Promise<ClientWithCollections>;
|
|
110
119
|
}
|
|
@@ -54,4 +54,57 @@ describe('mutations', () => {
|
|
|
54
54
|
expect(itemAExists).toBeNull();
|
|
55
55
|
expect(itemBExists === itemB).toBe(true);
|
|
56
56
|
});
|
|
57
|
+
|
|
58
|
+
describe('on entities', () => {
|
|
59
|
+
it('should allow them to delete themselves (root level)', async () => {
|
|
60
|
+
const client = await createTestStorage();
|
|
61
|
+
|
|
62
|
+
const itemA = await client.todos.put({
|
|
63
|
+
id: '1',
|
|
64
|
+
content: 'itemA',
|
|
65
|
+
category: 'test',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
await itemA.deleteSelf();
|
|
69
|
+
|
|
70
|
+
const itemAExists = await client.todos.get('1').resolved;
|
|
71
|
+
|
|
72
|
+
expect(itemAExists).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should allow them to delete themselves (nested array)', async () => {
|
|
76
|
+
const client = await createTestStorage();
|
|
77
|
+
|
|
78
|
+
const itemA = await client.todos.put({
|
|
79
|
+
id: '1',
|
|
80
|
+
content: 'itemA',
|
|
81
|
+
category: 'test',
|
|
82
|
+
attachments: [
|
|
83
|
+
{
|
|
84
|
+
name: 'foo',
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
itemA.get('attachments').get(0).deleteSelf();
|
|
90
|
+
|
|
91
|
+
expect(itemA.get('attachments').length).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should allow them to delete themselves (nested map)', async () => {
|
|
95
|
+
const client = await createTestStorage();
|
|
96
|
+
|
|
97
|
+
const weird = await client.weirds.put({
|
|
98
|
+
objectMap: {
|
|
99
|
+
foo: {
|
|
100
|
+
content: 'bar',
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
weird.get('objectMap').get('foo').deleteSelf();
|
|
106
|
+
|
|
107
|
+
expect(weird.get('objectMap').size).toBe(0);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
57
110
|
});
|
package/src/client/Client.ts
CHANGED
|
@@ -197,7 +197,7 @@ export class Client<Presence = any, Profile = any> extends EventSubscriber<{
|
|
|
197
197
|
return this.entities.batch;
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
stats = async () => {
|
|
200
|
+
stats = async (): Promise<ClientStats> => {
|
|
201
201
|
const collectionNames = Object.keys(this.schema.collections);
|
|
202
202
|
let collections = {} as Record<string, { count: number; size: number }>;
|
|
203
203
|
if (this.disposed) {
|
|
@@ -427,3 +427,16 @@ export class Client<Presence = any, Profile = any> extends EventSubscriber<{
|
|
|
427
427
|
await this.import(exportData);
|
|
428
428
|
};
|
|
429
429
|
}
|
|
430
|
+
|
|
431
|
+
export interface ClientStats {
|
|
432
|
+
collections: Record<string, { count: number; size: number }>;
|
|
433
|
+
meta: {
|
|
434
|
+
baselinesSize: { count: number; size: number };
|
|
435
|
+
operationsSize: { count: number; size: number };
|
|
436
|
+
};
|
|
437
|
+
storage: StorageEstimate | undefined;
|
|
438
|
+
totalMetaSize: number;
|
|
439
|
+
totalCollectionsSize: number;
|
|
440
|
+
metaToDataRatio: number;
|
|
441
|
+
quotaUsage: number | undefined;
|
|
442
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EventSubscriber,
|
|
3
3
|
Migration,
|
|
4
|
+
Operation,
|
|
4
5
|
StorageSchema,
|
|
5
6
|
hashObject,
|
|
6
7
|
} from '@verdant-web/common';
|
|
@@ -60,6 +61,13 @@ export interface ClientDescriptorOptions<Presence = any, Profile = any> {
|
|
|
60
61
|
* Configuration for file management
|
|
61
62
|
*/
|
|
62
63
|
files?: FileManagerConfig;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Listen for operations as they are applied to the database.
|
|
67
|
+
* Wouldn't recommend using this unless you know what you're doing.
|
|
68
|
+
* It's a very hot code path...
|
|
69
|
+
*/
|
|
70
|
+
onOperation?: (operation: Operation) => void;
|
|
63
71
|
/**
|
|
64
72
|
* Enables experimental WeakRef usage to cull documents
|
|
65
73
|
* from cache that aren't being used. This is a performance
|
|
@@ -173,6 +181,7 @@ export class ClientDescriptor<
|
|
|
173
181
|
const meta = new Metadata({
|
|
174
182
|
context,
|
|
175
183
|
disableRebasing: init.disableRebasing,
|
|
184
|
+
onOperation: init.onOperation,
|
|
176
185
|
});
|
|
177
186
|
|
|
178
187
|
// verify schema integrity
|
|
@@ -51,7 +51,9 @@ describe('Entity', () => {
|
|
|
51
51
|
},
|
|
52
52
|
} as const;
|
|
53
53
|
|
|
54
|
-
function createTestEntity(
|
|
54
|
+
function createTestEntity({
|
|
55
|
+
onPendingOperations = vi.fn(),
|
|
56
|
+
}: { onPendingOperations?: () => void } = {}) {
|
|
55
57
|
const events: EntityStoreEvents = {
|
|
56
58
|
add: new WeakEvent(),
|
|
57
59
|
replace: new WeakEvent(),
|
|
@@ -70,7 +72,7 @@ describe('Entity', () => {
|
|
|
70
72
|
events,
|
|
71
73
|
metadataFamily: new EntityFamilyMetadata({
|
|
72
74
|
ctx: mockContext,
|
|
73
|
-
onPendingOperations
|
|
75
|
+
onPendingOperations,
|
|
74
76
|
rootOid: 'test/1',
|
|
75
77
|
}),
|
|
76
78
|
files: {
|
|
@@ -79,6 +81,7 @@ describe('Entity', () => {
|
|
|
79
81
|
} as any as FileManager,
|
|
80
82
|
patchCreator,
|
|
81
83
|
readonlyKeys: ['id'],
|
|
84
|
+
deleteSelf: vi.fn(),
|
|
82
85
|
});
|
|
83
86
|
|
|
84
87
|
function initialize(data: any) {
|
|
@@ -238,4 +241,30 @@ describe('Entity', () => {
|
|
|
238
241
|
expect(entity.get('string')).toBe('new world');
|
|
239
242
|
});
|
|
240
243
|
});
|
|
244
|
+
|
|
245
|
+
describe('dropping pending operations', () => {
|
|
246
|
+
it('drops the correct operation', () => {
|
|
247
|
+
const onPendingOperations = vi.fn();
|
|
248
|
+
const { entity, initialize } = createTestEntity({ onPendingOperations });
|
|
249
|
+
|
|
250
|
+
initialize({
|
|
251
|
+
id: 'hi',
|
|
252
|
+
string: 'world',
|
|
253
|
+
number: 1,
|
|
254
|
+
nullable: null,
|
|
255
|
+
nonNullable: { first: { inner: 'foo' } },
|
|
256
|
+
map: {},
|
|
257
|
+
list: [],
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
entity.update({ string: 'new world' });
|
|
261
|
+
expect(onPendingOperations).toHaveBeenCalledTimes(1);
|
|
262
|
+
const operation = onPendingOperations.mock.calls[0][0][0];
|
|
263
|
+
console.log(operation);
|
|
264
|
+
|
|
265
|
+
entity.__discardPendingOperation__(operation);
|
|
266
|
+
|
|
267
|
+
expect(entity.get('string')).toBe('world');
|
|
268
|
+
});
|
|
269
|
+
});
|
|
241
270
|
});
|
package/src/entities/Entity.ts
CHANGED
|
@@ -58,6 +58,7 @@ export interface EntityInit {
|
|
|
58
58
|
fieldPath?: (string | number)[];
|
|
59
59
|
patchCreator: PatchCreator;
|
|
60
60
|
events: EntityStoreEvents;
|
|
61
|
+
deleteSelf: () => void;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
export class Entity<
|
|
@@ -94,6 +95,9 @@ export class Entity<
|
|
|
94
95
|
// only used for root entities to track delete/restore state.
|
|
95
96
|
private wasDeletedLastChange = false;
|
|
96
97
|
private cachedView: any | undefined = undefined;
|
|
98
|
+
// provided from external creators, this is a method to delete
|
|
99
|
+
// this entity.
|
|
100
|
+
private _deleteSelf: () => void;
|
|
97
101
|
|
|
98
102
|
constructor({
|
|
99
103
|
oid,
|
|
@@ -106,6 +110,7 @@ export class Entity<
|
|
|
106
110
|
files,
|
|
107
111
|
patchCreator,
|
|
108
112
|
events,
|
|
113
|
+
deleteSelf,
|
|
109
114
|
}: EntityInit) {
|
|
110
115
|
super();
|
|
111
116
|
|
|
@@ -125,6 +130,7 @@ export class Entity<
|
|
|
125
130
|
this.metadataFamily = metadataFamily;
|
|
126
131
|
this.events = events;
|
|
127
132
|
this.parent = parent;
|
|
133
|
+
this._deleteSelf = deleteSelf;
|
|
128
134
|
|
|
129
135
|
// TODO: should any but the root entity be listening to these?
|
|
130
136
|
if (!this.parent) {
|
|
@@ -366,6 +372,15 @@ export class Entity<
|
|
|
366
372
|
return this.viewData.fromOlderVersion;
|
|
367
373
|
}
|
|
368
374
|
|
|
375
|
+
/**
|
|
376
|
+
* Returns the storage namespace this entity came from. For example, if you
|
|
377
|
+
* have multiple stores initialized from the same schema, you can use this
|
|
378
|
+
* to figure out where an isolated entity was created / stored.
|
|
379
|
+
*/
|
|
380
|
+
get namespace() {
|
|
381
|
+
return this.ctx.namespace;
|
|
382
|
+
}
|
|
383
|
+
|
|
369
384
|
/**
|
|
370
385
|
* Pruning - when entities have invalid children, we 'prune' that
|
|
371
386
|
* data up to the nearest prunable point - a nullable field,
|
|
@@ -460,30 +475,19 @@ export class Entity<
|
|
|
460
475
|
}
|
|
461
476
|
};
|
|
462
477
|
|
|
478
|
+
private invalidateCachedView = () => {
|
|
479
|
+
this._viewData = undefined;
|
|
480
|
+
this.cachedView = undefined;
|
|
481
|
+
};
|
|
482
|
+
|
|
463
483
|
private change = (ev: EntityChange) => {
|
|
464
484
|
if (ev.oid === this.oid) {
|
|
465
485
|
// reset cached view
|
|
466
|
-
this.
|
|
467
|
-
this.cachedView = undefined;
|
|
468
|
-
// chain deepChanges to parents
|
|
469
|
-
this.deepChange(this, ev);
|
|
470
|
-
// emit the change, it's for us
|
|
471
|
-
this.ctx.log('Emitting change event', this.oid);
|
|
472
|
-
this.emit('change', { isLocal: ev.isLocal });
|
|
473
|
-
// for root entities, we need to go ahead and decide if we're
|
|
474
|
-
// deleted or not - so queries can exclude us if we are.
|
|
486
|
+
this.invalidateCachedView();
|
|
475
487
|
if (!this.parent) {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
this.emit('delete', { isLocal: ev.isLocal });
|
|
480
|
-
this.wasDeletedLastChange = true;
|
|
481
|
-
} else if (!this.deleted && this.wasDeletedLastChange) {
|
|
482
|
-
this.ctx.log('debug', 'Entity restored', this.oid);
|
|
483
|
-
// newly restored - emit event
|
|
484
|
-
this.emit('restore', { isLocal: ev.isLocal });
|
|
485
|
-
this.wasDeletedLastChange = false;
|
|
486
|
-
}
|
|
488
|
+
this.changeRoot(ev);
|
|
489
|
+
} else {
|
|
490
|
+
this.changeNested(ev);
|
|
487
491
|
}
|
|
488
492
|
} else {
|
|
489
493
|
// forward it to the correct family member. if none exists
|
|
@@ -494,6 +498,36 @@ export class Entity<
|
|
|
494
498
|
}
|
|
495
499
|
}
|
|
496
500
|
};
|
|
501
|
+
private changeRoot = (ev: EntityChange) => {
|
|
502
|
+
// for root entities, we need to determine if we're deleted or not
|
|
503
|
+
// before firing any events
|
|
504
|
+
if (this.deleted) {
|
|
505
|
+
if (!this.wasDeletedLastChange) {
|
|
506
|
+
this.ctx.log('debug', 'Entity deleted', this.oid);
|
|
507
|
+
this.emit('delete', { isLocal: ev.isLocal });
|
|
508
|
+
this.wasDeletedLastChange = true;
|
|
509
|
+
}
|
|
510
|
+
// already deleted, do nothing.
|
|
511
|
+
} else {
|
|
512
|
+
if (this.wasDeletedLastChange) {
|
|
513
|
+
this.ctx.log('debug', 'Entity restored', this.oid);
|
|
514
|
+
this.emit('restore', { isLocal: ev.isLocal });
|
|
515
|
+
this.wasDeletedLastChange = false;
|
|
516
|
+
}
|
|
517
|
+
// emit deepchange, too
|
|
518
|
+
this.deepChange(this, ev);
|
|
519
|
+
// emit the change, it's for us
|
|
520
|
+
this.ctx.log('Emitting change event', this.oid);
|
|
521
|
+
this.emit('change', { isLocal: ev.isLocal });
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
private changeNested = (ev: EntityChange) => {
|
|
525
|
+
// chain deepChanges to parents
|
|
526
|
+
this.deepChange(this, ev);
|
|
527
|
+
// emit the change, it's for us
|
|
528
|
+
this.ctx.log('Emitting change event', this.oid);
|
|
529
|
+
this.emit('change', { isLocal: ev.isLocal });
|
|
530
|
+
};
|
|
497
531
|
protected deepChange = (target: Entity, ev: EntityChange) => {
|
|
498
532
|
// reset cached deep updated at timestamp; either this
|
|
499
533
|
// entity or children have changed
|
|
@@ -530,6 +564,7 @@ export class Entity<
|
|
|
530
564
|
fieldPath: [...this.fieldPath, key],
|
|
531
565
|
patchCreator: this.patchCreator,
|
|
532
566
|
events: this.events,
|
|
567
|
+
deleteSelf: this.delete.bind(this, key),
|
|
533
568
|
});
|
|
534
569
|
};
|
|
535
570
|
|
|
@@ -720,15 +755,42 @@ export class Entity<
|
|
|
720
755
|
}
|
|
721
756
|
};
|
|
722
757
|
|
|
723
|
-
set =
|
|
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
|
+
) => {
|
|
724
774
|
assertNotSymbol(key);
|
|
725
|
-
this.
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
this.
|
|
730
|
-
|
|
731
|
-
|
|
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
|
+
}
|
|
732
794
|
};
|
|
733
795
|
|
|
734
796
|
/**
|
|
@@ -781,6 +843,13 @@ export class Entity<
|
|
|
781
843
|
return Object.values(this.view);
|
|
782
844
|
};
|
|
783
845
|
|
|
846
|
+
get size() {
|
|
847
|
+
if (this.isList) {
|
|
848
|
+
return this.length;
|
|
849
|
+
}
|
|
850
|
+
return this.keys().length;
|
|
851
|
+
}
|
|
852
|
+
|
|
784
853
|
update = (
|
|
785
854
|
data: any,
|
|
786
855
|
{
|
|
@@ -960,6 +1029,17 @@ export class Entity<
|
|
|
960
1029
|
this.view.forEach(callback);
|
|
961
1030
|
};
|
|
962
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
|
+
|
|
963
1043
|
some = (predicate: (value: ListItemValue<KeyValue>) => boolean): boolean => {
|
|
964
1044
|
return this.view.some(predicate);
|
|
965
1045
|
};
|
|
@@ -976,11 +1056,31 @@ export class Entity<
|
|
|
976
1056
|
|
|
977
1057
|
includes = this.has;
|
|
978
1058
|
|
|
1059
|
+
/**
|
|
1060
|
+
* Deletes this entity. WARNING: this can be tricky to
|
|
1061
|
+
* use correctly. You must not reference this entity
|
|
1062
|
+
* instance in any way after the deletion happens, or
|
|
1063
|
+
* you will get an error!
|
|
1064
|
+
*
|
|
1065
|
+
* It's a little easier to delete using client.delete
|
|
1066
|
+
* if you can manage it with your app's code. For example,
|
|
1067
|
+
* in React, use hooks.useClient() to get the client and
|
|
1068
|
+
* call delete from there.
|
|
1069
|
+
*/
|
|
1070
|
+
deleteSelf = () => {
|
|
1071
|
+
return this._deleteSelf();
|
|
1072
|
+
};
|
|
1073
|
+
|
|
979
1074
|
// TODO: make these escape hatches unnecessary
|
|
980
1075
|
__getViewData__ = (oid: ObjectIdentifier, type: 'confirmed' | 'pending') => {
|
|
981
1076
|
return this.metadataFamily.get(oid).computeView(type === 'confirmed');
|
|
982
1077
|
};
|
|
983
1078
|
__getFamilyOids__ = () => this.metadataFamily.getAllOids();
|
|
1079
|
+
|
|
1080
|
+
__discardPendingOperation__ = (operation: Operation) => {
|
|
1081
|
+
this.metadataFamily.discardPendingOperation(operation);
|
|
1082
|
+
this.invalidateCachedView();
|
|
1083
|
+
};
|
|
984
1084
|
}
|
|
985
1085
|
|
|
986
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.
|
|
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) => {
|
|
@@ -451,6 +446,7 @@ export class EntityStore extends Disposable {
|
|
|
451
446
|
metadataFamily: metadataFamily,
|
|
452
447
|
patchCreator: this.meta.patchCreator,
|
|
453
448
|
events: this.events,
|
|
449
|
+
deleteSelf: this.delete.bind(this, oid),
|
|
454
450
|
});
|
|
455
451
|
};
|
|
456
452
|
|
|
@@ -458,6 +454,11 @@ export class EntityStore extends Disposable {
|
|
|
458
454
|
this.batcher.addOperations(operations);
|
|
459
455
|
};
|
|
460
456
|
|
|
457
|
+
discardPendingOperation = (operation: Operation) => {
|
|
458
|
+
const root = getOidRoot(operation.oid);
|
|
459
|
+
this.cache.get(root)?.deref()?.__discardPendingOperation__(operation);
|
|
460
|
+
};
|
|
461
|
+
|
|
461
462
|
/**
|
|
462
463
|
* Loads initial Entity data from storage
|
|
463
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
|
-
|
|
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(
|
|
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({
|