Skip to content

Commit a854e89

Browse files
refactor(TransitionHook): remove context from TransitionHook
1 parent e102a85 commit a854e89

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

src/transition/hookBuilder.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ export class HookBuilder {
9898
return matchingNodes.map(node => {
9999
let _options = extend({ bind: hook.bind, traceData: { hookType, context: node} }, this.baseHookOptions, options);
100100
let state = _options.stateHook ? node.state : null;
101-
let context = resolveContext.subContext(node.state);
102-
let transitionHook = new TransitionHook(this.transition, state, hook.callback, context, _options);
101+
let transitionHook = new TransitionHook(this.transition, state, hook.callback, _options);
103102
return <HookTuple> { hook, node, transitionHook };
104103
});
105104
};

src/transition/transitionHook.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @module transition */ /** for typedoc */
2-
import {TransitionHookOptions, TransitionStateHookFn, HookFn, TransitionHookFn} from "./interface";
3-
import {defaults, noop, Predicate} from "../common/common";
2+
import {TransitionHookOptions, HookFn, HookResult} from "./interface";
3+
import {defaults, noop} from "../common/common";
44
import {fnToString, maxLength} from "../common/strings";
55
import {isDefined, isPromise } from "../common/predicates";
66
import {pattern, val, eq, is, parse } from "../common/hof";
@@ -27,15 +27,14 @@ export class TransitionHook {
2727
constructor(private transition: Transition,
2828
private stateContext: State,
2929
private hookFn: HookFn,
30-
private resolveContext: ResolveContext,
3130
private options: TransitionHookOptions) {
3231
this.options = defaults(options, defaultOptions);
3332
}
3433

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

37-
invokeHook(): Promise<any> {
38-
let { options, hookFn, resolveContext } = this;
36+
invokeHook(): Promise<HookResult> {
37+
let { options, hookFn } = this;
3938
trace.traceHookInvocation(this, options);
4039
if (options.rejectIfSuperseded && this.isSuperseded()) {
4140
return Rejection.superseded(options.current()).toPromise();

test/core/hookBuilderSpec.ts

+12-17
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,18 @@ describe('HookBuilder:', function() {
162162
Object.keys($trans._deregisterHookFns).forEach(key => $trans._deregisterHookFns[key]());
163163
});
164164

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

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

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

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

189-
it('; onRetain should be bound to the retained state(s)', function() {
190-
trans.onRetain({}, callback);
191-
expect(hb.getOnRetainHooks().map(context)).toEqual(["", "A"]);
192-
});
193-
194189
it('; onExit should be bound to the exiting state(s)', function() {
195190
trans2.onExit({}, callback);
196191
expect(hb2.getOnExitHooks().map(context)).toEqual(["A.B.C", "A.B"]);
197192
});
198193

199-
it('; onFinish should be bound to the to state', function() {
194+
it('; onFinish should not have a state context', function() {
200195
trans.onFinish({}, callback);
201-
expect(hb.getOnFinishHooks().map(context)).toEqual(["A.B.C"]);
196+
expect(hb.getOnFinishHooks().map(context)).toEqual([null]);
202197
});
203198

204-
it('; onSuccess should be bound to the to state', function() {
199+
it('; onSuccess should not have a state context', function() {
205200
trans.onSuccess({}, callback);
206-
expect(hb.getOnSuccessHooks().map(context)).toEqual(["A.B.C"]);
201+
expect(hb.getOnSuccessHooks().map(context)).toEqual([null]);
207202
});
208203

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

215210
});

0 commit comments

Comments
 (0)