Skip to content

Commit 6e02905

Browse files
refactor(Transition): rename get*Hooks to build*Hooks. move some code around.
1 parent fc2a888 commit 6e02905

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

src/transition/hookBuilder.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import {IPromise} from "angular";
3-
import {IInjectable, extend, tail, isPromise, isArray, assertPredicate, unnestR} from "../common/common";
3+
import {IInjectable, extend, tail, isPromise, isArray, assertPredicate, unnestR, flatten, identity} from "../common/common";
44
import {runtime} from "../common/angular1";
55

66
import {ITransitionOptions, ITransitionHookOptions, IHookRegistry, ITreeChanges, IEventHook, ITransitionService} from "./interface";
@@ -55,14 +55,25 @@ export default class HookBuilder {
5555
// onBefore/onStart/onFinish/onSuccess/onError returns an array of hooks
5656
// onExit/onRetain/onEnter returns an array of arrays of hooks
5757

58-
getOnBeforeHooks = () => this._getTransitionHooks("onBefore", this.treeChanges.from);
59-
getOnStartHooks = () => this._getTransitionHooks("onStart", this.treeChanges.to);
60-
getOnExitHooks = () => this._getNodeHooks("onExit", this.treeChanges.exiting.reverse(), (node) => this._toFrom({ from: node.state }));
61-
getOnRetainHooks = () => this._getNodeHooks("onRetain", this.treeChanges.retained, (node) => this._toFrom());
62-
getOnEnterHooks = () => this._getNodeHooks("onEnter", this.treeChanges.entering, (node) => this._toFrom({ to: node.state }));
63-
getOnFinishHooks = () => this._getTransitionHooks("onFinish", this.treeChanges.to, { $treeChanges$: this.treeChanges });
64-
getOnSuccessHooks = () => this._getTransitionHooks("onSuccess", this.treeChanges.to, {}, successErrorOptions);
65-
getOnErrorHooks = () => this._getTransitionHooks("onError", this.treeChanges.to, {}, successErrorOptions);
58+
getOnBeforeHooks = () => this._buildTransitionHooks("onBefore");
59+
getOnStartHooks = () => this._buildTransitionHooks("onStart");
60+
getOnExitHooks = () => this._buildNodeHooks("onExit", this.treeChanges.exiting.reverse(), (node) => this._toFrom({ from: node.state }));
61+
getOnRetainHooks = () => this._buildNodeHooks("onRetain", this.treeChanges.retained, (node) => this._toFrom());
62+
getOnEnterHooks = () => this._buildNodeHooks("onEnter", this.treeChanges.entering, (node) => this._toFrom({ to: node.state }));
63+
getOnFinishHooks = () => this._buildTransitionHooks("onFinish", { $treeChanges$: this.treeChanges });
64+
getOnSuccessHooks = () => this._buildTransitionHooks("onSuccess", {}, {async: false, rejectIfSuperseded: false});
65+
getOnErrorHooks = () => this._buildTransitionHooks("onError", {}, {async: false, rejectIfSuperseded: false});
66+
67+
68+
asyncHooks() {
69+
let onStartHooks = this.getOnStartHooks();
70+
let onExitHooks = this.getOnExitHooks();
71+
let onRetainHooks = this.getOnRetainHooks();
72+
let onEnterHooks = this.getOnEnterHooks();
73+
let onFinishHooks = this.getOnFinishHooks();
74+
75+
return flatten([onStartHooks, onExitHooks, onRetainHooks, onEnterHooks, onFinishHooks]).filter(identity);
76+
}
6677

6778
private _toFrom(toFromOverride?): IToFrom {
6879
return extend({ to: this.toState, from: this.fromState }, toFromOverride);
@@ -75,8 +86,8 @@ export default class HookBuilder {
7586
* Finds all registered IEventHooks which matched the hookType and toFrom criteria.
7687
* A TransitionHook is then built from each IEventHook with the context, locals, and options provided.
7788
*/
78-
private _getTransitionHooks(hookType: string, context: (Node[]|State), locals = {}, options: ITransitionHookOptions = {}) {
79-
let node = tail(this.treeChanges.to);
89+
private _buildTransitionHooks(hookType: string, locals = {}, options: ITransitionHookOptions = {}) {
90+
let context = this.treeChanges.to, node = tail(context);
8091
options.traceData = { hookType, context };
8192

8293
const transitionHook = eventHook => this.buildHook(node, eventHook.callback, locals, options);
@@ -92,7 +103,7 @@ export default class HookBuilder {
92103
* Finds all registered IEventHooks which matched the hookType and toFrom criteria.
93104
* A TransitionHook is then built from each IEventHook with the context, locals, and options provided.
94105
*/
95-
private _getNodeHooks(hookType: string, path: Node[], toFromFn: (node: Node) => IToFrom, locals: any = {}, options: ITransitionHookOptions = {}) {
106+
private _buildNodeHooks(hookType: string, path: Node[], toFromFn: (node: Node) => IToFrom, locals: any = {}, options: ITransitionHookOptions = {}) {
96107
const hooksForNode = (node: Node) => {
97108
let toFrom = toFromFn(node);
98109
options.traceData = { hookType, context: node };
@@ -108,7 +119,7 @@ export default class HookBuilder {
108119
/** Given a node and a callback function, builds a TransitionHook */
109120
buildHook(node: Node, fn: IInjectable, locals?, options: ITransitionHookOptions = {}): TransitionHook {
110121
let _options = extend({}, this.baseHookOptions, options);
111-
return new TransitionHook(node.state, fn, extend({}, locals), node.resolveContext, _options);
122+
return new TransitionHook(fn, extend({}, locals), node.resolveContext, _options);
112123
}
113124

114125

src/transition/transitionHook.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ let defaultOptions = {
2020
};
2121

2222
export default class TransitionHook {
23-
constructor(private state: State,
24-
private fn: IInjectable,
23+
constructor(private fn:IInjectable,
2524
private locals: any,
2625
private resolveContext: ResolveContext,
2726
private options: ITransitionHookOptions) {

src/transition/transitionRunner.ts

+7-29
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,25 @@ export default class TransitionRunner {
1313
}
1414

1515
run(): IPromise<any> {
16-
const runSuccessHooks = () => runSynchronousHooks(this.success(), {}, true);
17-
const runErrorHooks = ($error$) => runSynchronousHooks(this.error(), { $error$ }, true);
16+
let hookBuilder = this.hookBuilder;
17+
const runSuccessHooks = () => runSynchronousHooks(hookBuilder.getOnSuccessHooks(), {}, true);
18+
const runErrorHooks = ($error$) => runSynchronousHooks(hookBuilder.getOnErrorHooks(), { $error$ }, true);
1819
// Run the success/error hooks *after* the Transition promise is settled.
1920
this.transition.promise.then(runSuccessHooks, runErrorHooks);
2021

2122
// ---- Synchronous hooks ----
2223
// Run the "onBefore" sync hooks
2324
// The results of the sync hooks is an async promise chain (which gets rejected or resolved)
24-
let chain = runSynchronousHooks(this.before());
25+
let chain = runSynchronousHooks(hookBuilder.getOnBeforeHooks());
2526

2627
// ---- Asynchronous section ----
2728
// Chain off the promise, build the remainder of the chain using each async step.
28-
chain = this.async().reduce((_chain, step) => _chain.then(step.invokeStep), chain);
29-
30-
// Make sure to settle the Transition promise, using the supplied callbacks and return the full chain.
31-
return chain.then(this._resolve, this._reject);
32-
}
33-
34-
before() {
35-
return this.hookBuilder.getOnBeforeHooks();
36-
}
37-
38-
async() {
39-
let hookBuilder = this.hookBuilder;
4029
// Build the async hooks *after* running onBefore hooks.
4130
// The synchronous onBefore hooks may register additional async hooks on-the-fly.
42-
let onStartHooks = hookBuilder.getOnStartHooks();
43-
let onExitHooks = hookBuilder.getOnExitHooks();
44-
let onRetainHooks = hookBuilder.getOnRetainHooks();
45-
let onEnterHooks = hookBuilder.getOnEnterHooks();
46-
let onFinishHooks = hookBuilder.getOnFinishHooks();
47-
48-
return flatten([onStartHooks, onExitHooks, onRetainHooks, onEnterHooks, onFinishHooks]).filter(identity);
49-
}
31+
chain = hookBuilder.asyncHooks().reduce((_chain, step) => _chain.then(step.invokeStep), chain);
5032

51-
success() {
52-
return this.hookBuilder.getOnSuccessHooks();
53-
}
54-
55-
error() {
56-
return this.hookBuilder.getOnErrorHooks();
33+
// Make sure to settle the Transition promise, using the supplied callbacks and return the full chain.
34+
return chain.then(this._resolve, this._reject);
5735
}
5836
}
5937

0 commit comments

Comments
 (0)