From 23eb8ea66e90d3c50029c7e74a1ddee01bcd3239 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Mon, 17 Jun 2024 20:11:08 +0200 Subject: [PATCH] perf: Update for Meteor 3 collection internals --- lib/client/ground.db.js | 34 +++++++++++++++++----------------- package.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/client/ground.db.js b/lib/client/ground.db.js index 194173f..f0e58ec 100644 --- a/lib/client/ground.db.js +++ b/lib/client/ground.db.js @@ -44,9 +44,9 @@ function strId(id) { } function throttle(func, timeFrame) { - var lastTime = 0; + let lastTime = 0; return function () { - var now = new Date(); + const now = new Date(); if (now - lastTime >= timeFrame) { func(); lastTime = now; @@ -59,13 +59,12 @@ function throttle(func, timeFrame) { */ const Invalidate = (collection, wait=100) => { return throttle(() => { - Object.keys(collection._collection.queries) - .forEach(qid => { + for (const qid of Object.keys(collection._collection.queries)) { const query = collection._collection.queries[qid]; if (query) { collection._collection._recomputeResults(query); } - }); + } collection._collection._observeQueue.drain(); }, wait); }; @@ -154,8 +153,8 @@ Ground.Collection = class GroundCollection { Kernel.defer(() => { // Add the document to minimongo - if (!this._collection._docs._map[id]) { - this._collection._docs._map[id] = EJSON.fromJSONValue(doc); + if (!this._collection._docs._map.get(id)) { + this._collection._docs._map.set(id, EJSON.fromJSONValue(doc)); // Invalidate the observers pr. document // this call is throttled @@ -166,7 +165,7 @@ Ground.Collection = class GroundCollection { this.pendingReads.dec(); - // Check if all documetns have been handled + // Check if all documents have been handled if (++handled === len) { Kernel.defer(() => { this.isLoaded = true; @@ -208,7 +207,7 @@ Ground.Collection = class GroundCollection { }); } else { this.storage - .setItem(doc._id, EJSON.toJSONValue(doc)) + .setItem(doc._id, EJSON.toJSONValue(doc.getDocument?.() || doc)) .then(() => { this.pendingWrites.dec(); }); @@ -221,9 +220,9 @@ Ground.Collection = class GroundCollection { setDocument(doc, remove) { doc._id = strId(doc._id); if (remove) { - delete this._collection._docs._map[doc._id]; + this._collection._docs._map.delete(doc._id); } else { - this._collection._docs._map[doc._id] = EJSON.clone(doc); + this._collection._docs._map.set(doc._id, EJSON.clone(doc)); } this.invalidate(); } @@ -328,7 +327,7 @@ Ground.Collection = class GroundCollection { clear() { this.storage.clear(); - this._collection._docs._map = {}; + this._collection._docs._map.clear(); this.invalidate(); } @@ -339,16 +338,17 @@ Ground.Collection = class GroundCollection { keep(cursors) { const arrayOfCursors = (Array.isArray(cursors)) ? cursors : [cursors]; // Map the ground db storage into an array of id's - const currentIds = Object.keys(this._collection._docs._map); + const currentIds = this._collection._docs._map.keys(); // Map each cursor id's into one flat array const keepIds = arrayOfCursors.map((cursor) => cursor.map((doc) => strId(doc._id))).flat(); // Remove all other documents from the collection - difference(currentIds, keepIds).forEach((id) => { + const arrays = [currentIds, keepIds]; + for (const id of arrays.reduce((a, b) => a.filter((c) => !b.includes(c)))) { // Remove it from in memory delete this._collection._docs._map[id]; // Remove it from storage this.saveDocument({ _id: id }, true); - }); + } this.invalidate(); } @@ -374,8 +374,8 @@ Ground.Collection = class GroundCollection { Meteor.defer(() => { pendingOnLoad.forEach(f => { f(); - }) - }) + }); + }); } } diff --git a/package.js b/package.js index 20e1e35..ce4f722 100644 --- a/package.js +++ b/package.js @@ -10,7 +10,7 @@ Npm.depends({ }); Package.onUse(function (api) { - api.versionsFrom(['1.3', '2.3']); + api.versionsFrom(['3.0-rc.4']); api.use(['ecmascript', 'mongo-id', 'reactive-var', 'diff-sequence', 'minimongo']); api.use([