mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-27 16:32:30 +00:00
Cleanup code
This commit is contained in:
parent
fd8b17d2d5
commit
ce99aa734f
70
WIP/test1.js
70
WIP/test1.js
@ -1,70 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const _ = require('lodash');
|
|
||||||
const Timer = require('./examples/Timer');
|
|
||||||
const List = require('./src/list/List');
|
|
||||||
|
|
||||||
var run = () => {
|
|
||||||
var redis = require("redis");
|
|
||||||
this.client1 = redis.createClient({ host: "localhost", port: 6379 });
|
|
||||||
this.client2 = redis.createClient({ host: "localhost", port: 6379 });
|
|
||||||
var hash = "ccc"
|
|
||||||
this.client1.subscribe(hash);
|
|
||||||
this.client1.subscribe(hash);
|
|
||||||
|
|
||||||
|
|
||||||
let listA = new List("A");
|
|
||||||
let listB = new List("B");
|
|
||||||
let listC = new List("C");
|
|
||||||
|
|
||||||
const handleMessage = (hash, event) => {
|
|
||||||
const l = List.fromJson(JSON.parse(event));
|
|
||||||
// console.log("LIST", l);
|
|
||||||
|
|
||||||
if(l.id === 'A') {
|
|
||||||
listB.join(l);
|
|
||||||
listC.join(l);
|
|
||||||
} else if(l.id === 'B') {
|
|
||||||
listA.join(l);
|
|
||||||
listC.join(l);
|
|
||||||
} else if(l.id === 'C') {
|
|
||||||
listA.join(l);
|
|
||||||
console.log("Items:", listA.items.length);
|
|
||||||
// console.log(JSON.stringify(listA, null, 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
this.client1.on("message", handleMessage);
|
|
||||||
this.client2.on("message", handleMessage);
|
|
||||||
|
|
||||||
let h = 0;
|
|
||||||
setInterval(() => {
|
|
||||||
listC.add("C--"+h);
|
|
||||||
this.client2.publish(hash, JSON.stringify(listC.toJson()));
|
|
||||||
h++;
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
setInterval(() => {
|
|
||||||
let a = 0;
|
|
||||||
for(let a = 0; a < 10; a ++) {
|
|
||||||
listB.add("B--"+(i+a));
|
|
||||||
}
|
|
||||||
this.client2.publish(hash, JSON.stringify(listB.toJson()));
|
|
||||||
i++;
|
|
||||||
}, 20);
|
|
||||||
|
|
||||||
let k = 0;
|
|
||||||
setInterval(() => {
|
|
||||||
listA.add("A--"+k);
|
|
||||||
k++;
|
|
||||||
listA.add("A--"+k);
|
|
||||||
k++;
|
|
||||||
listA.add("A--"+k);
|
|
||||||
k++;
|
|
||||||
this.client2.publish(hash, JSON.stringify(listA.toJson()));
|
|
||||||
}, 100);
|
|
||||||
};
|
|
||||||
|
|
||||||
run();
|
|
92
WIP/test2.js
92
WIP/test2.js
@ -1,92 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const _ = require('lodash');
|
|
||||||
const async = require('asyncawait/async');
|
|
||||||
const await = require('asyncawait/await');
|
|
||||||
const ipfsDaemon = require('orbit-common/lib/ipfs-daemon');
|
|
||||||
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
|
||||||
const List = require('./src/list/OrbitList');
|
|
||||||
const Timer = require('./examples/Timer');
|
|
||||||
|
|
||||||
const startIpfs = async (() => {
|
|
||||||
return new Promise(async((resolve, reject) => {
|
|
||||||
const ipfsd = await(ipfsDaemon());
|
|
||||||
resolve(ipfsd.daemon);
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
let ipfs;
|
|
||||||
|
|
||||||
var run = async(() => {
|
|
||||||
ipfs = await(startIpfs());
|
|
||||||
|
|
||||||
var redis = require("redis");
|
|
||||||
this.client1 = redis.createClient({ host: "localhost", port: 6379 });
|
|
||||||
this.client2 = redis.createClient({ host: "localhost", port: 6379 });
|
|
||||||
var hash = "ccc"
|
|
||||||
this.client1.subscribe(hash);
|
|
||||||
this.client1.subscribe(hash);
|
|
||||||
|
|
||||||
|
|
||||||
let listA = new List("A", ipfs);
|
|
||||||
let listB = new List("B", ipfs);
|
|
||||||
let listC = new List("C", ipfs);
|
|
||||||
|
|
||||||
const handleMessage = async((hash, event) => {
|
|
||||||
// const l = List.fromJson(JSON.parse(event));
|
|
||||||
console.log(">", event);
|
|
||||||
const l = await(List.fromIpfsHash(ipfs, event));
|
|
||||||
// console.log("ITEMS RECEIVED", l.items.length);
|
|
||||||
|
|
||||||
if(l.id === 'A') {
|
|
||||||
listB.join(l);
|
|
||||||
listC.join(l);
|
|
||||||
} else if(l.id === 'B') {
|
|
||||||
// listA.join(l);
|
|
||||||
// listC.join(l);
|
|
||||||
} else if(l.id === 'C') {
|
|
||||||
listB.join(l);
|
|
||||||
var timer = new Timer('a');
|
|
||||||
listC.join(listB);
|
|
||||||
console.log("join took " + timer.stop(true) + " ms");
|
|
||||||
console.log("Items:", listC.items.length);
|
|
||||||
// console.log(listC.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.client1.on("message", handleMessage);
|
|
||||||
this.client2.on("message", handleMessage);
|
|
||||||
|
|
||||||
let h = 0;
|
|
||||||
setInterval(async(() => {
|
|
||||||
listC.add("C--"+h);
|
|
||||||
const list = await(listC.getIpfsHash());
|
|
||||||
this.client2.publish(hash, list);
|
|
||||||
h++;
|
|
||||||
}), 1000);
|
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
setInterval(async(() => {
|
|
||||||
let a = 0;
|
|
||||||
// for(let a = 0; a < 10; a ++) {
|
|
||||||
listB.add("B--"+(i+a));
|
|
||||||
// }
|
|
||||||
const list = await(listB.getIpfsHash());
|
|
||||||
this.client2.publish(hash, list);
|
|
||||||
i++;
|
|
||||||
}), 50);
|
|
||||||
|
|
||||||
// let k = 0;
|
|
||||||
// setInterval(async(() => {
|
|
||||||
// listA.add("A--"+k);
|
|
||||||
// k++;
|
|
||||||
// listA.add("A--"+k);
|
|
||||||
// k++;
|
|
||||||
// listA.add("A--"+k);
|
|
||||||
// k++;
|
|
||||||
// this.client2.publish(hash, JSON.stringify(listA.toJson()));
|
|
||||||
// }), 100);
|
|
||||||
// });
|
|
||||||
});
|
|
||||||
|
|
||||||
run();
|
|
@ -13,10 +13,6 @@ class Node {
|
|||||||
return "" + this.id + "." + this.seq + "." + this.ver;
|
return "" + this.id + "." + this.seq + "." + this.ver;
|
||||||
}
|
}
|
||||||
|
|
||||||
compact() {
|
|
||||||
return { id: this.id, seq: this.seq, ver: this.ver, data: this.data, next: this.next }
|
|
||||||
}
|
|
||||||
|
|
||||||
toJson() {
|
toJson() {
|
||||||
return { id: this.id, seq: this.seq, ver: this.ver, data: this.data, next: this.next }
|
return { id: this.id, seq: this.seq, ver: this.ver, data: this.data, next: this.next }
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ class OrbitList extends List {
|
|||||||
node._commit();
|
node._commit();
|
||||||
this._currentBatch.push(node);
|
this._currentBatch.push(node);
|
||||||
this.ver ++;
|
this.ver ++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
join(other) {
|
join(other) {
|
||||||
|
@ -14,10 +14,6 @@ class OrbitNode extends Node {
|
|||||||
this.hash = hash ? hash : this.ipfsHash;
|
this.hash = hash ? hash : this.ipfsHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
get compactId() {
|
|
||||||
return "" + this.id + "." + this.seq + "." + this.ver;
|
|
||||||
}
|
|
||||||
|
|
||||||
get ipfsHash() {
|
get ipfsHash() {
|
||||||
this._commit();
|
this._commit();
|
||||||
return this.hash;
|
return this.hash;
|
||||||
|
@ -37,25 +37,4 @@ describe('Node', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('compact', () => {
|
|
||||||
it('presents the node as a compacted object', (done) => {
|
|
||||||
const node1 = new Node('A', 0, 0, 'hello');
|
|
||||||
const node2 = new Node('B', 0, 0, 'hello', [node1]);
|
|
||||||
const compacted1 = node1.compact();
|
|
||||||
const compacted2 = node2.compact();
|
|
||||||
|
|
||||||
assert.notEqual(compacted1, null);
|
|
||||||
assert.equal(compacted1.id, 'A');
|
|
||||||
assert.equal(compacted1.seq, 0);
|
|
||||||
assert.equal(compacted1.ver, 0);
|
|
||||||
assert.equal(compacted1.data, 'hello');
|
|
||||||
assert.equal(compacted1.next instanceof Array, true);
|
|
||||||
assert.equal(compacted1.next.length, 0);
|
|
||||||
|
|
||||||
assert.equal(compacted2.id, 'B');
|
|
||||||
assert.equal(compacted2.next.length, 1);
|
|
||||||
assert.equal(compacted2.next[0], 'A.0.0');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user