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

Run only required synchronous onBefore hooks #2

Merged
merged 1 commit into from
Oct 21, 2016
Merged

Run only required synchronous onBefore hooks #2

merged 1 commit into from
Oct 21, 2016

Conversation

csvn
Copy link
Contributor

@csvn csvn commented Oct 21, 2016

This pull request aims to solve the issue discussed in angular-ui/ui-router#3091

@csvn
Copy link
Contributor Author

csvn commented Oct 21, 2016

I added tests to make sure I did not break onSuccess or onError, since all of those hooks are supposed to run. Feel free to point out any suggestions.

Copy link
Member

@christopherthielen christopherthielen left a comment

Choose a reason for hiding this comment

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

Really great PR, thanks so much! I offered a couple of minor suggestions

* Run all TransitionHooks, ignoring their return value.
*/
static runAllHooks(hooks: TransitionHook[]): void {
hooks.map(hook => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you change this to forEach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, makes sense. Fixed.

errorHandler(exception);
// Break on first thrown error or false/TargetState
if (Rejection.isTransitionRejectionPromise(hookResult)) {
return hookResult;
Copy link
Member

Choose a reason for hiding this comment

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

I think this block could read a little nicer like so:

for (let hook of hooks) {
  try {
    let hookResult = hook.invokeHook();

    if (Rejection.isTransitionRejectionPromise(hookResult)) {
      // Break on first thrown error or false/TargetState
      return hookResult;
    }

    results.push(hookResult);
  } catch (exception) {
    return Rejection.errored(exception).toPromise();
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it improved from before, but this is even nicer. No need to check for TransitionRejection for known errors. Fixed.

@@ -317,7 +372,9 @@ describe('transition', function () {
.then(done);
}));

it('should be called even if other .onSuccess() callbacks fail (throw errors, etc)', ((done) => {
it('should call all .onSuccess() even when callbacks fail (throw errors, etc)', ((done) => {
Copy link
Member

Choose a reason for hiding this comment

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

lovely, thanks for adding these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was worried about breaking existing functionality, so I thought it best to make sure :)

@@ -364,6 +421,33 @@ describe('transition', function () {
.then(() => expect(hooks).toEqual([ 'splatsplat', 'AD' ]))
.then(done);
}));

it('should call all error handlers when transition fails.', ((done) => {
Copy link
Member

Choose a reason for hiding this comment

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

👍

first hook that modifies the transition.

updated request changes
@christopherthielen christopherthielen merged commit 8a45d04 into ui-router:master Oct 21, 2016
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