@docstack/pouchdb-adapter-googledrive 0.1.2 → 0.1.3

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 (2) hide show
  1. package/lib/drive.js +61 -56
  2. package/package.json +1 -1
package/lib/drive.js CHANGED
@@ -160,22 +160,31 @@ class DriveHandler {
160
160
  return { id, changes: null };
161
161
  }
162
162
  }));
163
+ const foundNew = {};
163
164
  for (const { id, changes } of logResults) {
164
165
  if (!changes) {
165
166
  this.log(`Skipping failed log ${id}`);
166
167
  continue;
167
168
  }
168
169
  let changesArray = Array.isArray(changes) ? changes : [changes];
169
- this.currentLogSizeEstimate += 100 * changesArray.length;
170
+ this.log('Processing log file', id, 'changes', changesArray.length);
170
171
  for (const change of changesArray) {
171
- this.log('Processing change, sequence', change.seq);
172
172
  this.updateIndex(change, id);
173
173
  if (this.docCache.get(change.id)) {
174
174
  this.docCache.remove(change.id);
175
175
  }
176
+ foundNew[change.id] = {
177
+ _id: change.id,
178
+ _rev: change.rev,
179
+ _deleted: !!change.deleted,
180
+ seq: change.seq
181
+ };
176
182
  }
177
183
  this.processedLogIds.add(id);
178
- this.log('Processed log', id);
184
+ }
185
+ if (Object.keys(foundNew).length > 0) {
186
+ this.log('Load complete, notifying of changes', Object.keys(foundNew).length);
187
+ this.notifyListeners(foundNew);
179
188
  }
180
189
  }
181
190
  // 2. Replay NEW Change Logs (Metadata only updates)
@@ -508,13 +517,21 @@ class DriveHandler {
508
517
  }
509
518
  this.localDocsEtag = res.etag;
510
519
  // Update Index
520
+ const changedDocs = {};
511
521
  for (const change of changes) {
512
522
  this.updateIndex(change, res.id);
513
523
  if (change.doc)
514
524
  this.docCache.put(change.id, change.doc);
515
525
  else
516
526
  this.docCache.remove(change.id);
527
+ changedDocs[change.id] = {
528
+ _id: change.id,
529
+ _rev: change.rev,
530
+ _deleted: !!change.deleted,
531
+ seq: change.seq
532
+ };
517
533
  }
534
+ this.notifyListeners(changedDocs);
518
535
  return;
519
536
  }
520
537
  catch (err) {
@@ -539,6 +556,7 @@ class DriveHandler {
539
556
  await this.saveMeta(nextMeta, this.metaEtag);
540
557
  // 4. Update Local State
541
558
  this.meta = nextMeta;
559
+ const changedDocs = {};
542
560
  for (const change of changes) {
543
561
  this.updateIndex(change, fileId);
544
562
  if (change.doc) {
@@ -547,9 +565,15 @@ class DriveHandler {
547
565
  else if (change.deleted) {
548
566
  this.docCache.remove(change.id);
549
567
  }
568
+ changedDocs[change.id] = {
569
+ _id: change.id,
570
+ _rev: change.rev,
571
+ _deleted: !!change.deleted,
572
+ seq: change.seq
573
+ };
550
574
  }
551
- // Notify local changes feed listeners about our own write
552
- this.notifyListeners();
575
+ // Notify local changes feed listeners about only what we just wrote
576
+ this.notifyListeners(changedDocs);
553
577
  // 5. Compaction Check
554
578
  const totalChanges = await this.countTotalChanges();
555
579
  if (totalChanges >= this.compactionThreshold ||
@@ -697,42 +721,27 @@ class DriveHandler {
697
721
  return createRes.id;
698
722
  }
699
723
  async findFile(name) {
700
- const pending = this.pendingFinds.get(name);
701
- if (pending) {
702
- this.log('findFile reuse pending search', name);
703
- return await pending;
704
- }
705
- const findPromise = (async () => {
706
- const safeName = this.escapeQuery(name);
707
- const q = `name = '${safeName}' and '${this.folderId}' in parents and trashed = false`;
708
- try {
709
- const files = await this.client.listFiles(q);
710
- if (files.length > 0) {
711
- let file = files[0];
712
- if (!file.etag) {
713
- // Robustness: Fetch metadata for the file if etag is missing from list
714
- try {
715
- file = await this.client.getFileMetadata(file.id);
716
- }
717
- catch (e) {
718
- this.log('Failed to fetch file metadata for etag', file.id, e);
719
- }
720
- }
721
- return {
722
- fileId: file.id,
723
- etag: file.etag,
724
- md5Checksum: file.md5Checksum,
725
- modifiedTime: file.modifiedTime
726
- };
724
+ const safeName = this.escapeQuery(name);
725
+ const q = `name = '${safeName}' and '${this.folderId}' in parents and trashed = false`;
726
+ const files = await this.client.listFiles(q);
727
+ if (files.length > 0) {
728
+ let file = files[0];
729
+ if (!file.etag) {
730
+ try {
731
+ file = await this.client.getFileMetadata(file.id);
732
+ }
733
+ catch (e) {
734
+ this.log('Failed to fetch file metadata for etag', file.id, e);
727
735
  }
728
- return null;
729
- }
730
- finally {
731
- this.pendingFinds.delete(name);
732
736
  }
733
- })();
734
- this.pendingFinds.set(name, findPromise);
735
- return await findPromise;
737
+ return {
738
+ fileId: file.id,
739
+ etag: file.etag,
740
+ md5Checksum: file.md5Checksum,
741
+ modifiedTime: file.modifiedTime
742
+ };
743
+ }
744
+ return null;
736
745
  }
737
746
  async downloadJson(fileId, skipCache = false) {
738
747
  return await this.fetchFile(fileId, skipCache);
@@ -855,24 +864,20 @@ class DriveHandler {
855
864
  }
856
865
  }, intervalMs);
857
866
  }
858
- notifyListeners() {
859
- // Observers expecting 'docs' object might be broken if they expect FULL body.
860
- // We can pass empty object or partials?
861
- // Real PouchDB changes feed calls `db.changes()`.
862
- // Our `adapter.js` uses `db.onChange` effectively.
863
- // We should pass a map of { ID: { _rev, ... } } (Index entries)
864
- // Adapter needs to handle this.
865
- const changes = {};
866
- for (const [id, entry] of Object.entries(this.index)) {
867
- changes[id] = {
868
- _id: id,
869
- _rev: entry.rev,
870
- _deleted: !!entry.deleted,
871
- seq: entry.seq // IMPORTANT: Missing previously, preventing filtered changes from working
872
- };
867
+ notifyListeners(changedDocs) {
868
+ const changes = changedDocs || {};
869
+ // If no specifically changed docs provided, we don't want to firehose the whole index anymore
870
+ // as it causes reprocessing loops in PouchDB sync.
871
+ if (Object.keys(changes).length === 0)
872
+ return;
873
+ for (const cb of this.listeners) {
874
+ try {
875
+ cb(changes);
876
+ }
877
+ catch (e) {
878
+ this.log('Error in change listener callback', e);
879
+ }
873
880
  }
874
- for (const cb of this.listeners)
875
- cb(changes);
876
881
  }
877
882
  // For tests/debug
878
883
  onChange(cb) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docstack/pouchdb-adapter-googledrive",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "PouchDB adapter for Google Drive",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",