Skip to content

Commit 2b0e48b

Browse files
fix(TransitionHook): Transition hooks no longer expose the internal StateObject
BREAKING CHANGE: Transition hooks no longer expose the internal `State` object (now named `StateObject`) - #### Before: ```js import { State } from "ui-router-core"; const match = { to: (state: State) => state.data.auth }; transitionsvc.onEnter(match, (trans: Transition, state: State) => { // state is the internal State object if (state.includes["foo"]) { // internal ui-router API return false; } } ``` - #### Now: ```js import { StateDeclaration } from "ui-router-core"; const match = { to: (state: StateDeclaration) => state.data.auth }; transitionsvc.onEnter(match, (trans: Transition, state: StateDeclaration) => { // state === the state object you registered // Access internal ui-router API using $$state() if (state.$$state().includes["foo"]) { return false; } } ``` - #### Motivation: The `State` object (now named `StateObject`) is an internal API and should not be exposed via any public APIs. If you depend on the internal APIs, you can still access the internal object by calling `state.$$state()`. - #### BC Likelihood How likely is this BC to affect me? Medium: You will likely be affected you 1) have transition hooks, 2) are using typescript and/or 3) use the internal ui-router State API. - #### BC Severity How severe is this BC? Low: Access to the internal api is still available using `$$state()`.
1 parent 3a5d055 commit 2b0e48b

File tree

7 files changed

+116
-115
lines changed

7 files changed

+116
-115
lines changed

src/hooks/onEnterExitRetain.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** @module hooks */ /** for typedoc */
22
import {TransitionStateHookFn} from "../transition/interface";
3-
import {StateObject} from "../state/stateObject";
43
import {Transition} from "../transition/transition";
54
import {TransitionService} from "../transition/transitionService";
5+
import { StateDeclaration } from '../state/interface';
66

77
/**
88
* A factory which creates an onEnter, onExit or onRetain transition hook function
@@ -13,7 +13,7 @@ import {TransitionService} from "../transition/transitionService";
1313
* @hidden
1414
*/
1515
function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn {
16-
return (transition: Transition, state: StateObject) => {
16+
return (transition: Transition, state: StateDeclaration) => {
1717
let hookFn: TransitionStateHookFn = state[hookName];
1818
return hookFn(transition, state);
1919
}

src/hooks/resolve.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/** @module hooks */ /** for typedoc */
2-
import {noop} from "../common/common";
3-
import {Transition} from "../transition/transition";
4-
import {StateObject} from "../state/stateObject";
5-
import {ResolveContext} from "../resolve/resolveContext";
6-
import {TransitionStateHookFn, TransitionHookFn} from "../transition/interface";
7-
import {TransitionService} from "../transition/transitionService";
8-
import {val} from "../common/hof";
1+
/** @module hooks */
2+
/** for typedoc */
3+
import { noop } from '../common/common';
4+
import { Transition } from '../transition/transition';
5+
import { ResolveContext } from '../resolve/resolveContext';
6+
import { TransitionStateHookFn, TransitionHookFn } from '../transition/interface';
7+
import { TransitionService } from '../transition/transitionService';
8+
import { val } from '../common/hof';
9+
import { StateDeclaration } from '../state/interface';
910

1011
/**
1112
* A [[TransitionHookFn]] which resolves all EAGER Resolvables in the To Path
@@ -33,9 +34,9 @@ export const registerEagerResolvePath = (transitionService: TransitionService) =
3334
*
3435
* See [[StateDeclaration.resolve]]
3536
*/
36-
const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: StateObject) =>
37+
const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: StateDeclaration) =>
3738
new ResolveContext(trans.treeChanges().to)
38-
.subContext(state)
39+
.subContext(state.$$state())
3940
.resolvePath("LAZY", trans)
4041
.then(noop);
4142

src/transition/hookBuilder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class HookBuilder {
7979
traceData: { hookType: hookType.name, context: node }
8080
}, baseHookOptions);
8181

82-
let state = hookType.criteriaMatchPath.scope === TransitionHookScope.STATE ? node.state : null;
82+
let state = hookType.criteriaMatchPath.scope === TransitionHookScope.STATE ? node.state.self : null;
8383
let transitionHook = new TransitionHook(transition, state, hook, _options);
8484
return <HookTuple> { hook, node, transitionHook };
8585
});

src/transition/interface.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export interface TransitionHookFn {
210210
* - [[IHookRegistry.onEnter]]
211211
*/
212212
export interface TransitionStateHookFn {
213-
(transition: Transition, state: StateObject) : HookResult
213+
(transition: Transition, state: StateDeclaration) : HookResult
214214
}
215215

216216
/**
@@ -700,8 +700,9 @@ export interface IHookRegistry {
700700
_registeredHooks: { [key: string]: RegisteredHook[] }
701701
}
702702

703-
/** A predicate type which takes a [[StateObject]] and returns a boolean */
704-
export type IStateMatch = Predicate<StateObject>
703+
/** A predicate type which tests if a [[StateDeclaration]] passes some test. Returns a boolean. */
704+
export type IStateMatch = Predicate<StateDeclaration>
705+
705706
/**
706707
* This object is used to configure whether or not a Transition Hook is invoked for a particular transition,
707708
* based on the Transition's "to state" and "from state".

src/transition/transitionHook.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { services } from '../common/coreservices';
1313
import { Rejection } from './rejectFactory';
1414
import { TargetState } from '../state/targetState';
1515
import { Transition } from './transition';
16-
import { StateObject } from '../state/stateObject';
1716
import { TransitionEventType } from './transitionEventType';
18-
import { RegisteredHook } from './hookRegistry'; // has or is using
17+
import { RegisteredHook } from './hookRegistry';
18+
import { StateDeclaration } from '../state/interface'; // has or is using
1919

2020
let defaultOptions: TransitionHookOptions = {
2121
current: noop,
@@ -34,7 +34,7 @@ export type ErrorHandler = (error) => Promise<any>;
3434
export class TransitionHook {
3535
type: TransitionEventType;
3636
constructor(private transition: Transition,
37-
private stateContext: StateObject,
37+
private stateContext: StateDeclaration,
3838
private registeredHook: RegisteredHook,
3939
private options: TransitionHookOptions) {
4040
this.options = defaults(options, defaultOptions);

0 commit comments

Comments
 (0)