Skip to content

Commit 67955df

Browse files
refactor(StateHandler): Create ES6 class for StateHandler
1 parent 8d47b87 commit 67955df

File tree

2 files changed

+75
-65
lines changed

2 files changed

+75
-65
lines changed

src/state/state.ts

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Glob from "./glob";
44
import StateQueueManager from "./stateQueueManager";
55
import StateBuilder from "./stateBuilder";
66
import StateMatcher from "./stateMatcher";
7+
import StateHandler from "./stateHandler";
78
import Queue from "../common/queue";
89
import {Transition} from "../transition/transition"
910
import {TransitionRejection, RejectType, RejectFactory} from "../transition/rejectFactory";
@@ -622,70 +623,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
622623
current: transQueue.peek.bind(transQueue)
623624
})
624625
));
625-
626-
var stateHandler = {
627-
628-
runTransition: function runTransition(transition) {
629-
// When the transition promise (prepromise; before callbacks) is resolved/rejected, update the $state service
630-
function handleSuccess() { return stateHandler.transitionSuccess(transition); }
631-
function handleFailure(error) { return stateHandler.transitionFailure(error); }
632-
transition.run();
633-
return transition.prepromise.then(handleSuccess, handleFailure);
634-
},
635-
636-
transitionSuccess: function transitionSuccess(transition) {
637-
// TODO: sync on entering/exiting state, not transition success?
638-
transition.views("exiting").forEach($view.reset.bind($view));
639-
$view.sync();
640-
transition.views("entering").forEach($view.registerStateViewConfig.bind($view));
641-
$view.sync();
642-
643-
// Update globals in $state
644-
$state.$current = transition.$to().$state();
645-
$state.current = transition.$to().state();
646-
647-
stateHandler.updateStateParams(transition);
648-
transQueue.clear();
649-
return transition;
650-
},
651-
652-
transitionFailure: function transitionFailure(error) {
653-
// Handle redirect and abort
654-
if (error instanceof TransitionRejection) {
655-
if (error.type === RejectType.IGNORED) {
656-
// Update $stateParmas/$state.params/$location.url if transition ignored, but dynamic params have changed.
657-
if (!$state.$current.params.$$filter(not(not(prop('dynamic')))).$$equals($stateParams, transition.params())) {
658-
stateHandler.updateStateParams(transition);
659-
}
660-
return $state.current;
661-
}
662-
663-
if (error.type === RejectType.SUPERSEDED) {
664-
//if (error.redirected && error.detail instanceof Transition) { // TODO: expose Transition class for instanceof
665-
if (error.redirected && error.detail instanceof Transition) {
666-
transQueue.enqueue(error.detail);
667-
return stateHandler.runTransition(error.detail);
668-
}
669-
}
670-
}
671-
672-
return $q.reject(error);
673-
},
674-
675-
updateStateParams: function updateStateParams(transition) {
676-
var options = transition.options();
677-
$state.params = transition.params();
678-
copy($state.params, $stateParams);
679-
$stateParams.$sync().$off();
680-
681-
if (options.location && $state.$current.navigable) {
682-
$urlRouter.push($state.$current.navigable.url, $stateParams, { replace: options.location === 'replace' });
683-
}
684-
685-
$urlRouter.update(true);
686-
}
687-
};
688-
626+
let stateHandler = new StateHandler($urlRouter, $view, $state, $stateParams, $q, transQueue);
689627
var result = stateHandler.runTransition(transition);
690628
result.finally(() => transQueue.remove(transition));
691629

@@ -1013,4 +951,4 @@ function $StateParamsProvider() {
1013951
angular.module('ui.router.state')
1014952
.provider('$stateParams', <IServiceProviderFactory> $StateParamsProvider)
1015953
.provider('$state', <IServiceProviderFactory> $StateProvider)
1016-
.run(['$state', function($state) { /* This effectively calls $get() to init when we enter runtime */ }]);
954+
.run(['$state', function($state) { /* This effectively calls $get() to init when we enter runtime */ }]);

src/state/stateHandler.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {TransitionRejection, RejectType} from "../transition/rejectFactory";
2+
import {Transition} from "../transition/transition";
3+
import {copy, prop} from "../common/common";
4+
5+
export default class StateHandler {
6+
constructor(private $urlRouter, private $view, private $state, private $stateParams, private $q, private transQueue) {
7+
8+
}
9+
10+
runTransition(transition) {
11+
// When the transition promise (prepromise; before callbacks) is resolved/rejected, update the $state service
12+
const handleSuccess = () => this.transitionSuccess(transition)
13+
const handleFailure = (error) => this.transitionFailure(transition, error)
14+
transition.run();
15+
return transition.prepromise.then(handleSuccess, handleFailure);
16+
};
17+
18+
transitionSuccess(transition) {
19+
let {$view, $state, transQueue} = this;
20+
// TODO: sync on entering/exiting state, not transition success?
21+
transition.views("exiting").forEach($view.reset.bind($view));
22+
$view.sync();
23+
transition.views("entering").forEach($view.registerStateViewConfig.bind($view));
24+
$view.sync();
25+
26+
// Update globals in $state
27+
$state.$current = transition.$to().$state();
28+
$state.current = transition.$to().state();
29+
30+
this.updateStateParams(transition);
31+
transQueue.clear();
32+
return transition;
33+
}
34+
35+
transitionFailure(transition, error) {
36+
let {$state, $stateParams, $q, transQueue} = this;
37+
// Handle redirect and abort
38+
if (error instanceof TransitionRejection) {
39+
if (error.type === RejectType.IGNORED) {
40+
// Update $stateParmas/$state.params/$location.url if transition ignored, but dynamic params have changed.
41+
if (!$state.$current.params.$$filter(prop('dynamic')).$$equals($stateParams, transition.params())) {
42+
this.updateStateParams(transition);
43+
}
44+
return $state.current;
45+
}
46+
47+
if (error.type === RejectType.SUPERSEDED) {
48+
//if (error.redirected && error.detail instanceof Transition) { // TODO: expose Transition class for instanceof
49+
if (error.redirected && error.detail instanceof Transition) {
50+
transQueue.enqueue(error.detail);
51+
return this.runTransition(error.detail);
52+
}
53+
}
54+
}
55+
56+
return $q.reject(error);
57+
}
58+
59+
updateStateParams(transition) {
60+
let {$urlRouter, $state, $stateParams} = this;
61+
var options = transition.options();
62+
$state.params = transition.params();
63+
copy($state.params, $stateParams);
64+
$stateParams.$sync().$off();
65+
66+
if (options.location && $state.$current.navigable) {
67+
$urlRouter.push($state.$current.navigable.url, $stateParams, { replace: options.location === 'replace' });
68+
}
69+
70+
$urlRouter.update(true);
71+
}
72+
}

0 commit comments

Comments
 (0)