Skip to content

Commit

Permalink
refactor(hooks): move each hooks definition and registration to the s…
Browse files Browse the repository at this point in the history
…ame file
  • Loading branch information
christopherthielen committed Aug 31, 2016
1 parent 37241e7 commit 1f98cee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
22 changes: 15 additions & 7 deletions src/hooks/onEnterExitRetain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {TransitionStateHookFn} from "../transition/interface";
import {State} from "../state/stateObject";
import {Transition} from "../transition/transition";
import {TransitionService} from "../transition/transitionService";

/**
* A factory which creates an onEnter, onExit or onRetain transition hook function
Expand All @@ -12,10 +13,10 @@ import {Transition} from "../transition/transition";
* @hidden
*/
function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
return (transition: Transition, state: State) => {
let hookFn: TransitionStateHookFn = state[hookName];
return hookFn(transition, state);
}
return (transition: Transition, state: State) => {
let hookFn: TransitionStateHookFn = state[hookName];
return hookFn(transition, state);
}
}

/**
Expand All @@ -27,7 +28,9 @@ function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
*
* See: [[IHookRegistry.onExit]]
*/
export const onExitHook: TransitionStateHookFn = makeEnterExitRetainHook('onExit');
const onExitHook: TransitionStateHookFn = makeEnterExitRetainHook('onExit');
export const registerOnExitHook = (transitionService: TransitionService) =>
transitionService.onExit({exiting: state => !!state.onExit}, onExitHook);

/**
* The [[TransitionStateHookFn]] for onRetain
Expand All @@ -38,7 +41,9 @@ export const onExitHook: TransitionStateHookFn = makeEnterExitRetainHook('o
*
* See: [[IHookRegistry.onRetain]]
*/
export const onRetainHook: TransitionStateHookFn = makeEnterExitRetainHook('onRetain');
const onRetainHook: TransitionStateHookFn = makeEnterExitRetainHook('onRetain');
export const registerOnRetainHook = (transitionService: TransitionService) =>
transitionService.onRetain({retained: state => !!state.onRetain}, onRetainHook);

/**
* The [[TransitionStateHookFn]] for onEnter
Expand All @@ -49,4 +54,7 @@ export const onRetainHook: TransitionStateHookFn = makeEnterExitRetainHook('o
*
* See: [[IHookRegistry.onEnter]]
*/
export const onEnterHook: TransitionStateHookFn = makeEnterExitRetainHook('onEnter');
const onEnterHook: TransitionStateHookFn = makeEnterExitRetainHook('onEnter');
export const registerOnEnterHook = (transitionService: TransitionService) =>
transitionService.onEnter({entering: state => !!state.onEnter}, onEnterHook);

9 changes: 7 additions & 2 deletions src/hooks/redirectTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {isString, isFunction} from "../common/predicates"
import {Transition} from "../transition/transition";
import {services} from "../common/coreservices";
import {TargetState} from "../state/targetState";
import {TransitionService} from "../transition/transitionService";
import {TransitionHookFn} from "../transition/interface";

/**
* A [[TransitionHookFn]] that redirects to a different state or params
Expand All @@ -11,7 +13,7 @@ import {TargetState} from "../state/targetState";
*
* See [[StateDeclaration.redirectTo]]
*/
export const redirectToHook = (trans: Transition) => {
const redirectToHook: TransitionHookFn = (trans: Transition) => {
let redirect = trans.to().redirectTo;
if (!redirect) return;

Expand All @@ -28,4 +30,7 @@ export const redirectToHook = (trans: Transition) => {
return services.$q.when(redirect(trans)).then(handleResult);
}
return handleResult(redirect);
};
};

export const registerRedirectToHook = (transitionService: TransitionService) =>
transitionService.onStart({to: (state) => !!state.redirectTo}, redirectToHook);
12 changes: 10 additions & 2 deletions src/hooks/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {Transition} from "../transition/transition";
import {State} from "../state/stateObject";
import {ResolveContext} from "../resolve/resolveContext";
import {TransitionStateHookFn, TransitionHookFn} from "../transition/interface";
import {TransitionService} from "../transition/transitionService";
import {val} from "../common/hof";

/**
* A [[TransitionHookFn]] which resolves all EAGER Resolvables in the To Path
Expand All @@ -14,11 +16,14 @@ import {TransitionStateHookFn, TransitionHookFn} from "../transition/interface";
*
* See [[StateDeclaration.resolve]]
*/
export const eagerResolvePath: TransitionHookFn = (trans: Transition) =>
const eagerResolvePath: TransitionHookFn = (trans: Transition) =>
new ResolveContext(trans.treeChanges().to)
.resolvePath("EAGER", trans)
.then(noop);

export const registerEagerResolvePath = (transitionService: TransitionService) =>
transitionService.onStart({}, eagerResolvePath, {priority: 1000});

/**
* A [[TransitionHookFn]] which resolves all LAZY Resolvables for the state (and all its ancestors) in the To Path
*
Expand All @@ -28,9 +33,12 @@ export const eagerResolvePath: TransitionHookFn = (trans: Transition) =>
*
* See [[StateDeclaration.resolve]]
*/
export const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: State) =>
const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: State) =>
new ResolveContext(trans.treeChanges().to)
.subContext(state)
.resolvePath("LAZY", trans)
.then(noop);

export const registerLazyResolveState = (transitionService: TransitionService) =>
transitionService.onEnter({ entering: val(true) }, lazyResolveState, {priority: 1000});

