magmo / apps

Proof of concept applications that use the Force Move Game framework.
9 stars 5 forks source link

`sharedData = someModifier(sharedData)` #627

Open andrewgordstewart opened 5 years ago

andrewgordstewart commented 5 years ago

As Alex pointed out here, the following does not modify sharedData out side the scope of someModifier:

function someModifier(sharedData) {
  sharedData = {...sharedData, modified: true }
}

someModifier({ modified: false }) // does not modify `sharedData`

Therefore, we can avoid having to declare a new variable when we want to return a modified sharedData

function someModifier(sharedData) {
  newSharedData = {...sharedData, modified: true }
  return sharedData
}

[Here])(https://github.com/magmo/apps/blob/master/packages/wallet/src/redux/protocols/concluding/instigator/reducer.ts#L122-L125) is an example of where this is performed.

We could instead do the following:

  let updatedConsensusUpdateState: ConsensusUpdateState;
  ({ protocolState: updatedConsensusUpdateState, sharedData } = consensusUpdateReducer(
    protocolState.consensusUpdateState,
    sharedData,
    action,
  ));

This would prevent the type of bug where some reducer declares a side effect in its returned sharedData, but the parent reducer forgets to return sharedData: newSharedData.