diff --git a/examples/react-example1/src/App.js b/examples/react-example1/src/App.js index d8abc45..288486a 100644 --- a/examples/react-example1/src/App.js +++ b/examples/react-example1/src/App.js @@ -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()), diff --git a/src/subspace.js b/src/subspace.js index 43ff124..d92d50d 100644 --- a/src/subspace.js +++ b/src/subspace.js @@ -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) {