(core) add explicit doc and inventory creation step

Summary:
Currently, if a document is created by importing a file, inventory
creation is a little haphazard - it works, but triggers a
"surprise" message.  This diff makes initialization of inventory
explicit, so that surprise messages shouldn't happen during
document creation.

Test Plan: manual

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2696
This commit is contained in:
Paul Fitzpatrick
2020-12-21 09:46:50 -05:00
parent 24e76b4abc
commit d5b00f5169
5 changed files with 38 additions and 8 deletions

View File

@@ -110,6 +110,17 @@ export class DocSnapshotInventory implements IInventory {
constructor(private _doc: ExternalStorage, private _meta: ExternalStorage,
private _getFilename: (key: string) => Promise<string>) {}
/**
* Start keeping inventory for a new document.
*/
public async create(key: string) {
await this._mutex.runExclusive(key, async() => {
const fname = await this._getFilename(key);
await this._saveToFile(fname, []);
this._needFlush.add(key);
});
}
/**
* Add a new snapshot of a document to the existing inventory. A prevSnapshotId may
* be supplied as a cross-check. It will be matched against the most recent
@@ -126,7 +137,7 @@ export class DocSnapshotInventory implements IInventory {
await this._mutex.runExclusive(key, async() => {
const snapshots = await this._getSnapshots(key, prevSnapshotId);
// Could be already added if reconstruction happened.
if (snapshots[0].snapshotId === snapshot.snapshotId) { return; }
if (snapshots[0] && snapshots[0].snapshotId === snapshot.snapshotId) { return; }
this._normalizeMetadata(snapshot);
snapshots.unshift(snapshot);
const fname = await this._getFilename(key);