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

refactor: reduce amount of work on initialization #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alexlafroscia
Copy link
Collaborator

@alexlafroscia alexlafroscia commented Dec 24, 2018

  • Simplify approach to wrapping most properties
  • Reduce the amount of work done when initializing objects

This is a follow up to some of the feedback given in #85

set(newValue) {
initialized = true;

runValidator(validator, this.constructor, key, newValue, 'set');
Copy link
Contributor

@pzuraq pzuraq Jan 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I'm realizing is that unless we call super, the last class that defines the argument will always win:

class Foo {
  @argument('string') baz;
}

class Bar extends Foo {
  @argument('number') baz;
}

We may need to store all of the validators in an array with addValidationFor and then run all of them in the setter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm following -- Are the usage in Foo and Bar related, even though the classes are independent?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sorry, that was a typo, corrected

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. We'd talked about this briefly earlier -- we have a few options for defining arguments for the same property on multiple components in a hierarchy

  1. First one (lowest in the chain) is run
  2. Last one (highest in the chain) is run
  3. All of them are run

We currently do the first option there, but I'm happy to refactor to run all of them. That should be pretty easy now with our and combinator!


wrapComputedProperty(constructor, this, meta, validation, key);

runValidation(validation, constructor, key, this[key], 'init');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to run validations here, since class fields and getters/setters will trigger when the values are set anyways. I suppose the init part of the error message is still somewhat valuable, but it probably won't show up anyways since the error for the accessor will trigger first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would remove the init validations altogether, right?

I'm fine with that, personally -- I think it probably makes more sense just to run the validations on get/set

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I looked into this and I'm not sure that it'll make sense to remove the init validation.

I tried removing the validator on init and adding a validator to get. This had the side-effect of making it impossible to check that the props passed to the object match the expected validation, because as part of initializing the object in .create(), the initial value is accessed. Basically, the validation on get will be called before the property is overwritten with the new value, which doesn't work.

@@ -26,7 +26,7 @@ module('Integration | Component Behavior', function(hooks) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add some tests that cover inheritance behavior, to make sure that it works correctly.

Copy link
Collaborator Author

@alexlafroscia alexlafroscia Feb 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in tests/unit/argument-test.js should cover the inheritance use-case -- there are a number of examples there around extending classes with @argument defined in a base class and overwritten (or not) in a parent class.

@alexlafroscia alexlafroscia force-pushed the better-property-wrapping branch from ea045f6 to db4fcbd Compare January 7, 2019 19:13
@alexlafroscia
Copy link
Collaborator Author

^ Rebased to make compatible with the conflicting changes in #90

- Simplify approach to wrapping most properties
- Reduce the amount of work done when initializing objects
It wasn't previously clear that the `wrapComputedProperty` behavior is
only invoked for computed properties. This code functions the same, but
makes it easier to see that the computed property wrapping is only
sometimes performed
@alexlafroscia alexlafroscia force-pushed the better-property-wrapping branch from db4fcbd to c670ce8 Compare February 21, 2019 05:13
@alexlafroscia
Copy link
Collaborator Author

New ComputedProperty implementation in in 3.9 trolling our tests... I'll take a look at adding ember-compatibility-helpers to address that issue, but would appreciate a re-review with the feedback I've left on the various "conversations" still open here!

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

Successfully merging this pull request may close these issues.

2 participants