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