Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Conversation

smashwilson
Copy link
Contributor

@smashwilson smashwilson commented Apr 25, 2018

To lay some groundwork for converting StagingView to React, I'm converting our ListSelection models to be immutable.

Rather than updating the selection in-place:

const selection = new ListSelection({items: ['a', 'b', 'c'});
selection.selectFirstItem();

Mutation methods return a new ListSelection with the modification made:

const selection0 = new ListSelection({items: ['a', 'b', 'c']);
const selection1 = selection0.selectFirstItem();

This should make manipulating selection models in setState() calls safer and more ergonomic, because we can use its updater method variant:

prevState is a reference to the previous state. It should not be directly mutated. Instead, changes should be represented by building a new object based on the input from prevState and props.

actionMethod = () => {
  this.setState(prevState => ({
    theSelection: prevState.theSelection.selectFirstItem(), 
  }));
}
  • Make ListSelection immutable
  • Make CompositeListSelection immutable
  • Update the StagingView to use its CompositeListSelection immutably

@smashwilson
Copy link
Contributor Author

By the way, I did this for FilePatchSelection a long time ago when I converted the FilePatchController to React. So right now one of our selection models is immutable and the others are mutable, which bugs me. 😄

@smashwilson
Copy link
Contributor Author

:shipit: to prepare for the final phase of the React port 🤘

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants