Skip to content

Commit df6ee24

Browse files
fix(onEnter): Fix typescript typing for onEnter/onRetain/onExit
Closes #3260
1 parent 4559c32 commit df6ee24

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

src/directives/viewDirective.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import {
1212
extend, unnestR, filter, tail, isDefined, isFunction, isString, trace, parse,
13-
ActiveUIView, TransitionService, ResolveContext, Transition, PathNode,
13+
ActiveUIView, TransitionService, ResolveContext, Transition, PathNode, StateDeclaration,
1414
Param, kebobString, HookRegOptions, ViewService, $QLike, Obj, TypedMap
1515
} from "ui-router-core";
1616
import {Ng1ViewConfig} from "../statebuilders/views";
@@ -429,7 +429,7 @@ function registerControllerCallbacks($transitions: TransitionService, controller
429429
const paramsUpdated = ($transition$: Transition) => {
430430
// Exit early if the $transition$ is the same as the view was created within.
431431
// Exit early if the $transition$ will exit the state the view is for.
432-
if ($transition$ === viewCreationTrans || $transition$.exiting().indexOf(viewState) !== -1) return;
432+
if ($transition$ === viewCreationTrans || $transition$.exiting().indexOf(viewState as StateDeclaration) !== -1) return;
433433

434434
let toParams = $transition$.params("to") as TypedMap<any>;
435435
let fromParams = $transition$.params<TypedMap<any>>("from") as TypedMap<any>;

src/interface.ts

+84-16
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,55 @@ import { StateDeclaration, _ViewDeclaration, IInjectable, Transition, HookResult
1010
*
1111
* State hooks are registered as onEnter/onRetain/onExit in state declarations.
1212
* State hooks can additionally be injected with $transition$ and $state$ for
13-
* the current [[Transition]] and [[State]] in the transition.
13+
* the current [[Transition]] and [[StateObject]] in the transition.
1414
*
1515
* Transition State Hooks are callback functions that hook into the lifecycle events of specific states during a transition.
1616
* As a transition runs, it may exit some states, retain (keep) states, and enter states.
1717
* As each lifecycle event occurs, the hooks which are registered for the event and that state are called (in priority order).
1818
*
19-
* @param injectables list of services to inject into the function
20-
*
21-
* @returns a [[HookResult]] which may alter the transition
22-
*
23-
* @see
19+
* #### See also:
2420
*
2521
* - [[IHookRegistry.onExit]]
2622
* - [[IHookRegistry.onRetain]]
2723
* - [[IHookRegistry.onEnter]]
24+
*
25+
* #### Example:
26+
* ```js
27+
* onEnter: function() { console.log('Entering'); }
28+
* ```
29+
*
30+
* Not minification-safe
31+
* ```js
32+
* onRetain: function($state$) { console.log('Retained ' + $state$.name); }
33+
* ```
34+
*
35+
* Annotated for minification-safety
36+
* ```js
37+
* onExit: [ '$transition$', '$state', function($transition$, $state) {
38+
* // always redirect to 'foo' state when being exited
39+
* if ($transition$.to().name !== 'foo') {
40+
* return $state.target('foo');
41+
* }
42+
* } ]
43+
* ```
44+
*
45+
* @returns an optional [[HookResult]] which may alter the transition
2846
*/
2947
export interface Ng1StateTransitionHook {
3048
(...injectables: any[]) : HookResult
3149
}
3250

51+
/**
52+
* @internalapi
53+
* an intermediate interface.
54+
*
55+
* Used to reset [[StateDeclaration]] typings to `any` so the [[Ng1StateDeclaration]] interface can then narrow them */
56+
export interface _Ng1StateDeclaration extends StateDeclaration {
57+
onExit?: any;
58+
onRetain?: any;
59+
onEnter?: any;
60+
}
61+
3362
/**
3463
* The StateDeclaration object is used to define a state or nested state.
3564
* It should be registered with the [[StateRegistry]].
@@ -95,7 +124,7 @@ export interface Ng1StateTransitionHook {
95124
* }
96125
* ```
97126
*/
98-
export interface Ng1StateDeclaration extends StateDeclaration, Ng1ViewDeclaration {
127+
export interface Ng1StateDeclaration extends _Ng1StateDeclaration, Ng1ViewDeclaration {
99128
/**
100129
* An optional object which defines multiple named views.
101130
*
@@ -246,7 +275,10 @@ export interface Ng1StateDeclaration extends StateDeclaration, Ng1ViewDeclaratio
246275
views?: { [key: string]: Ng1ViewDeclaration; };
247276

248277
/**
249-
* State hook that can be injected with `$transition$` or `$state$` for the current transition.
278+
* A state hook invoked when a state is being entered.
279+
*
280+
* The hook can inject global services.
281+
* It can also inject `$transition$` or `$state$` (from the current transition).
250282
*
251283
* ### Example:
252284
* ```js
@@ -257,12 +289,25 @@ export interface Ng1StateDeclaration extends StateDeclaration, Ng1ViewDeclaratio
257289
* }
258290
* });
259291
* ```
292+
*
293+
* #### Example:`
294+
* ```js
295+
* $stateProvider.state({
296+
* name: 'mystate',
297+
* onEnter: [ 'MyService', '$transition$', '$state$', function (MyService, $transition$, $state$) {
298+
* return MyService.doSomething($state$.name, $transition$.params());
299+
* } ]
300+
* });
301+
* ```
260302
*/
261-
onEnter?: Ng1StateTransitionHook;
303+
onEnter?: Ng1StateTransitionHook | IInjectable;
262304

263305
/**
264-
* State hook that can be injected with `$transition$` or `$state$` for the current transition.
265-
*
306+
* A state hook invoked when a state is being exited.
307+
*
308+
* The hook can inject global services.
309+
* It can also inject `$transition$` or `$state$` (from the current transition).
310+
*
266311
* ### Example:
267312
* ```js
268313
* $stateProvider.state({
@@ -272,13 +317,26 @@ export interface Ng1StateDeclaration extends StateDeclaration, Ng1ViewDeclaratio
272317
* }
273318
* });
274319
* ```
320+
*
321+
* #### Example:`
322+
* ```js
323+
* $stateProvider.state({
324+
* name: 'mystate',
325+
* onExit: [ 'MyService', '$transition$', '$state$', function (MyService, $transition$, $state$) {
326+
* return MyService.doSomething($state$.name, $transition$.params());
327+
* } ]
328+
* });
329+
* ```
275330
*/
276-
onExit?: Ng1StateTransitionHook;
331+
onExit?: Ng1StateTransitionHook | IInjectable;
277332

278333
/**
279-
* State hook that can be injected with `$transition$` or `$state$` for the current transition.
280-
*
281-
* ### Example:
334+
* A state hook invoked when a state is being retained.
335+
*
336+
* The hook can inject global services.
337+
* It can also inject `$transition$` or `$state$` (from the current transition).
338+
*
339+
* #### Example:
282340
* ```js
283341
* $stateProvider.state({
284342
* name: 'mystate',
@@ -287,8 +345,18 @@ export interface Ng1StateDeclaration extends StateDeclaration, Ng1ViewDeclaratio
287345
* }
288346
* });
289347
* ```
348+
*
349+
* #### Example:`
350+
* ```js
351+
* $stateProvider.state({
352+
* name: 'mystate',
353+
* onRetain: [ 'MyService', '$transition$', '$state$', function (MyService, $transition$, $state$) {
354+
* return MyService.doSomething($state$.name, $transition$.params());
355+
* } ]
356+
* });
357+
* ```
290358
*/
291-
onRetain?: Ng1StateTransitionHook;
359+
onRetain?: Ng1StateTransitionHook | IInjectable;
292360

293361
/**
294362
* Makes all search/query parameters `dynamic`

src/statebuilders/onEnterExitRetain.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Ng1StateDeclaration } from '../interface';
99
* This is a [[StateBuilder.builder]] function for angular1 `onEnter`, `onExit`,
1010
* `onRetain` callback hooks on a [[Ng1StateDeclaration]].
1111
*
12-
* When the [[StateBuilder]] builds a [[State]] object from a raw [[StateDeclaration]], this builder
12+
* When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
1313
* ensures that those hooks are injectable for angular-ui-router (ng1).
1414
*/
1515
export const getStateHookBuilder = (hookName: "onEnter"|"onExit"|"onRetain") =>

src/statebuilders/views.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const hasAnyKey = (keys, obj) =>
2323
/**
2424
* This is a [[StateBuilder.builder]] function for angular1 `views`.
2525
*
26-
* When the [[StateBuilder]] builds a [[State]] object from a raw [[StateDeclaration]], this builder
26+
* When the [[StateBuilder]] builds a [[StateObject]] object from a raw [[StateDeclaration]], this builder
2727
* handles the `views` property with logic specific to angular-ui-router (ng1).
2828
*
2929
* If no `views: {}` property exists on the [[StateDeclaration]], then it creates the `views` object

0 commit comments

Comments
 (0)