Skip to content

Commit

Permalink
refactor(TransitionHook): remove context from TransitionHook
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Aug 2, 2016
1 parent e102a85 commit a854e89
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/transition/hookBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ export class HookBuilder {
return matchingNodes.map(node => {
let _options = extend({ bind: hook.bind, traceData: { hookType, context: node} }, this.baseHookOptions, options);
let state = _options.stateHook ? node.state : null;
let context = resolveContext.subContext(node.state);
let transitionHook = new TransitionHook(this.transition, state, hook.callback, context, _options);
let transitionHook = new TransitionHook(this.transition, state, hook.callback, _options);
return <HookTuple> { hook, node, transitionHook };
});
};
Expand Down
9 changes: 4 additions & 5 deletions src/transition/transitionHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @module transition */ /** for typedoc */
import {TransitionHookOptions, TransitionStateHookFn, HookFn, TransitionHookFn} from "./interface";
import {defaults, noop, Predicate} from "../common/common";
import {TransitionHookOptions, HookFn, HookResult} from "./interface";
import {defaults, noop} from "../common/common";
import {fnToString, maxLength} from "../common/strings";
import {isDefined, isPromise } from "../common/predicates";
import {pattern, val, eq, is, parse } from "../common/hof";
Expand All @@ -27,15 +27,14 @@ export class TransitionHook {
constructor(private transition: Transition,
private stateContext: State,
private hookFn: HookFn,
private resolveContext: ResolveContext,
private options: TransitionHookOptions) {
this.options = defaults(options, defaultOptions);
}

private isSuperseded = () => this.options.current() !== this.options.transition;

invokeHook(): Promise<any> {
let { options, hookFn, resolveContext } = this;
invokeHook(): Promise<HookResult> {
let { options, hookFn } = this;
trace.traceHookInvocation(this, options);
if (options.rejectIfSuperseded && this.isSuperseded()) {
return Rejection.superseded(options.current()).toPromise();
Expand Down
29 changes: 12 additions & 17 deletions test/core/hookBuilderSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ describe('HookBuilder:', function() {
Object.keys($trans._deregisterHookFns).forEach(key => $trans._deregisterHookFns[key]());
});

describe('should be bound to the correct context', function() {
describe('should have the correct state context', function() {
const context = hook =>
tail(hook.resolveContext['_path']).state.name;
hook.stateContext && hook.stateContext.name;

it('; onBefore should be bound to the to state', function() {
it('; onBefore should not have a state context', function() {
trans.onBefore({}, callback);
expect(hb.getOnBeforeHooks().map(context)).toEqual(["A.B.C"]);
expect(hb.getOnBeforeHooks().map(context)).toEqual([null]);
});

it('; onStart should be bound to the to state', function() {
it('; onStart should not have a state context', function() {
trans.onStart({}, callback);
expect(hb.getOnStartHooks().map(context)).toEqual(["A.B.C"]);
expect(hb.getOnStartHooks().map(context)).toEqual([null]);
});

it('; onEnter should be bound to the entering state(s)', function() {
Expand All @@ -186,30 +186,25 @@ describe('HookBuilder:', function() {
expect(hb.getOnRetainHooks().map(context)).toEqual(["", "A"]);
});

it('; onRetain should be bound to the retained state(s)', function() {
trans.onRetain({}, callback);
expect(hb.getOnRetainHooks().map(context)).toEqual(["", "A"]);
});

it('; onExit should be bound to the exiting state(s)', function() {
trans2.onExit({}, callback);
expect(hb2.getOnExitHooks().map(context)).toEqual(["A.B.C", "A.B"]);
});

it('; onFinish should be bound to the to state', function() {
it('; onFinish should not have a state context', function() {
trans.onFinish({}, callback);
expect(hb.getOnFinishHooks().map(context)).toEqual(["A.B.C"]);
expect(hb.getOnFinishHooks().map(context)).toEqual([null]);
});

it('; onSuccess should be bound to the to state', function() {
it('; onSuccess should not have a state context', function() {
trans.onSuccess({}, callback);
expect(hb.getOnSuccessHooks().map(context)).toEqual(["A.B.C"]);
expect(hb.getOnSuccessHooks().map(context)).toEqual([null]);
});

it('; onError should be bound to the to state', function() {
it('; onError should not have a state context', function() {
trans.onStart({}, () => { throw new Error('shuckydarn') });
trans.onError({}, callback);
expect(hb.getOnErrorHooks().map(context)).toEqual(["A.B.C"]);
expect(hb.getOnErrorHooks().map(context)).toEqual([null]);
});

});
Expand Down

0 comments on commit a854e89

Please sign in to comment.