33import { extend , tail , assertPredicate , unnestR , identity } from "../common/common" ;
44import { isArray } from "../common/predicates" ;
55
6- import { TransitionOptions , TransitionHookOptions , IHookRegistry , TreeChanges , IEventHook , IMatchingNodes } from "./interface" ;
6+ import {
7+ TransitionOptions , TransitionHookOptions , IHookRegistry , TreeChanges , IEventHook , IMatchingNodes ,
8+ TransitionHookPhase , TransitionHookScope
9+ } from "./interface" ;
710
811import { Transition } from "./transition" ;
912import { TransitionHook } from "./transitionHook" ;
1013import { State } from "../state/stateObject" ;
1114import { PathNode } from "../path/node" ;
1215import { TransitionService } from "./transitionService" ;
13- import { ResolveContext } from "../resolve/resolveContext " ;
16+ import { TransitionHookType } from "./transitionHookType " ;
1417
1518/**
1619 * This class returns applicable TransitionHooks for a specific Transition instance.
@@ -28,37 +31,32 @@ import {ResolveContext} from "../resolve/resolveContext";
2831 */
2932export class HookBuilder {
3033
34+ private $transitions : TransitionService ;
35+ private baseHookOptions : TransitionHookOptions ;
36+
3137 treeChanges : TreeChanges ;
3238 transitionOptions : TransitionOptions ;
3339
3440 toState : State ;
3541 fromState : State ;
3642
37- constructor ( private $transitions : TransitionService , private transition : Transition , private baseHookOptions : TransitionHookOptions ) {
43+ constructor ( private transition : Transition ) {
3844 this . treeChanges = transition . treeChanges ( ) ;
45+ this . transitionOptions = transition . options ( ) ;
3946 this . toState = tail ( this . treeChanges . to ) . state ;
4047 this . fromState = tail ( this . treeChanges . from ) . state ;
41- this . transitionOptions = transition . options ( ) ;
48+ this . $transitions = transition . router . transitionService ;
49+ this . baseHookOptions = < TransitionHookOptions > {
50+ transition : transition ,
51+ current : transition . options ( ) . current
52+ } ;
4253 }
4354
44- getOnBeforeHooks = ( ) => this . _buildNodeHooks ( "onBefore" , "to" , tupleSort ( ) ) ;
45- getOnStartHooks = ( ) => this . _buildNodeHooks ( "onStart" , "to" , tupleSort ( ) ) ;
46- getOnExitHooks = ( ) => this . _buildNodeHooks ( "onExit" , "exiting" , tupleSort ( true ) , { stateHook : true } ) ;
47- getOnRetainHooks = ( ) => this . _buildNodeHooks ( "onRetain" , "retained" , tupleSort ( false ) , { stateHook : true } ) ;
48- getOnEnterHooks = ( ) => this . _buildNodeHooks ( "onEnter" , "entering" , tupleSort ( false ) , { stateHook : true } ) ;
49- getOnFinishHooks = ( ) => this . _buildNodeHooks ( "onFinish" , "to" , tupleSort ( ) ) ;
50- getOnSuccessHooks = ( ) => this . _buildNodeHooks ( "onSuccess" , "to" , tupleSort ( ) , { rejectIfSuperseded : false } ) ;
51- getOnErrorHooks = ( ) => this . _buildNodeHooks ( "onError" , "to" , tupleSort ( ) , { rejectIfSuperseded : false } ) ;
52-
53- asyncHooks ( ) {
54- let onStartHooks = this . getOnStartHooks ( ) ;
55- let onExitHooks = this . getOnExitHooks ( ) ;
56- let onRetainHooks = this . getOnRetainHooks ( ) ;
57- let onEnterHooks = this . getOnEnterHooks ( ) ;
58- let onFinishHooks = this . getOnFinishHooks ( ) ;
59-
60- let asyncHooks = [ onStartHooks , onExitHooks , onRetainHooks , onEnterHooks , onFinishHooks ] ;
61- return asyncHooks . reduce ( unnestR , [ ] ) . filter ( identity ) ;
55+ buildHooksForPhase ( phase : TransitionHookPhase ) : TransitionHook [ ] {
56+ return this . $transitions . getTransitionHookTypes ( phase )
57+ . map ( type => this . buildHooks ( type ) )
58+ . reduce ( unnestR , [ ] )
59+ . filter ( identity ) ;
6260 }
6361
6462 /**
@@ -68,44 +66,35 @@ export class HookBuilder {
6866 * - Finds [[PathNode]] (or `PathNode[]`) to use as the TransitionHook context(s)
6967 * - For each of the [[PathNode]]s, creates a TransitionHook
7068 *
71- * @param hookType the name of the hook registration function, e.g., 'onEnter', 'onFinish'.
72- * @param matchingNodesProp selects which [[PathNode]]s from the [[IMatchingNodes]] object to create hooks for.
73- * @param getLocals a function which accepts a [[PathNode]] and returns additional locals to provide to the hook as injectables
74- * @param sortHooksFn a function which compares two HookTuple and returns <1, 0, or >1
75- * @param options any specific Transition Hook Options
69+ * @param hookType the type of the hook registration function, e.g., 'onEnter', 'onFinish'.
7670 */
77- private _buildNodeHooks ( hookType : string ,
78- matchingNodesProp : string ,
79- sortHooksFn : ( l : HookTuple , r : HookTuple ) => number ,
80- options ?: TransitionHookOptions ) : TransitionHook [ ] {
81-
71+ buildHooks ( hookType : TransitionHookType ) : TransitionHook [ ] {
8272 // Find all the matching registered hooks for a given hook type
83- let matchingHooks = this . _matchingHooks ( hookType , this . treeChanges ) ;
73+ let matchingHooks = this . _matchingHooks ( hookType . name , this . treeChanges ) ;
8474 if ( ! matchingHooks ) return [ ] ;
8575
8676 const makeTransitionHooks = ( hook : IEventHook ) => {
8777 // Fetch the Nodes that caused this hook to match.
8878 let matches : IMatchingNodes = hook . matches ( this . treeChanges ) ;
8979 // Select the PathNode[] that will be used as TransitionHook context objects
90- let matchingNodes : PathNode [ ] = matches [ matchingNodesProp ] ;
91-
92- // When invoking 'exiting' hooks, give them the "from path" for resolve data.
93- // Everything else gets the "to path"
94- let resolvePath = matchingNodesProp === 'exiting' ? this . treeChanges . from : this . treeChanges . to ;
95- let resolveContext = new ResolveContext ( resolvePath ) ;
80+ let matchingNodes : PathNode [ ] = matches [ hookType . criteriaMatchPath ] ;
9681
9782 // Return an array of HookTuples
9883 return matchingNodes . map ( node => {
99- let _options = extend ( { bind : hook . bind , traceData : { hookType, context : node } } , this . baseHookOptions , options ) ;
100- let state = _options . stateHook ? node . state : null ;
84+ let _options = extend ( {
85+ bind : hook . bind ,
86+ traceData : { hookType : hookType . name , context : node }
87+ } , this . baseHookOptions ) ;
88+
89+ let state = hookType . hookScope === TransitionHookScope . STATE ? node . state : null ;
10190 let transitionHook = new TransitionHook ( this . transition , state , hook , _options ) ;
10291 return < HookTuple > { hook, node, transitionHook } ;
10392 } ) ;
10493 } ;
10594
10695 return matchingHooks . map ( makeTransitionHooks )
10796 . reduce ( unnestR , [ ] )
108- . sort ( sortHooksFn )
97+ . sort ( tupleSort ( hookType . reverseSort ) )
10998 . map ( tuple => tuple . transitionHook ) ;
11099 }
111100
0 commit comments