diff --git a/examples/keyvalue.js b/examples/keyvalue.js index 1f6f363..587a408 100644 --- a/examples/keyvalue.js +++ b/examples/keyvalue.js @@ -40,7 +40,6 @@ let run = (async(() => { } } catch(e) { - console.error("error:", e); console.error(e.stack); process.exit(1); } diff --git a/src/list/List.js b/src/list/List.js index c5d798a..3facc7d 100644 --- a/src/list/List.js +++ b/src/list/List.js @@ -12,8 +12,9 @@ class List { this._currentBatch = []; } + /* Methods */ add(data) { - const heads = this._findHeads(this.items); + const heads = List.findHeads(this.items); const node = new Node(this.id, this.seq, this.ver, data, heads); this._currentBatch.push(node); this.ver ++; @@ -36,20 +37,16 @@ class List { this.ver = 0; } - _findHeads(list) { - return Lazy(list) - .reverse() - .indexBy((f) => f.id) - .pairs() - .map((f) => f[1]) - .filter((f) => !this._isReferencedInChain(list, f)) - .toArray(); - } - - _isReferencedInChain(all, item) { - return Lazy(all).find((e) => e.hasChild(item)) !== undefined; + /* Private methods */ + _commit() { + const current = Lazy(this._currentBatch).difference(this._items).toArray(); + this._items = this._items.concat(current); + this._currentBatch = []; + this.ver = 0; + this.seq ++; } + /* Properties */ get items() { return this._items.concat(this._currentBatch); } @@ -67,6 +64,7 @@ class List { } } + /* Static methods */ static fromJson(json) { let list = new List(json.id); list.seq = json.seq; @@ -77,6 +75,20 @@ class List { .toArray(); return list; } + + static findHeads(list) { + return Lazy(list) + .reverse() + .indexBy((f) => f.id) + .pairs() + .map((f) => f[1]) + .filter((f) => !List.isReferencedInChain(list, f)) + .toArray(); + } + + static isReferencedInChain(all, item) { + return Lazy(all).find((e) => e.hasChild(item)) !== undefined; + } } module.exports = List; diff --git a/src/list/OrbitList.js b/src/list/OrbitList.js index af13746..841ae08 100644 --- a/src/list/OrbitList.js +++ b/src/list/OrbitList.js @@ -23,7 +23,7 @@ class OrbitList extends List { if(this.ver >= MaxBatchSize) this._commit(); - const heads = super._findHeads(this.items); + const heads = List.findHeads(this.items); const node = new Node(this._ipfs, this.id, this.seq, this.ver, data, heads); node._commit(); // TODO: obsolete? this._currentBatch.push(node);