3
3
* @module state
4
4
*/
5
5
/** for typedoc */
6
- import { StateDeclaration , _ViewDeclaration } from "./interface" ;
6
+ import { StateDeclaration , _ViewDeclaration , _StateDeclaration } from "./interface" ;
7
7
import { defaults , values , find , inherit } from "../common/common" ;
8
8
import { propEq } from "../common/hof" ;
9
9
import { Param } from "../params/param" ;
@@ -13,20 +13,20 @@ import { TransitionStateHookFn } from "../transition/interface";
13
13
import { TargetState } from "./targetState" ;
14
14
import { Transition } from "../transition/transition" ;
15
15
import { Glob } from "../common/glob" ;
16
- import { isObject } from "../common/predicates" ;
16
+ import { isObject , isFunction } from "../common/predicates" ;
17
17
18
18
/**
19
19
* Internal representation of a UI-Router state.
20
20
*
21
21
* Instances of this class are created when a [[StateDeclaration]] is registered with the [[StateRegistry]].
22
22
*
23
- * A registered [[StateDeclaration]] is augmented with a getter ([[StateDeclaration.$$state]]) which returns the corresponding [[State ]] object.
23
+ * A registered [[StateDeclaration]] is augmented with a getter ([[StateDeclaration.$$state]]) which returns the corresponding [[StateObject ]] object.
24
24
*
25
25
* This class prototypally inherits from the corresponding [[StateDeclaration]].
26
26
* Each of its own properties (i.e., `hasOwnProperty`) are built using builders from the [[StateBuilder]].
27
27
*/
28
28
export class StateObject {
29
- /** The parent [[State ]] */
29
+ /** The parent [[StateObject ]] */
30
30
public parent : StateObject ;
31
31
32
32
/** The name used to register the state */
@@ -58,15 +58,15 @@ export class StateObject {
58
58
public views : { [ key : string ] : _ViewDeclaration ; } ;
59
59
60
60
/**
61
- * The original [[StateDeclaration]] used to build this [[State ]].
61
+ * The original [[StateDeclaration]] used to build this [[StateObject ]].
62
62
* Note: `this` object also prototypally inherits from the `self` declaration object.
63
63
*/
64
64
public self : StateDeclaration ;
65
65
66
- /** The nearest parent [[State ]] which has a URL */
66
+ /** The nearest parent [[StateObject ]] which has a URL */
67
67
public navigable : StateObject ;
68
68
69
- /** The parent [[State ]] objects from this state up to the root */
69
+ /** The parent [[StateObject ]] objects from this state up to the root */
70
70
public path : StateObject [ ] ;
71
71
72
72
/**
@@ -117,7 +117,9 @@ export class StateObject {
117
117
* @param stateDecl the user-supplied State Declaration
118
118
* @returns {StateObject } an internal State object
119
119
*/
120
- static create ( stateDecl : StateDeclaration ) : StateObject {
120
+ static create ( stateDecl : _StateDeclaration ) : StateObject {
121
+ stateDecl = StateObject . isStateClass ( stateDecl ) ? new stateDecl ( ) : stateDecl ;
122
+
121
123
let state = inherit ( inherit ( stateDecl , StateObject . prototype ) ) as StateObject ;
122
124
stateDecl . $$state = ( ) => state ;
123
125
state . self = stateDecl ;
@@ -127,7 +129,11 @@ export class StateObject {
127
129
return state ;
128
130
}
129
131
130
- /** Predicate which returns true if the object is an internal [[State]] object */
132
+ /** Predicate which returns true if the object is an class with @State() decorator */
133
+ static isStateClass = ( stateDecl : _StateDeclaration ) : stateDecl is ( { new ( ) : StateDeclaration } ) =>
134
+ isFunction ( stateDecl ) && stateDecl [ '__uiRouterState' ] === true ;
135
+
136
+ /** Predicate which returns true if the object is an internal [[StateObject]] object */
131
137
static isState = ( obj : any ) : obj is StateObject =>
132
138
isObject ( obj [ '__stateObjectCache' ] ) ;
133
139
0 commit comments