This commit is contained in:
Iuri Matias 2019-10-02 17:58:42 -04:00
parent 6f1afc42a4
commit ee5d56397c
9 changed files with 35 additions and 100 deletions

View File

@ -22,7 +22,6 @@ class App extends React.Component {
await subspace.init();
Product = await ProductContract.getInstance();
console.dir(Product.options)
const rating$ = subspace.trackEvent(Product, "Rating").pipe(map(x => parseInt(x.rating)));
window.Product = Product;

View File

@ -25,8 +25,6 @@ const getENV = function () {
return 'CORDOVA';
};
class Database {
constructor(dbFilename, events, cb) {
@ -69,7 +67,6 @@ class Database {
};
}
getEventsFor(eventKey) {
let children = this.db.getCollection(eventKey);
return children.find();

View File

@ -2,11 +2,11 @@ import { fromEvent, ReplaySubject } from 'rxjs';
import hash from 'object-hash';
class EventSyncer {
constructor(web3, events, db) {
this.events = events;
this.web3 = web3;
this.db = db;
this.subscriptions = [];
}

View File

@ -1,30 +0,0 @@
// var Web3 = require('web3')
const Events = require('events')
const { map, scan } = require('rxjs/operators');
const Simulator = require('./simulator.js')
const EventSyncer = require('./eventSyncer.js')
const events = new Events()
const eventSyncer = new EventSyncer(events);
eventSyncer.init(run);
function run() {
let myscan = scan((acc, curr) => {
acc.push(curr);
if (acc.length > 4) {
acc.shift();
}
return acc;
}, [])
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => {
console.dir("current average is " + v)
})
const simulator = new Simulator(events);
simulator.emitEvents()
}

View File

@ -1,27 +0,0 @@
class Simulator {
constructor(events) {
this.events = events;
this.contractEvents = [
{ id: 1, from: "0x123", type: "Rating", rating: 3 },
{ id: 2, from: "0x123", type: "Rating", rating: 1 },
{ id: 3, from: "0x234", type: "Rating", rating: 5 },
{ id: 4, from: "0x123", type: "Rating", rating: 4 },
{ id: 5, from: "0x123", type: "Rating", rating: 2 },
{ id: 6, from: "0x342", type: "Rating", rating: 2 }
]
}
emitEvents() {
let i = 0
// emit contract event each 1 second
setInterval(() => {
if (i >= this.contractEvents.length) return
this.events.emit("contractEvent", this.contractEvents[i])
i += 1
}, 1 * 1000)
}
}
module.exports = Simulator;

View File

@ -13,7 +13,6 @@ import LogSyncer from './logSyncer';
export default class Subspace {
constructor(provider, options = {}) {
if(provider.constructor.name !== "WebsocketProvider"){
console.warn("subspace: it's recommended to use a websocket provider to react to new events");
}
@ -63,7 +62,6 @@ export default class Subspace {
return this.logSyncer.track(options);
}
_initNewBlocksSubscription() {
if(this.newBlocksSubscription != null || this.options.callInterval !== 0) return;
@ -87,7 +85,6 @@ export default class Subspace {
fn();
});
}, this.options.callInterval);
}
// TODO: should save value in database?

View File

@ -3,7 +3,6 @@ export function randomString() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
export function isAddress(address) {
return /^(0x)?[0-9a-fA-F]{40}$/i.test(address)
};