8 changes: 6 additions & 2 deletions src/hooks/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import {UrlRouter} from "../url/urlRouter";
import {StateService} from "../state/stateService";
import {Transition} from "../transition/transition";
import {TransitionHookFn} from "../transition/interface";

/**
* A [[TransitionHookFn]] which updates the URL after a successful transition
*
* Registered using `transitionService.onSuccess({}, updateUrl);`
*/
export function updateUrl(transition: Transition) {
const updateUrl: TransitionHookFn = (transition: Transition) => {
let options = transition.options();
let $state: StateService = transition.router.stateService;
let $urlRouter: UrlRouter = transition.router.urlRouter;
Expand All @@ -19,4 +20,7 @@ export function updateUrl(transition: Transition) {
}

$urlRouter.update(true);
}
};

export const registerUpdateUrl = (transitionService: TransitionService) =>
transitionService.onSuccess({}, updateUrl, {priority: 9999});
16 changes: 12 additions & 4 deletions src/hooks/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {services} from "../common/coreservices";
import {Transition} from "../transition/transition";
import {ViewService} from "../view/view";
import {ViewConfig} from "../view/interface";
import {TransitionHookFn} from "../transition/interface";
import {TransitionService} from "../transition/transitionService";


/**
Expand All @@ -14,11 +16,14 @@ import {ViewConfig} from "../view/interface";
* Allows the views to do async work in [[ViewConfig.load]] before the transition continues.
* In angular 1, this includes loading the templates.
*/
export function loadEnteringViews(transition: Transition) {
const loadEnteringViews: TransitionHookFn = (transition: Transition) => {
let enteringViews = transition.views("entering");
if (!enteringViews.length) return;
return services.$q.all(enteringViews.map(view => view.load())).then(noop);
}
};

export const registerLoadEnteringViews = (transitionService: TransitionService) =>
transitionService.onStart({}, loadEnteringViews);

/**
* A [[TransitionHookFn]] which activates the new views when a transition is successful.
Expand All @@ -30,7 +35,7 @@ export function loadEnteringViews(transition: Transition) {
*
* See [[ViewService]]
*/
export function activateViews(transition: Transition) {
const activateViews: TransitionHookFn = (transition: Transition) => {
let enteringViews = transition.views("entering");
let exitingViews = transition.views("exiting");
if (!enteringViews.length && !exitingViews.length) return;
Expand All @@ -41,4 +46,7 @@ export function activateViews(transition: Transition) {
enteringViews.forEach((vc: ViewConfig) => $view.activateViewConfig(vc));

$view.sync();
}
};

export const registerActivateViews = (transitionService: TransitionService) =>
transitionService.onSuccess({}, activateViews);
30 changes: 15 additions & 15 deletions src/transition/transitionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {TargetState} from "../state/targetState";
import {PathNode} from "../path/node";
import {IEventHook} from "./interface";
import {ViewService} from "../view/view";
import {eagerResolvePath, lazyResolveState} from "../hooks/resolve";
import {loadEnteringViews, activateViews} from "../hooks/views";
import {updateUrl} from "../hooks/url";
import {redirectToHook} from "../hooks/redirectTo";
import {onExitHook, onRetainHook, onEnterHook} from "../hooks/onEnterExitRetain";
import {UIRouter} from "../router";
import {val} from "../common/hof";

import {registerEagerResolvePath, registerLazyResolveState} from "../hooks/resolve";
import {registerLoadEnteringViews, registerActivateViews} from "../hooks/views";
import {registerUpdateUrl} from "../hooks/url";
import {registerRedirectToHook} from "../hooks/redirectTo";
import {registerOnExitHook, registerOnRetainHook, registerOnEnterHook} from "../hooks/onEnterExitRetain";

/**
* The default [[Transition]] options.
Expand Down Expand Up @@ -79,23 +79,23 @@ export class TransitionService implements IHookRegistry {
let fns = this._deregisterHookFns;

// Wire up redirectTo hook
fns.redirectTo = this.onStart({to: (state) => !!state.redirectTo}, redirectToHook);
fns.redirectTo = registerRedirectToHook(this);

// Wire up onExit/Retain/Enter state hooks
fns.onExit = this.onExit ({exiting: state => !!state.onExit}, onExitHook);
fns.onRetain = this.onRetain({retained: state => !!state.onRetain}, onRetainHook);
fns.onEnter = this.onEnter ({entering: state => !!state.onEnter}, onEnterHook);
fns.onExit = registerOnExitHook(this);
fns.onRetain = registerOnRetainHook(this);
fns.onEnter = registerOnEnterHook(this);

// Wire up Resolve hooks
fns.eagerResolve = this.onStart({}, eagerResolvePath, {priority: 1000});
fns.lazyResolve = this.onEnter({ entering: val(true) }, lazyResolveState, {priority: 1000});
fns.eagerResolve = registerEagerResolvePath(this);
fns.lazyResolve = registerLazyResolveState(this);

// Wire up the View management hooks
fns.loadViews = this.onStart({}, loadEnteringViews);
fns.activateViews = this.onSuccess({}, activateViews);
fns.loadViews = registerLoadEnteringViews(this);
fns.activateViews = registerActivateViews(this);

// After globals.current is updated at priority: 10000
fns.updateUrl = this.onSuccess({}, updateUrl, {priority: 9999});
fns.updateUrl = registerUpdateUrl(this);
}

/** @inheritdoc */
Expand Down

0 comments on commit 1f98cee

Please sign in to comment.