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

Collapse consecutive if statements with the same condition #450

Closed
Rich-Harris opened this issue Apr 4, 2017 · 1 comment
Closed

Collapse consecutive if statements with the same condition #450

Rich-Harris opened this issue Apr 4, 2017 · 1 comment

Comments

@Rich-Harris
Copy link
Member

E.g. if you have two computed values with the same dependencies (REPL), the generated code looks like this:

function recompute ( state, newState, oldState, isInitial ) {
  if ( isInitial || ( 'a' in newState && differs( state.a, oldState.a ) ) ) {
    state.x = newState.x = template.computed.x( state.a );
  }
	
  if ( isInitial || ( 'a' in newState && differs( state.a, oldState.a ) ) ) {
    state.y = newState.y = template.computed.y( state.a );
  }
}

It could look like this:

function recompute ( state, newState, oldState, isInitial ) {
  if ( isInitial || ( 'a' in newState && differs( state.a, oldState.a ) ) ) {
    state.x = newState.x = template.computed.x( state.a );
    state.y = newState.y = template.computed.y( state.a );
  }
}

Separately, I've been wondering about doing away with isInitial and just having a block like this at the start of the constructor:

function App ( options ) {
  options = options || {};
  this._state = options.data || {};
  this._state.x = template.computed.x( state.a );
  this._state.y = template.computed.y( state.a );

  // ...
}

// instead of this:
function App ( options ) {
  options = options || {};
  this._state = options.data || {};
  recompute( this._state, this._state, {}, true );

  // ...
}

Slightly more code, but maybe slightly less work? Not sure about this trade-off.

@Rich-Harris
Copy link
Member Author

fixed in 1.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant