Browse Source

feat: add ability to clear cached calls from state

develop
Ezerous 4 years ago
parent
commit
70ff3f1f30
  1. 2
      package.json
  2. 25
      src/DrizzleContract.js
  3. 1
      src/contracts/constants.js
  4. 24
      src/contracts/contractsReducer.js

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@ezerous/drizzle", "name": "@ezerous/drizzle",
"version": "0.4.3", "version": "0.5.0",
"description": "A reactive data-store for web3 and smart contracts.", "description": "A reactive data-store for web3 and smart contracts.",
"license": "MIT", "license": "MIT",
"author": "Ezerous <ezerous@gmail.com>", "author": "Ezerous <ezerous@gmail.com>",

25
src/DrizzleContract.js

@ -25,6 +25,7 @@ class DrizzleContract {
var item = this.abi[i] var item = this.abi[i]
if (isGetterFunction(item)) { if (isGetterFunction(item)) {
this.methods[item.name].cacheCall = this.cacheCallFunction(item.name, i) this.methods[item.name].cacheCall = this.cacheCallFunction(item.name, i)
this.methods[item.name].clearCacheCall = this.clearCacheCallFunction(item.name, i)
} }
if (isSetterFunction(item)) { if (isSetterFunction(item)) {
this.methods[item.name].cacheSend = this.cacheSendFunction(item.name, i) this.methods[item.name].cacheSend = this.cacheSendFunction(item.name, i)
@ -55,16 +56,13 @@ class DrizzleContract {
} }
cacheCallFunction (fnName, fnIndex, fn) { cacheCallFunction (fnName, fnIndex, fn) {
var contract = this const contract = this
return function () { return function () {
// Collect args and hash to use as key, 0x0 if no args // Collect args and hash to use as key, 0x0 if no args
var argsHash = '0x0' const args = arguments
var args = arguments const argsHash = args.length > 0 ? contract.generateArgsHash(args) : '0x0'
if (args.length > 0) {
argsHash = contract.generateArgsHash(args)
}
const contractName = contract.contractName const contractName = contract.contractName
const functionState = contract.store.getState().contracts[contractName][ const functionState = contract.store.getState().contracts[contractName][
fnName fnName
@ -92,6 +90,21 @@ class DrizzleContract {
} }
} }
clearCacheCallFunction (fnName, fnIndex) {
const contract = this
return function () {
// Collect args and hash to use as key, 0x0 if no args
const args = arguments
const argsHash = args.length > 0 ? contract.generateArgsHash(args) : '0x0'
contract.store.dispatch({
type: ContractActions.CLEAR_CALL_CONTRACT_FN,
contractName: contract.contractName,
fnName,
argsHash
})
}
}
cacheSendFunction (fnName, fnIndex, fn) { cacheSendFunction (fnName, fnIndex, fn) {
// NOTE: May not need fn index // NOTE: May not need fn index
var contract = this var contract = this

1
src/contracts/constants.js

@ -12,6 +12,7 @@ export const CONTRACT_SYNCED = 'CONTRACT_SYNCED'
export const CONTRACT_SYNC_IND = 'CONTRACT_SYNC_IND' export const CONTRACT_SYNC_IND = 'CONTRACT_SYNC_IND'
export const ERROR_CONTRACT_VAR = 'ERROR_CONTRACT_VAR' export const ERROR_CONTRACT_VAR = 'ERROR_CONTRACT_VAR'
export const CALL_CONTRACT_FN = 'CALL_CONTRACT_FN' export const CALL_CONTRACT_FN = 'CALL_CONTRACT_FN'
export const CLEAR_CALL_CONTRACT_FN = 'CLEAR_CALL_CONTRACT_FN'
export const SEND_CONTRACT_TX = 'SEND_CONTRACT_TX' export const SEND_CONTRACT_TX = 'SEND_CONTRACT_TX'
export const ADD_CONTRACT = 'ADD_CONTRACT' export const ADD_CONTRACT = 'ADD_CONTRACT'
export const ERROR_ADD_CONTRACT = 'ERROR_ADD_CONTRACT' export const ERROR_ADD_CONTRACT = 'ERROR_ADD_CONTRACT'

24
src/contracts/contractsReducer.js

@ -100,6 +100,30 @@ const contractsReducer = (state = initialState, action) => {
} }
} }
if (action.type === ContractActions.CLEAR_CALL_CONTRACT_FN) {
const { argsHash, contractName, fnName } = action;
if(argsHash !== '0x0'){
const {[argsHash]: _, ...remainingHashes} = state[contractName][fnName];
return {
...state,
[contractName]: {
...state[contractName],
[fnName]: {
...remainingHashes
}
}
}
}
return {
...state,
[contractName]: {
...state[contractName],
[fnName]: { }
}
}
}
if (action.type === ContractActions.ERROR_CONTRACT_VAR) { if (action.type === ContractActions.ERROR_CONTRACT_VAR) {
return { return {
...state, ...state,

Loading…
Cancel
Save