From 52aac845f12d08739dffbeac64b8f03f7373a854 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 3 Oct 2019 10:58:49 -0400 Subject: [PATCH 1/2] if property is methodArgs is not an array transform it into one --- examples/react-example1/src/App.js | 2 +- src/subspace.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/react-example1/src/App.js b/examples/react-example1/src/App.js index d8abc45..27aaa8a 100644 --- a/examples/react-example1/src/App.js +++ b/examples/react-example1/src/App.js @@ -28,7 +28,7 @@ class App extends React.Component { window.web3 = web3; this.setState({ - title: subspace.trackProperty(Product, "products", [0]).pipe(map(x => x.title)), + title: subspace.trackProperty(Product, "products", 0).pipe(map(x => x.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..b05bb48 100644 --- a/src/subspace.js +++ b/src/subspace.js @@ -95,6 +95,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) => { From 85d8d80db89523a4bf9933a9d772d31a4a3d5c6e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 3 Oct 2019 11:21:05 -0400 Subject: [PATCH 2/2] implement map method for trackProperty and trackEvent --- examples/react-example1/src/App.js | 6 ++--- src/subspace.js | 40 +++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/examples/react-example1/src/App.js b/examples/react-example1/src/App.js index 27aaa8a..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 b05bb48..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) { @@ -114,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) {