Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add updateNestedCollectibleState to clean up redundant nested state update logic #665

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address feedback
  • Loading branch information
adonesky1 committed Dec 16, 2021
commit 90f1c9bd13eba38025530f8288896ac6ee6f1efd
20 changes: 16 additions & 4 deletions src/assets/CollectiblesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,32 @@ export class CollectiblesController extends BaseController<
*
* @param newCollection - the modified piece of state to update in the controller's store
* @param baseStateKey - The root key in the store to update.
* @param passedConfig - An object containing the selectedAddress and chainId that are passed through the auto-detection flow.
* @param passedConfig.selectedAddress - the address passed through the collectible detection flow to ensure detected assets are stored to the correct account
* @param passedConfig.chainId - the chainId passed through the collectible detection flow to ensure detected assets are stored to the correct account
*/
private updateNestedCollectibleState(
newCollection: Collectible[] | CollectibleContract[],
baseStateKey: 'allCollectibles' | 'allCollectibleContracts',
passedConfig?: { selectedAddress: string; chainId: string },
) {
const { chainId, selectedAddress } = this.config;
// We want to use the passedSelectedAddress and passedChainId when defined and not null
// these values are passed through the collectible detection flow, meaning they may not
// match as the currently configured values (which may be stale for this update)
const address =
passedConfig?.selectedAddress ?? this.config.selectedAddress;
const chain = passedConfig?.chainId ?? this.config.chainId;

const { [baseStateKey]: oldState } = this.state;

const addressState = oldState[selectedAddress];
const addressState = oldState[address];
const newAddressState = {
...addressState,
...{ [chainId]: newCollection },
...{ [chain]: newCollection },
};
const newState = {
...oldState,
...{ [selectedAddress]: newAddressState },
...{ [address]: newAddressState },
};

this.update({
Expand Down Expand Up @@ -585,6 +595,7 @@ export class CollectiblesController extends BaseController<
this.updateNestedCollectibleState(
newCollectibles,
ALL_COLLECTIBLES_STATE_KEY,
{ chainId, selectedAddress },
);

return newCollectibles;
Expand Down Expand Up @@ -672,6 +683,7 @@ export class CollectiblesController extends BaseController<
this.updateNestedCollectibleState(
newCollectibleContracts,
ALL_COLLECTIBLES_CONTRACTS_STATE_KEY,
{ chainId, selectedAddress },
);

return newCollectibleContracts;
Expand Down