From 431e10280897f3c7e379e1c7a34f63fda9c3c626 Mon Sep 17 00:00:00 2001 From: haad Date: Wed, 13 Apr 2016 09:46:17 +0200 Subject: [PATCH] Cleanup OrbitDB --- src/OrbitDB.js | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index fd95933..2287391 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -133,12 +133,27 @@ class OrbitDB { /* Private methods */ + // Cache DB operation entries in memory from a log + _cacheOperations(log) { + return new Promise((resolve, reject) => { + const payloadHashes = log.items + .map((f) => f.payload) + .filter((f) => Lazy(this._cached).find((e) => e.hash === f.payload) === undefined); + + Promise.map(payloadHashes, (f) => OrbitDB.fetchPayload(this._ipfs, f), { concurrency: 4 }) + .then((payloads) => { + payloads.forEach((f) => this._cached.push(f)); + resolve(); + }) + .catch(reject); + }); + } + // LWW-element-set _read(sequence, key, amount, inclusive) { // Last-Write-Wins, ie. use only the first occurance of the key let handled = []; const _createLWWSet = (item) => { - // console.log("-->", item, handled) if(Lazy(handled).indexOf(item.key) === -1) { handled.push(item.key); if(DBOperation.Types.isInsert(item.op)) @@ -148,16 +163,13 @@ class OrbitDB { }; // Find the items from the sequence (list of operations) - // console.log("333", this._cached) return sequence .map((f) => Lazy(this._cached).find((e) => { - // console.log("e", e, f) return e.hash === f.payload })) .compact() // Remove nulls .skipWhile((f) => key && f.key !== key) // Drop elements until we have the first one requested .map((f) => { - // console.log("f", f, "key", key); return f; }) .map(_createLWWSet) // Return items as LWW (ignore values after the first found) @@ -188,22 +200,6 @@ class OrbitDB { }); } - // Cache DB operation entries in memory from a log - _cacheOperations(log) { - return new Promise((resolve, reject) => { - const payloadHashes = log.items - .map((f) => f.payload) - .filter((f) => Lazy(this._cached).find((e) => e.hash === f.payload) === undefined); - - Promise.map(payloadHashes, (f) => OrbitDB.fetchPayload(this._ipfs, f), { concurrency: 4 }) - .then((payloads) => { - payloads.forEach((f) => this._cached.push(f)); - resolve(); - }) - .catch(reject); - }); - } - static fetchPayload(ipfs, hash) { return new Promise((resolve, reject) => { ipfs.object.get(hash)