From 84e984a4cb13b4fc8cbf8577e0dcaf5981082e41 Mon Sep 17 00:00:00 2001 From: haad Date: Tue, 4 Oct 2016 09:27:15 +0200 Subject: [PATCH] Eventlog example. Cleanup code. --- examples/eventlog.js | 85 +++++++++++++------------------------------- src/OrbitDB.js | 5 +-- 2 files changed, 26 insertions(+), 64 deletions(-) diff --git a/examples/eventlog.js b/examples/eventlog.js index 49a8b72..dd36f51 100644 --- a/examples/eventlog.js +++ b/examples/eventlog.js @@ -1,65 +1,30 @@ -'use strict'; +'use strict' -const async = require('asyncawait/async'); -const await = require('asyncawait/await'); -const ipfsd = require('ipfsd-ctl'); -const OrbitDB = require('../src/OrbitDB'); -const Timer = require('./Timer'); +const IpfsApi = require('ipfs-api') +const OrbitDB = require('../src/OrbitDB') -// usage: reader.js +const ipfs = IpfsApi('localhost', '5001') +const orbitdb = new OrbitDB(ipfs) +const db = orbitdb.eventlog("/orbit-db/examples/eventlog-example") -// orbit-server -const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // 'localhost:3333' -const username = process.argv[2] ? process.argv[2] : 'testrunner'; -const password = ''; -const channelName = process.argv[3] ? process.argv[3] : 'c2'; -const prefix = process.argv[4] ? process.argv[4] : 'Hello'; +const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓'] -const startIpfs = () => { - return new Promise((resolve, reject) => { - ipfsd.disposableApi((err, ipfs) => { - if(err) console.error(err); - resolve(ipfs); - }); - }); -}; +const query = () => { + const index = Math.floor(Math.random() * creatures.length) + db.add(creatures[index]) + .then(() => { + const latest = db.iterator({ limit: 5 }).collect() + let output = `` + output += `---------------------------------------------------\n` + output += `Latest Visitors\n` + output += `---------------------------------------------------\n` + output += latest.reverse().map((e) => e.payload.value).join('\n') + `\n` + console.log(output) + }) + .catch((e) => { + console.error(e.stack) + console.log("Make sure you have an IPFS daemon running at localhost:5001") + }) +} -let run = (async(() => { - try { - const ipfs = await(startIpfs()); - const orbit = await(OrbitDB.connect(network, username, password, ipfs)); - const db = await(orbit.eventlog(channelName)); - - let count = 1; - let running = false; - - setInterval(async(() => { - if(!running) { - running = true; - - let timer = new Timer(true); - await(db.add(prefix + count)); - console.log(`Query #${count} took ${timer.stop(true)} ms\n`); - - let timer2 = new Timer(true); - let items = db.iterator({ limit: -1 }).collect(); - console.log("----------------------------------------------------------------------------------------") - console.log("Hash | Timestamp | Value") - console.log("----------------------------------------------------------------------------------------") - console.log(items.map((e) => `${e.hash} | ${e.meta.ts} | ${e.value}`).join("\n")); - console.log("----------------------------------------------------------------------------------------") - console.log(`Query 2 #${count} took ${timer2.stop(true)} ms\n`); - - running = false; - count ++; - } - }), process.argv[6] ? process.argv[6] : 1000); - - } catch(e) { - console.error(e.stack); - console.log("Exiting...") - process.exit(1); - } -}))(); - -module.exports = run; +setInterval(query, 1000) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 703c280..278a5d5 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -11,7 +11,7 @@ const Pubsub = require('orbit-db-pubsub') const Cache = require('./Cache') class OrbitDB { - constructor(ipfs, id, options) { + constructor(ipfs, id = 'default', options = {}) { this._ipfs = ipfs this._pubsub = options && options.broker ? new options.broker(ipfs) : new Pubsub(ipfs) this.user = { id: id } @@ -52,15 +52,12 @@ class OrbitDB { /* Private methods */ _createStore(Store, dbname, options = { subscribe: true }) { - // if(!options) options = {} - // const replicate = options.subscribe !== undefined ? options.subscribe : true const store = new Store(this._ipfs, this.user.id, dbname, options) this.stores[dbname] = store return this._subscribe(store, dbname, options.subscribe, options) } _subscribe(store, dbname, subscribe = true, options) { - // if(subscribe === undefined) subscribe = true store.events.on('data', this._onData.bind(this)) store.events.on('write', this._onWrite.bind(this)) store.events.on('close', this._onClose.bind(this))