-
Notifications
You must be signed in to change notification settings - Fork 38
Cleaner code #17
Cleaner code #17
Changes from 1 commit
8a59985
f113991
5e613d4
9ecae21
660c5ef
6e99b67
e4298fa
bb5509c
1008d5a
3aa360b
ea75324
7c03c5c
80b283f
a46b19a
3447e95
1f0c25a
b4404e2
165df16
9b8edd8
fb89a94
439d3bd
18e640f
431fe51
44dde4a
15caa62
0cbe22a
85f8fb9
c5e0c56
82fca45
4893e97
e449a57
3a885fe
c6ee32e
041441b
8a066db
d2bb3c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,9 @@ import { abiEncode } from '@parity/api/lib/util/encode'; | |
| import * as memoizee from 'memoizee'; | ||
|
|
||
| import { Address } from '../../types'; | ||
| import { createApiFromProvider, getApi } from '../../api'; | ||
| import createRpc from '../utils/createRpc'; | ||
| import { switchMapPromise } from '../../utils/operators'; | ||
| import api from '../../api'; | ||
| import frequency from '../../frequency'; | ||
| import { post$ } from './post'; | ||
|
|
||
|
|
@@ -32,7 +32,10 @@ interface MakeContract { | |
| * @return - The contract object as defined in @parity/api. | ||
| */ | ||
| const getContract = memoizee( | ||
| (address: Address, abiJson: any[]) => api().newContract(abiJson, address), // use types from @parity/abi | ||
| (address: Address, abiJson: any[], provider: any) => { | ||
| const api = provider ? createApiFromProvider(provider) : getApi(); | ||
| return api.newContract(abiJson, address); | ||
| }, // use types from @parity/abi | ||
| { length: 1 } // Only memoize by address | ||
|
||
| ); | ||
|
|
||
|
|
@@ -46,15 +49,15 @@ const getContract = memoizee( | |
| * function resolves. | ||
| */ | ||
| export const makeContract = memoizee( | ||
| (address: Address, abiJson: any[]) => { | ||
| // use types from @parity/abi | ||
| const abi = new Abi(abiJson); | ||
| (address: Address, abiJson: any[], options: { provider?: any } = {}) => { | ||
| const { provider } = options; | ||
| const abi = new Abi(abiJson); // use types from @parity/abi | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memoization needs to be done based on address and api (same as above)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so it's a bit more tricky here |
||
| // Variable result will hold the final object to return | ||
| const result: MakeContract = { | ||
| abi, | ||
| address, | ||
| get contractObject() { | ||
| return getContract(address, abiJson); | ||
| return getContract(address, abiJson, provider); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -66,7 +69,7 @@ export const makeContract = memoizee( | |
| // We only get the contract when the function is called for the 1st | ||
| // time. Note: getContract is memoized, won't create contract on each | ||
| // call. | ||
| const contract = getContract(address, abiJson); | ||
| const contract = getContract(address, abiJson, provider); | ||
| const method = contract.instance[name]; // Hold the method from the Abi | ||
|
|
||
| // The last arguments in args can be an options object | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,9 @@ | |
| import * as eth from './eth'; | ||
| import { memoizeAll } from '../utils/memoizeAll'; | ||
| import * as net from './net'; | ||
| import { post$ } from './other'; | ||
| import * as parity from './parity'; | ||
|
|
||
| const rpc = { ...eth, ...net, ...parity }; | ||
| const rpc = { ...eth, ...net, ...parity, post$ }; | ||
|
|
||
| export default memoizeAll(rpc); | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,17 @@ | |
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| import * as Api from '@parity/api'; | ||
| import { isFunction } from '@parity/api/lib/util/types'; | ||
| import { merge, ReplaySubject, Observable, OperatorFunction } from 'rxjs'; | ||
| import { multicast, refCount } from 'rxjs/operators'; | ||
| import * as prune from 'json-prune'; | ||
|
|
||
| import { getApi } from '../../api'; | ||
| import { Metadata, RpcObservable, RpcObservableOptions } from '../../types'; | ||
| import { createApiFromProvider, getApi } from '../../api'; | ||
| import { | ||
| distinctValues, | ||
| withoutLoading as withoutLoadingOperator | ||
| } from '../../utils/operators'; | ||
| import { Metadata, RpcObservable, RpcObservableOptions } from '../../types'; | ||
|
|
||
| interface RpcObservableWithoutMetadata<_, Out> { | ||
| (...args: any[]): Observable<Out>; | ||
|
|
@@ -60,7 +59,7 @@ const createRpc = <Source, Out>(metadata: Metadata<Source, Out>) => ( | |
| options: RpcObservableOptions = {} | ||
| ) => { | ||
| const { provider, withoutLoading } = options; | ||
| const api = provider ? new Api(provider) : getApi(); | ||
| const api = provider ? createApiFromProvider(provider) : getApi(); | ||
| // rpc$ will hold the RpcObservable minus its metadata | ||
| const rpc$: RpcObservableWithoutMetadata<Source, Out> = (...args: any[]) => { | ||
|
||
| // The source Observable can either be another RpcObservable (in the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memoization needs to be done based on address &
api(provider ? createApiFromProvider(provider) : getApi()), otherwise if we dosetApi() getContract() setApi() getContract()the lastgetContract()will return the memoized return value from the firstgetContract()call with the old api