Skip to content

Commit

Permalink
Merge pull request #221 from salmanhasni/salman-220-fixing-map-ds
Browse files Browse the repository at this point in the history
fixes #220
  • Loading branch information
StorytellerCZ authored Aug 2, 2024
2 parents 13bf992 + af20e07 commit 195bdac
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/client/ground.db.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,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.has(id)) {
this._collection._docs._map.set([id], EJSON.fromJSONValue(doc));

// Invalidate the observers pr. document
// this call is throttled
Expand Down Expand Up @@ -221,9 +221,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.remove(doc._id);
} else {
this._collection._docs._map[doc._id] = EJSON.clone(doc);
this._collection._docs._map.set(doc._id, EJSON.clone(doc));
}
this.invalidate();
}
Expand Down Expand Up @@ -328,7 +328,7 @@ Ground.Collection = class GroundCollection {

clear() {
this.storage.clear();
this._collection._docs._map = {};
this._collection._docs._map.clear()
this.invalidate();
}

Expand All @@ -339,14 +339,15 @@ 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);
let currentIds = this._collection._docs._map.keys()
currentIds = Array.from(currentIds);
// 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
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];
this._collection._docs._map.delete(id)
// Remove it from storage
this.saveDocument({ _id: id }, true);
}
Expand All @@ -355,7 +356,7 @@ Ground.Collection = class GroundCollection {
}

toJSON() {
return JSON.stringify(this._collection._docs._map);
return JSON.stringify(Object.fromEntries(this._collection._docs._map.entries()))
}

// Simulate the Event Emitter Api "once"
Expand Down

0 comments on commit 195bdac

Please sign in to comment.