@dabble/patches 0.7.16 → 0.7.17

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.
@@ -56,6 +56,14 @@ interface ClientAlgorithm {
56
56
  * @returns The changes created (for broadcast to other tabs)
57
57
  */
58
58
  handleDocChange<T extends object>(docId: string, ops: JSONPatchOp[], doc: PatchesDoc<T> | undefined, metadata: Record<string, any>): Promise<Change[]>;
59
+ /**
60
+ * Read-only check for whether a document has any pending local data.
61
+ * - OT: Checks pendingChanges
62
+ * - LWW: Checks both pendingOps and sendingChange
63
+ *
64
+ * Unlike getPendingToSend, this has no side effects.
65
+ */
66
+ hasPending(docId: string): Promise<boolean>;
59
67
  /**
60
68
  * Gets pending data to send to the server.
61
69
  * - OT: Returns all pending changes (may batch)
@@ -28,6 +28,7 @@ declare class LWWAlgorithm implements ClientAlgorithm {
28
28
  createDoc<T extends object>(docId: string, snapshot?: PatchesSnapshot<T>): PatchesDoc<T>;
29
29
  loadDoc(docId: string): Promise<PatchesSnapshot | undefined>;
30
30
  handleDocChange<T extends object>(docId: string, ops: JSONPatchOp[], doc: PatchesDoc<T> | undefined, metadata: Record<string, any>): Promise<Change[]>;
31
+ hasPending(docId: string): Promise<boolean>;
31
32
  getPendingToSend(docId: string): Promise<Change[] | null>;
32
33
  applyServerChanges<T extends object>(docId: string, serverChanges: Change[], doc: PatchesDoc<T> | undefined): Promise<Change[]>;
33
34
  confirmSent(docId: string, _changes: Change[]): Promise<void>;
@@ -30,6 +30,12 @@ class LWWAlgorithm {
30
30
  }
31
31
  return changes;
32
32
  }
33
+ async hasPending(docId) {
34
+ const sendingChange = await this.store.getSendingChange(docId);
35
+ if (sendingChange) return true;
36
+ const pendingOps = await this.store.getPendingOps(docId);
37
+ return pendingOps.length > 0;
38
+ }
33
39
  async getPendingToSend(docId) {
34
40
  const sendingChange = await this.store.getSendingChange(docId);
35
41
  if (sendingChange) {
@@ -23,6 +23,7 @@ declare class OTAlgorithm implements ClientAlgorithm {
23
23
  createDoc<T extends object>(docId: string, snapshot?: PatchesSnapshot<T>): PatchesDoc<T>;
24
24
  loadDoc(docId: string): Promise<PatchesSnapshot | undefined>;
25
25
  handleDocChange<T extends object>(docId: string, ops: JSONPatchOp[], doc: PatchesDoc<T> | undefined, metadata: Record<string, any>): Promise<Change[]>;
26
+ hasPending(docId: string): Promise<boolean>;
26
27
  getPendingToSend(docId: string): Promise<Change[] | null>;
27
28
  applyServerChanges<T extends object>(docId: string, serverChanges: Change[], doc: PatchesDoc<T> | undefined): Promise<Change[]>;
28
29
  confirmSent(_docId: string, _changes: Change[]): Promise<void>;
@@ -40,6 +40,10 @@ class OTAlgorithm {
40
40
  }
41
41
  return changes;
42
42
  }
43
+ async hasPending(docId) {
44
+ const pending = await this.store.getPendingChanges(docId);
45
+ return pending.length > 0;
46
+ }
43
47
  async getPendingToSend(docId) {
44
48
  const pending = await this.store.getPendingChanges(docId);
45
49
  return pending.length > 0 ? pending : null;
@@ -164,10 +164,10 @@ class PatchesSync extends (_a = ReadonlyStoreClass, _syncDoc_dec = [blockable],
164
164
  const syncedEntries = {};
165
165
  for (const doc of activeDocs) {
166
166
  const algorithm = this._getAlgorithm(doc.docId);
167
- const pending = await algorithm.getPendingToSend(doc.docId);
167
+ const hasPending = await algorithm.hasPending(doc.docId);
168
168
  const entry = {
169
169
  committedRev: doc.committedRev,
170
- hasPending: pending != null && pending.length > 0,
170
+ hasPending,
171
171
  syncStatus: doc.committedRev === 0 ? "unsynced" : "synced",
172
172
  syncError: void 0,
173
173
  isLoaded: false
@@ -291,7 +291,7 @@ class PatchesSync extends (_a = ReadonlyStoreClass, _syncDoc_dec = [blockable],
291
291
  await algorithm.confirmSent(docId, changeBatch);
292
292
  pending = await algorithm.getPendingToSend(docId) ?? [];
293
293
  }
294
- const stillHasPending = pending != null && pending.length > 0;
294
+ const stillHasPending = await algorithm.hasPending(docId);
295
295
  this._updateDocSyncState(docId, { hasPending: stillHasPending, syncStatus: "synced" });
296
296
  } catch (err) {
297
297
  if (this._isDocDeletedError(err)) {
@@ -381,11 +381,11 @@ class PatchesSync extends (_a = ReadonlyStoreClass, _syncDoc_dec = [blockable],
381
381
  for (const docId of newIds) {
382
382
  const algorithm = this._getAlgorithm(docId);
383
383
  const committedRev = await algorithm.getCommittedRev(docId);
384
- const pending = await algorithm.getPendingToSend(docId);
384
+ const hasPending = await algorithm.hasPending(docId);
385
385
  docData.push({
386
386
  docId,
387
387
  committedRev,
388
- hasPending: pending != null && pending.length > 0
388
+ hasPending
389
389
  });
390
390
  }
391
391
  batch(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dabble/patches",
3
- "version": "0.7.16",
3
+ "version": "0.7.17",
4
4
  "description": "Immutable JSON Patch implementation based on RFC 6902 supporting operational transformation and last-writer-wins",
5
5
  "author": "Jacob Wright <jacwright@gmail.com>",
6
6
  "bugs": {