Merge pull request #44 from status-im/misc_improvements

tolerate single param as arg, add `map` functionality
This commit is contained in:
Iuri Matias 2019-10-03 16:00:01 -04:00 committed by GitHub
commit d67bbd41b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View File

@ -18,17 +18,17 @@ class App extends React.Component {
};
async componentDidMount() {
const subspace = new Subspace(web3.currentProvider);
const subspace = new Subspace(web3.currentProvider);
await subspace.init();
Product = await ProductContract.getInstance();
const rating$ = subspace.trackEvent(Product, "Rating").pipe(map(x => parseInt(x.rating)));
const rating$ = subspace.trackEvent(Product, "Rating").map("rating").pipe(map(x => parseInt(x)));
window.Product = Product;
window.web3 = web3;
this.setState({
title: subspace.trackProperty(Product, "products", [0]).pipe(map(x => x.title)),
title: subspace.trackProperty(Product, "products", 0).map('title'),
averageRating: rating$.pipe($average()),
minRating: rating$.pipe($min()),
maxRating: rating$.pipe($max()),

View File

@ -1,5 +1,5 @@
import { ReplaySubject } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
import { distinctUntilChanged, map } from 'rxjs/operators';
import equal from 'fast-deep-equal';
import Database from './database.js';
import Events from 'events';
@ -51,7 +51,24 @@ export default class Subspace {
// TODO: get contract abi/address instead
trackEvent(contractInstance, eventName, filterConditionsOrCb) {
return this.eventSyncer.track(contractInstance, eventName, filterConditionsOrCb, this.latestBlockNumber - this.options.refreshLastNBlocks);
let returnSub = this.eventSyncer.track(contractInstance, eventName, filterConditionsOrCb, this.latestBlockNumber - this.options.refreshLastNBlocks);
returnSub.map = (prop) => {
return returnSub.pipe(map((x) => {
if (typeof(prop) === "string") {
return x[prop];
}
if (Array.isArray(prop)) {
let newValues = {}
prop.forEach((p) => {
newValues[p] = x[p]
})
return newValues
}
}))
}
return returnSub;
}
clearDB(collection) {
@ -95,6 +112,10 @@ export default class Subspace {
trackProperty(contractInstance, propName, methodArgs = [], callArgs = {}) {
const sub = new ReplaySubject();
if (!Array.isArray(methodArgs)) {
methodArgs = [methodArgs]
}
const method = contractInstance.methods[propName].apply(contractInstance.methods[propName], methodArgs)
const callContractMethod = () => {
method.call.apply(method.call, [callArgs, (err, result) => {
@ -110,7 +131,24 @@ export default class Subspace {
this.callables.push(callContractMethod);
return sub.pipe(distinctUntilChanged((a, b) => equal(a, b)));
let returnSub = sub.pipe(distinctUntilChanged((a, b) => equal(a, b)));
returnSub.map = (prop) => {
return returnSub.pipe(map((x) => {
if (typeof(prop) === "string") {
return x[prop];
}
if (Array.isArray(prop)) {
let newValues = {}
prop.forEach((p) => {
newValues[p] = x[p]
})
return newValues
}
}))
}
return returnSub;
}
trackBalance(address, erc20Address) {