mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-29 10:16:39 +00:00
Cleanup code
This commit is contained in:
parent
0da4bf7aa6
commit
a8dd379a23
@ -1,48 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('asyncawait/async');
|
||||
var await = require('asyncawait/await');
|
||||
var OrbitClient = require('../src/OrbitClient');
|
||||
var Timer = require('./Timer');
|
||||
const async = require('asyncawait/async');
|
||||
const await = require('asyncawait/await');
|
||||
const OrbitClient = require('../src/OrbitClient');
|
||||
const Timer = require('./Timer');
|
||||
|
||||
// Redis
|
||||
var host = 'localhost';
|
||||
var port = 6379;
|
||||
const host = 'localhost';
|
||||
const port = 6379;
|
||||
|
||||
var username = 'LambOfGod';
|
||||
var password = '';
|
||||
const username = 'LambOfGod';
|
||||
const password = '';
|
||||
|
||||
let run = (async(() => {
|
||||
try {
|
||||
var orbit = OrbitClient.connect(host, port, username, password);
|
||||
const c1 = 'c1';
|
||||
const channel = orbit.channel(c1);
|
||||
const orbit = OrbitClient.connect(host, port, username, password);
|
||||
const channel = 'testing123';
|
||||
const db = orbit.channel(channel);
|
||||
|
||||
let count = 1;
|
||||
let running = false;
|
||||
|
||||
// setInterval(async(() => {
|
||||
// if(!running) {
|
||||
while(true) {
|
||||
running = true;
|
||||
const key = "username";
|
||||
let timer = new Timer(true);
|
||||
db.put(key, "Lamb Of God " + count);
|
||||
let v = db.get(key);
|
||||
|
||||
const key = "username";
|
||||
let timer = new Timer(true);
|
||||
channel.put(key, "Lamb Of God " + count);
|
||||
let v = channel.get(key);
|
||||
console.log(`Query #${count} took ${timer.stop(true)} ms\n`);
|
||||
console.log("---------------------------------------------------")
|
||||
console.log("Key | Value")
|
||||
console.log("---------------------------------------------------")
|
||||
console.log(`${key} | ${v}`);
|
||||
console.log("---------------------------------------------------")
|
||||
console.log(`Query #${count} took ${timer.stop(true)} ms\n`);
|
||||
|
||||
console.log("---------------------------------------------------")
|
||||
console.log("Key | Value")
|
||||
console.log("---------------------------------------------------")
|
||||
console.log(`${key} | ${v}`);
|
||||
console.log("---------------------------------------------------")
|
||||
|
||||
running = false;
|
||||
count ++;
|
||||
count ++;
|
||||
}
|
||||
// }
|
||||
// }), 500);
|
||||
|
||||
} catch(e) {
|
||||
console.error("error:", e);
|
||||
|
@ -1,16 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('asyncawait/async');
|
||||
var await = require('asyncawait/await');
|
||||
var OrbitClient = require('../src/OrbitClient');
|
||||
var Timer = require('./Timer');
|
||||
const async = require('asyncawait/async');
|
||||
const await = require('asyncawait/await');
|
||||
const OrbitClient = require('../src/OrbitClient');
|
||||
const Timer = require('./Timer');
|
||||
|
||||
// Redis
|
||||
var host = 'localhost';
|
||||
var port = 6379;
|
||||
const host = 'localhost';
|
||||
const port = 6379;
|
||||
|
||||
var username = 'LambOfGod';
|
||||
var password = '';
|
||||
const username = 'LambOfGod';
|
||||
const password = '';
|
||||
|
||||
let run = (async(() => {
|
||||
try {
|
||||
|
@ -3,12 +3,9 @@
|
||||
const _ = require('lodash');
|
||||
const async = require('asyncawait/async');
|
||||
const await = require('asyncawait/await');
|
||||
const ipfsAPI = require('orbit-common/lib/ipfs-api-promised');
|
||||
const OrbitList = require('./list/OrbitList');
|
||||
const HashCacheOps = require('./HashCacheOps');
|
||||
|
||||
var Timer = require('../examples/Timer');
|
||||
|
||||
const DefaultAmount = 1;
|
||||
|
||||
class DataStore {
|
||||
@ -44,8 +41,6 @@ class DataStore {
|
||||
}
|
||||
|
||||
_fetchRecursive(options, currentAmount, deleted, res) {
|
||||
// console.log("-->")
|
||||
// console.log("opts:", options, currentAmount)
|
||||
const opts = {
|
||||
amount: options && options.amount ? options.amount : DefaultAmount,
|
||||
first: options && options.first ? options.first : null,
|
||||
@ -59,7 +54,6 @@ class DataStore {
|
||||
if(!currentAmount) currentAmount = 0;
|
||||
|
||||
const item = this._fetchOne(currentAmount);
|
||||
// console.log("ITEM", item)
|
||||
|
||||
if(item && item.payload) {
|
||||
const wasHandled = _.includes(handledItems, item.payload.key);
|
||||
@ -68,12 +62,10 @@ class DataStore {
|
||||
(!opts.first || (opts.first && (opts.first === item.payload.key && result.length === 0))
|
||||
|| (opts.first && (opts.first !== item.payload.key && result.length > 0))))
|
||||
{
|
||||
// console.log("PUSH!", item, currentAmount, result.length);
|
||||
result.push(item);
|
||||
handledItems.push(item.payload.key);
|
||||
}
|
||||
} else if(item.payload.op === HashCacheOps.Delete) {
|
||||
// console.log("DELETE!", item);
|
||||
handledItems.push(item.payload.key);
|
||||
}
|
||||
|
||||
@ -82,7 +74,6 @@ class DataStore {
|
||||
if(opts.key && item.payload.key === opts.key)
|
||||
return result;
|
||||
|
||||
// console.log("ITEM", item.payload.key, opts.last)
|
||||
if(opts.last && item.payload.key === opts.last)
|
||||
return result;
|
||||
|
||||
@ -92,7 +83,6 @@ class DataStore {
|
||||
if(currentAmount >= this.list.items.length)
|
||||
return result;
|
||||
|
||||
// console.log("RES!", result)
|
||||
result = this._fetchRecursive(opts, currentAmount, handledItems, result);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
let ItemTypes = {
|
||||
const ItemTypes = {
|
||||
Message: "text",
|
||||
Snippet: "snippet",
|
||||
File: "file",
|
||||
|
@ -21,7 +21,6 @@ const privkey = Keystore.getKeys().privateKey;
|
||||
class OrbitClient {
|
||||
constructor(ipfs) {
|
||||
this._ipfs = ipfs;
|
||||
this.network = {};
|
||||
this.user = null;
|
||||
}
|
||||
|
||||
@ -30,16 +29,11 @@ class OrbitClient {
|
||||
|
||||
this._pubsub.subscribe(hash, password, async((hash, message) => {
|
||||
const other = await(List.fromIpfsHash(this._ipfs, message));
|
||||
// console.log(">", other.id, other.seq, other.ver);
|
||||
if(other.id !== this.user.username) {
|
||||
// let timer = new Timer(true);
|
||||
if(other.id !== this.user.username)
|
||||
this._store.join(other);
|
||||
// console.log(`Join took ${timer.stop(true)} ms`);
|
||||
}
|
||||
}));
|
||||
|
||||
return {
|
||||
// info: (options) => this._info(hash, password),
|
||||
delete: () => this._deleteChannel(hash, password),
|
||||
iterator: (options) => this._iterator(hash, password, options),
|
||||
setMode: (mode) => this._setMode(hash, password, mode),
|
||||
@ -101,13 +95,11 @@ class OrbitClient {
|
||||
|
||||
// Get messages
|
||||
messages = await(this._store.get(opts));
|
||||
// console.log("M", messages)
|
||||
|
||||
// Remove the first/last item if greater/lesser than is set
|
||||
let startIndex = lt ? 1 : 0;
|
||||
let endIndex = gt ? messages.length - 1 : messages.length;
|
||||
messages = messages.slice(startIndex, endIndex)
|
||||
// console.log("M2", messages)
|
||||
|
||||
if(!reverse) messages.reverse();
|
||||
|
||||
@ -122,7 +114,7 @@ class OrbitClient {
|
||||
|
||||
_createMessage(channel, password, operation, key, value) {
|
||||
const size = -1;
|
||||
const meta = new MetaInfo(ItemTypes.Message, size, this.user.id, new Date().getTime());
|
||||
const meta = new MetaInfo(ItemTypes.Message, size, this.user.username, new Date().getTime());
|
||||
const item = new OrbitDBItem(operation, key, value, meta);
|
||||
const data = await (ipfsAPI.putObject(this._ipfs, JSON.stringify(item)));
|
||||
return data.Hash;
|
||||
@ -149,8 +141,8 @@ class OrbitClient {
|
||||
const res = await(this._store.add(hash));
|
||||
const listHash = await(this._store.list.getIpfsHash());
|
||||
await(this._pubsub.publish(channel, listHash));
|
||||
return key;
|
||||
// return res;
|
||||
return key;
|
||||
}
|
||||
|
||||
_deleteChannel(channel, password) {
|
||||
@ -158,38 +150,12 @@ class OrbitClient {
|
||||
return true;
|
||||
}
|
||||
|
||||
// _setMode(channel, password, modes) {
|
||||
// let m = [];
|
||||
// if(typeof modes !== 'Array')
|
||||
// m.push(modes);
|
||||
// else
|
||||
// m = modes;
|
||||
// // const res = await(this.client.linkedList(channel, password).setMode(m));
|
||||
// // return res.modes;
|
||||
// return { todo: 'TODO!' }
|
||||
// }
|
||||
|
||||
// _info(channel, password) {
|
||||
// var l = this._pubsub.latest(channel);
|
||||
// return l;
|
||||
// }
|
||||
|
||||
_connect(host, port, username, password) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.user = { username: username, id: 'hello-todo' }
|
||||
this._pubsub = new PubSub(this._ipfs, host, port, username, password);
|
||||
// this.client = this._pubsub._client;
|
||||
// this.user = this.client.info.user;
|
||||
this.user = { id: 'hello-todo', username: username }
|
||||
this._store = new DataStore(username, this._ipfs);
|
||||
this._store = new DataStore(username, this._ipfs);
|
||||
resolve();
|
||||
// this.network = {
|
||||
// id: this.client.info.networkId,
|
||||
// name: this.client.info.name,
|
||||
// config: this.client.info.config
|
||||
// };
|
||||
// setTimeout(() => {
|
||||
// resolve();
|
||||
// }, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var Encryption = require('orbit-common/lib/Encryption');
|
||||
const Encryption = require('orbit-common/lib/Encryption');
|
||||
|
||||
class Post {
|
||||
constructor(content) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user