From feceaf951f66f6f372c14b08d01e46dd88d4ab1b Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Thu, 16 Feb 2017 19:43:20 -0600 Subject: [PATCH] feat(StateObject): Rename internal `State` object to `StateObject` BREAKING CHANGE: Renamed internal API `State` object to `StateObject` - #### Before: ``` import {State} from "ui-router-core"; ``` - #### Now: ``` import {StateObject} from "ui-router-core"; ``` - #### Motivation: We'd like to use the `State` name/symbol as a public API. It will be an ES7/TS decorator for ES6/TS state definition classes, i.e: ```js @State("foo") export class FooState implements StateDeclaration { url = "/foo"; component = FooComponent; @Resolve({ deps: [FooService] }) fooData(fooService) { return fooService.getFoos(); } } ``` - #### BC Likelihood How likely is this to affect me? Low: This only affects code that imports the internal API symbol `State`. You will likely be affected you 1) import that symbol, 2) are using typescript and 3) explicitly typed a variable such as `let internalStateObject = state.$$state();` - #### BC Severity How severe is this change? Low: Find all places where `State` is imported and rename to `StateObject` --- src/common/common.ts | 6 +++--- src/common/predicates.ts | 4 ++-- src/common/trace.ts | 4 ++-- src/globals.ts | 4 ++-- src/hooks/onEnterExitRetain.ts | 4 ++-- src/hooks/resolve.ts | 4 ++-- src/params/stateParams.ts | 4 ++-- src/path/node.ts | 8 +++---- src/path/pathFactory.ts | 10 ++++----- src/resolve/resolvable.ts | 6 +++--- src/resolve/resolveContext.ts | 6 +++--- src/state/interface.ts | 8 +++---- src/state/stateBuilder.ts | 36 ++++++++++++++++---------------- src/state/stateMatcher.ts | 8 +++---- src/state/stateObject.ts | 22 +++++++++---------- src/state/stateQueueManager.ts | 18 ++++++++-------- src/state/stateRegistry.ts | 16 +++++++------- src/state/stateService.ts | 4 ++-- src/state/targetState.ts | 6 +++--- src/transition/hookBuilder.ts | 2 +- src/transition/hookRegistry.ts | 6 +++--- src/transition/interface.ts | 12 +++++------ src/transition/transition.ts | 10 ++++----- src/transition/transitionHook.ts | 4 ++-- src/url/interface.ts | 4 ++-- src/url/urlRule.ts | 8 +++---- test/hookBuilderSpec.ts | 4 ++-- test/resolveSpec.ts | 4 ++-- test/stateBuilderSpec.ts | 12 +++++------ test/stateServiceSpec.ts | 4 ++-- 30 files changed, 124 insertions(+), 124 deletions(-) diff --git a/src/common/common.ts b/src/common/common.ts index 54d015d8..a6e07e17 100644 --- a/src/common/common.ts +++ b/src/common/common.ts @@ -10,7 +10,7 @@ import { isFunction, isString, isArray, isRegExp, isDate, isDefined } from "./predicates"; import { all, any, prop, curry, val, not } from "./hof"; import { services } from "./coreservices"; -import { State } from "../state/stateObject"; +import { StateObject } from "../state/stateObject"; let w: any = typeof window === 'undefined' ? {} : window; let angular = w.angular || {}; @@ -222,8 +222,8 @@ export const mergeR = (memo: Obj, item: Obj) => extend(memo, item); * @param {Object} second The second state. * @return {Array} Returns an array of state names in descending order, not including the root. */ -export function ancestors(first: State, second: State) { - let path: State[] = []; +export function ancestors(first: StateObject, second: StateObject) { + let path: StateObject[] = []; for (var n in first.path) { if (first.path[n] !== second.path[n]) break; diff --git a/src/common/predicates.ts b/src/common/predicates.ts index d7144fe7..c3d8c836 100644 --- a/src/common/predicates.ts +++ b/src/common/predicates.ts @@ -8,7 +8,7 @@ /** */ import { and, not, pipe, prop, or } from "./hof"; import { Predicate } from "./common"; // has or is using -import { State } from "../state/stateObject"; +import { StateObject } from "../state/stateObject"; const toStr = Object.prototype.toString; const tis = (t: string) => (x: any) => typeof(x) === t; @@ -23,7 +23,7 @@ export const isObject = (x: any) => x !== null && typeof x === 'object'; export const isArray = Array.isArray; export const isDate: (x: any) => x is Date = ((x: any) => toStr.call(x) === '[object Date]'); export const isRegExp: (x: any) => x is RegExp = ((x: any) => toStr.call(x) === '[object RegExp]'); -export const isState: (x: any) => x is State = State.isState; +export const isState: (x: any) => x is StateObject = StateObject.isState; /** * Predicate which checks if a value is injectable diff --git a/src/common/trace.ts b/src/common/trace.ts index 35d965f2..7a25daf3 100644 --- a/src/common/trace.ts +++ b/src/common/trace.ts @@ -43,7 +43,7 @@ import {PathNode} from "../path/node"; import {PolicyWhen} from "../resolve/interface"; import {TransitionHook} from "../transition/transitionHook"; import {HookResult} from "../transition/interface"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; /** @hidden */ function uiViewString (viewData: ActiveUIView) { @@ -193,7 +193,7 @@ export class Trace { } /** @internalapi called by ui-router code */ - traceSuccess(finalState: State, trans: Transition) { + traceSuccess(finalState: StateObject, trans: Transition) { if (!this.enabled(Category.TRANSITION)) return; console.log(`${transLbl(trans)}: <- Success ${stringify(trans)}, final state: ${finalState.name}`); } diff --git a/src/globals.ts b/src/globals.ts index 0b2690bf..dc28c5d3 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -4,7 +4,7 @@ */ /** */ import {StateParams} from "./params/stateParams"; import {StateDeclaration} from "./state/interface"; -import {State} from "./state/stateObject"; +import {StateObject} from "./state/stateObject"; import {Transition} from "./transition/transition"; import {Queue} from "./common/queue"; import {TransitionService} from "./transition/transitionService"; @@ -38,7 +38,7 @@ export class UIRouterGlobals implements Disposable { * The to-state from the latest successful transition * @internalapi */ - $current: State; + $current: StateObject; /** * The current transition (in progress) diff --git a/src/hooks/onEnterExitRetain.ts b/src/hooks/onEnterExitRetain.ts index e6e6472d..5e7fa80b 100644 --- a/src/hooks/onEnterExitRetain.ts +++ b/src/hooks/onEnterExitRetain.ts @@ -1,6 +1,6 @@ /** @module hooks */ /** for typedoc */ import {TransitionStateHookFn} from "../transition/interface"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {Transition} from "../transition/transition"; import {TransitionService} from "../transition/transitionService"; @@ -13,7 +13,7 @@ import {TransitionService} from "../transition/transitionService"; * @hidden */ function makeEnterExitRetainHook(hookName: string): TransitionStateHookFn { - return (transition: Transition, state: State) => { + return (transition: Transition, state: StateObject) => { let hookFn: TransitionStateHookFn = state[hookName]; return hookFn(transition, state); } diff --git a/src/hooks/resolve.ts b/src/hooks/resolve.ts index e737b43f..08cc9cdb 100644 --- a/src/hooks/resolve.ts +++ b/src/hooks/resolve.ts @@ -1,7 +1,7 @@ /** @module hooks */ /** for typedoc */ import {noop} from "../common/common"; import {Transition} from "../transition/transition"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {ResolveContext} from "../resolve/resolveContext"; import {TransitionStateHookFn, TransitionHookFn} from "../transition/interface"; import {TransitionService} from "../transition/transitionService"; @@ -33,7 +33,7 @@ export const registerEagerResolvePath = (transitionService: TransitionService) = * * See [[StateDeclaration.resolve]] */ -const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: State) => +const lazyResolveState: TransitionStateHookFn = (trans: Transition, state: StateObject) => new ResolveContext(trans.treeChanges().to) .subContext(state) .resolvePath("LAZY", trans) diff --git a/src/params/stateParams.ts b/src/params/stateParams.ts index 5b7ba024..045b2aa3 100644 --- a/src/params/stateParams.ts +++ b/src/params/stateParams.ts @@ -4,7 +4,7 @@ */ /** */ import {extend, ancestors, Obj} from "../common/common"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; /** @internalapi */ export class StateParams { @@ -22,7 +22,7 @@ export class StateParams { * @param {Object} $current Internal definition of object representing the current state. * @param {Object} $to Internal definition of object representing state to transition to. */ - $inherit(newParams: Obj, $current: State, $to: State) { + $inherit(newParams: Obj, $current: StateObject, $to: StateObject) { let parents = ancestors($current, $to), parentParams: string[], inherited: Obj = {}, diff --git a/src/path/node.ts b/src/path/node.ts index fc2251d0..7b9af75a 100644 --- a/src/path/node.ts +++ b/src/path/node.ts @@ -1,7 +1,7 @@ /** @module path */ /** for typedoc */ import {extend, applyPairs, find, allTrueR} from "../common/common"; import {propEq} from "../common/hof"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {RawParams} from "../params/interface"; import {Param} from "../params/param"; import {Resolvable} from "../resolve/resolvable"; @@ -16,7 +16,7 @@ import {ViewConfig} from "../view/interface"; */ export class PathNode { /** The state being entered, exited, or retained */ - public state: State; + public state: StateObject; /** The parameters declared on the state */ public paramSchema: Param[]; /** The parameter values that belong to the state */ @@ -29,7 +29,7 @@ export class PathNode { /** Creates a copy of a PathNode */ constructor(state: PathNode); /** Creates a new (empty) PathNode for a State */ - constructor(state: State); + constructor(state: StateObject); constructor(stateOrPath: any) { if (stateOrPath instanceof PathNode) { let node: PathNode = stateOrPath; @@ -39,7 +39,7 @@ export class PathNode { this.resolvables = node.resolvables.slice(); this.views = node.views && node.views.slice(); } else { - let state: State = stateOrPath; + let state: StateObject = stateOrPath; this.state = state; this.paramSchema = state.parameters({ inherit: false }); this.paramValues = {}; diff --git a/src/path/pathFactory.ts b/src/path/pathFactory.ts index 1001c66f..5cc5f944 100644 --- a/src/path/pathFactory.ts +++ b/src/path/pathFactory.ts @@ -8,7 +8,7 @@ import {TreeChanges} from "../transition/interface"; import {ViewConfig} from "../view/interface"; import {_ViewDeclaration} from "../state/interface"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {TargetState} from "../state/targetState"; import {PathNode} from "../path/node"; import {ViewService} from "../view/view"; @@ -45,7 +45,7 @@ export class PathFactory { * * On each [[PathNode]], creates ViewConfig objects from the views: property of the node's state */ - static applyViewConfigs($view: ViewService, path: PathNode[], states: State[]) { + static applyViewConfigs($view: ViewService, path: PathNode[], states: StateObject[]) { // Only apply the viewConfigs to the nodes for the given states path.filter(node => inArray(states, node.state)).forEach(node => { let viewDecls: _ViewDeclaration[] = values(node.state.views || {}); @@ -67,7 +67,7 @@ export class PathFactory { * it is not inherited from the fromPath. */ static inheritParams(fromPath: PathNode[], toPath: PathNode[], toKeys: string[] = []): PathNode[] { - function nodeParamVals(path: PathNode[], state: State): RawParams { + function nodeParamVals(path: PathNode[], state: StateObject): RawParams { let node: PathNode = find(path, propEq('state', state)); return extend({}, node && node.paramValues); } @@ -100,9 +100,9 @@ export class PathFactory { /** * Computes the tree changes (entering, exiting) between a fromPath and toPath. */ - static treeChanges(fromPath: PathNode[], toPath: PathNode[], reloadState: State): TreeChanges { + static treeChanges(fromPath: PathNode[], toPath: PathNode[], reloadState: StateObject): TreeChanges { let keep = 0, max = Math.min(fromPath.length, toPath.length); - const staticParams = (state: State) => + const staticParams = (state: StateObject) => state.parameters({ inherit: false }).filter(not(prop('dynamic'))).map(prop('id')); const nodesMatch = (node1: PathNode, node2: PathNode) => node1.equals(node2, staticParams(node1.state)); diff --git a/src/resolve/resolvable.ts b/src/resolve/resolvable.ts index 985193ee..e3216f3c 100644 --- a/src/resolve/resolvable.ts +++ b/src/resolve/resolvable.ts @@ -11,7 +11,7 @@ import {ResolveContext} from "./resolveContext"; import {stringify} from "../common/strings"; import {isFunction, isObject} from "../common/predicates"; import {Transition} from "../transition/transition"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {PathNode} from "../path/node"; @@ -90,7 +90,7 @@ export class Resolvable implements ResolvableLiteral { } } - getPolicy(state:State): ResolvePolicy { + getPolicy(state:StateObject): ResolvePolicy { let thisPolicy = this.policy || {}; let statePolicy = state && state.resolvePolicy || {}; return { @@ -133,7 +133,7 @@ export class Resolvable implements ResolvableLiteral { // If the resolve policy is RXWAIT, wait for the observable to emit something. otherwise pass through. let node: PathNode = resolveContext.findNode(this); - let state: State = node && node.state; + let state: StateObject = node && node.state; let maybeWaitForRx = this.getPolicy(state).async === "RXWAIT" ? waitForRx : identity; // After the final value has been resolved, update the state of the Resolvable diff --git a/src/resolve/resolveContext.ts b/src/resolve/resolveContext.ts index 3529c599..7e90edd7 100644 --- a/src/resolve/resolveContext.ts +++ b/src/resolve/resolveContext.ts @@ -7,7 +7,7 @@ import { services, $InjectorLike } from "../common/coreservices"; import { resolvePolicies, PolicyWhen, ResolvePolicy } from "./interface"; import { PathNode } from "../path/node"; import { Resolvable } from "./resolvable"; -import { State } from "../state/stateObject"; +import { StateObject } from "../state/stateObject"; import { PathFactory } from "../path/pathFactory"; import { stringify } from "../common/strings"; import { Transition } from "../transition/transition"; @@ -81,7 +81,7 @@ export class ResolveContext { * When resolving for the `B` node, first take the full "To Path" Context `[A,B,C,D]` and limit to the subpath `[A,B]`. * `let AB = ABCD.subcontext(a)` */ - subContext(state: State): ResolveContext { + subContext(state: StateObject): ResolveContext { return new ResolveContext(PathFactory.subPath(this._path, node => node.state === state)); } @@ -100,7 +100,7 @@ export class ResolveContext { * @param newResolvables the new Resolvables * @param state Used to find the node to put the resolvable on */ - addResolvables(newResolvables: Resolvable[], state: State) { + addResolvables(newResolvables: Resolvable[], state: StateObject) { let node = find(this._path, propEq('state', state)); let keys = newResolvables.map(r => r.token); node.resolvables = node.resolvables.filter(r => keys.indexOf(r.token) === -1).concat(newResolvables); diff --git a/src/state/interface.ts b/src/state/interface.ts index 4a3f7261..34c1e56b 100644 --- a/src/state/interface.ts +++ b/src/state/interface.ts @@ -3,7 +3,7 @@ * @module state */ /** for typedoc */ import { ParamDeclaration, RawParams, ParamsOrArray } from "../params/interface"; -import { State } from "./stateObject"; +import { StateObject } from "./stateObject"; import { ViewContext } from "../view/interface"; import { IInjectable } from "../common/common"; import { Transition } from "../transition/transition"; @@ -12,10 +12,10 @@ import { ResolvePolicy, ResolvableLiteral, ProviderLike } from "../resolve/inter import { Resolvable } from "../resolve/resolvable"; import { TargetState } from "./targetState"; -export type StateOrName = (string|StateDeclaration|State); +export type StateOrName = (string|StateDeclaration|StateObject); /** @internalapi */ -export interface TransitionPromise extends Promise { +export interface TransitionPromise extends Promise { transition: Transition; } @@ -163,7 +163,7 @@ export interface StateDeclaration { * * Note: the internal [[State]] API is subject to change without notice */ - $$state?: () => State; + $$state?: () => StateObject; /** * Resolve - a mechanism to asynchronously fetch data, participating in the Transition lifecycle diff --git a/src/state/stateBuilder.ts b/src/state/stateBuilder.ts index 011a3414..4bf94c6e 100644 --- a/src/state/stateBuilder.ts +++ b/src/state/stateBuilder.ts @@ -5,7 +5,7 @@ import {stringify} from "../common/strings"; import {prop, pattern, is, pipe, val} from "../common/hof"; import {StateDeclaration} from "./interface"; -import {State} from "./stateObject"; +import {StateObject} from "./stateObject"; import {StateMatcher} from "./stateMatcher"; import {Param} from "../params/param"; import {UrlMatcherFactory} from "../url/urlMatcherFactory"; @@ -21,7 +21,7 @@ const parseUrl = (url: string): any => { return { val: root ? url.substring(1) : url, root }; }; -export type BuilderFunction = (state: State, parent?: BuilderFunction) => any; +export type BuilderFunction = (state: StateObject, parent?: BuilderFunction) => any; interface Builders { [key: string]: BuilderFunction[]; @@ -39,24 +39,24 @@ interface Builders { } -function nameBuilder(state: State) { +function nameBuilder(state: StateObject) { return state.name; } -function selfBuilder(state: State) { +function selfBuilder(state: StateObject) { state.self.$$state = () => state; return state.self; } -function dataBuilder(state: State) { +function dataBuilder(state: StateObject) { if (state.parent && state.parent.data) { state.data = state.self.data = inherit(state.parent.data, state.data); } return state.data; } -const getUrlBuilder = ($urlMatcherFactoryProvider: UrlMatcherFactory, root: () => State) => -function urlBuilder(state: State) { +const getUrlBuilder = ($urlMatcherFactoryProvider: UrlMatcherFactory, root: () => StateObject) => +function urlBuilder(state: StateObject) { let stateDec: StateDeclaration = state; // For future states, i.e., states whose name ends with `.**`, @@ -79,24 +79,24 @@ function urlBuilder(state: State) { return (parsed && parsed.root) ? url : ((parent && parent.navigable) || root()).url.append( url); }; -const getNavigableBuilder = (isRoot: (state: State) => boolean) => -function navigableBuilder(state: State) { +const getNavigableBuilder = (isRoot: (state: StateObject) => boolean) => +function navigableBuilder(state: StateObject) { return !isRoot(state) && state.url ? state : (state.parent ? state.parent.navigable : null); }; const getParamsBuilder = (paramFactory: ParamFactory) => -function paramsBuilder(state: State): { [key: string]: Param } { +function paramsBuilder(state: StateObject): { [key: string]: Param } { const makeConfigParam = (config: any, id: string) => paramFactory.fromConfig(id, null, config); let urlParams: Param[] = (state.url && state.url.parameters({inherit: false})) || []; let nonUrlParams: Param[] = values(mapObj(omit(state.params || {}, urlParams.map(prop('id'))), makeConfigParam)); return urlParams.concat(nonUrlParams).map(p => [p.id, p]).reduce(applyPairs, {}); }; -function pathBuilder(state: State) { +function pathBuilder(state: StateObject) { return state.parent ? state.parent.path.concat(state) : /*root*/ [state]; } -function includesBuilder(state: State) { +function includesBuilder(state: StateObject) { let includes = state.parent ? extend({}, state.parent.includes) : {}; includes[state.name] = true; return includes; @@ -143,7 +143,7 @@ function includesBuilder(state: State) { * { provide: "myBazResolve", useFactory: function(dep) { dep.fetchSomethingAsPromise() }, deps: [ "DependencyName" ] } * ] */ -export function resolvablesBuilder(state: State): Resolvable[] { +export function resolvablesBuilder(state: StateObject): Resolvable[] { interface Tuple { token: any, val: any, deps: any[], policy: ResolvePolicy } /** convert resolve: {} and resolvePolicy: {} objects to an array of tuples */ @@ -221,9 +221,9 @@ export class StateBuilder { let self = this; const root = () => matcher.find(""); - const isRoot = (state: State) => state.name === ""; + const isRoot = (state: StateObject) => state.name === ""; - function parentBuilder(state: State) { + function parentBuilder(state: StateObject) { if (isRoot(state)) return null; return matcher.find(self.parentName(state)) || root(); } @@ -278,7 +278,7 @@ export class StateBuilder { * @param state an uninitialized State object * @returns the built State object */ - build(state: State): State { + build(state: StateObject): StateObject { let {matcher, builders} = this; let parent = this.parentName(state); if (parent && !matcher.find(parent)) return null; @@ -291,7 +291,7 @@ export class StateBuilder { return state; } - parentName(state: State) { + parentName(state: StateObject) { let name = state.name || ""; let segments = name.split('.'); @@ -308,7 +308,7 @@ export class StateBuilder { return isString(state.parent) ? state.parent : state.parent.name; } - name(state: State) { + name(state: StateObject) { let name = state.name; if (name.indexOf('.') !== -1 || !state.parent) return name; diff --git a/src/state/stateMatcher.ts b/src/state/stateMatcher.ts index 143c95f2..55495f86 100644 --- a/src/state/stateMatcher.ts +++ b/src/state/stateMatcher.ts @@ -1,11 +1,11 @@ /** @module state */ /** for typedoc */ import {isString} from "../common/predicates"; import {StateOrName} from "./interface"; -import {State} from "./stateObject"; +import {StateObject} from "./stateObject"; import {values} from "../common/common"; export class StateMatcher { - constructor (private _states: { [key: string]: State }) { } + constructor (private _states: { [key: string]: StateObject }) { } isRelative(stateName: string) { stateName = stateName || ""; @@ -13,7 +13,7 @@ export class StateMatcher { } - find(stateOrName: StateOrName, base?: StateOrName): State { + find(stateOrName: StateOrName, base?: StateOrName): StateObject { if (!stateOrName && stateOrName !== "") return undefined; let isStr = isString(stateOrName); let name: string = isStr ? stateOrName : (stateOrName).name; @@ -41,7 +41,7 @@ export class StateMatcher { resolvePath(name: string, base: StateOrName) { if (!base) throw new Error(`No reference point given for path '${name}'`); - let baseState: State = this.find(base); + let baseState: StateObject = this.find(base); let splitName = name.split("."), i = 0, pathLength = splitName.length, current = baseState; diff --git a/src/state/stateObject.ts b/src/state/stateObject.ts index d8cc0041..4e1f568c 100644 --- a/src/state/stateObject.ts +++ b/src/state/stateObject.ts @@ -25,9 +25,9 @@ import { isObject } from "../common/predicates"; * This class prototypally inherits from the corresponding [[StateDeclaration]]. * Each of its own properties (i.e., `hasOwnProperty`) are built using builders from the [[StateBuilder]]. */ -export class State { +export class StateObject { /** The parent [[State]] */ - public parent: State; + public parent: StateObject; /** The name used to register the state */ public name: string; @@ -64,10 +64,10 @@ export class State { public self: StateDeclaration; /** The nearest parent [[State]] which has a URL */ - public navigable: State; + public navigable: StateObject; /** The parent [[State]] objects from this state up to the root */ - public path: State[]; + public path: StateObject[]; /** * Prototypally inherits from [[StateDeclaration.data]] @@ -106,7 +106,7 @@ export class State { /** @deprecated use State.create() */ constructor(config?: StateDeclaration) { - return State.create(config || {}); + return StateObject.create(config || {}); } /** @@ -115,10 +115,10 @@ export class State { * (Internal State Object) -> (Copy of State.prototype) -> (State Declaration object) -> (State Declaration's prototype...) * * @param stateDecl the user-supplied State Declaration - * @returns {State} an internal State object + * @returns {StateObject} an internal State object */ - static create(stateDecl: StateDeclaration): State { - let state = inherit(inherit(stateDecl, State.prototype)) as State; + static create(stateDecl: StateDeclaration): StateObject { + let state = inherit(inherit(stateDecl, StateObject.prototype)) as StateObject; stateDecl.$$state = () => state; state.self = stateDecl; state.__stateObjectCache = { @@ -128,7 +128,7 @@ export class State { } /** Predicate which returns true if the object is an internal [[State]] object */ - static isState = (obj: any): obj is State => + static isState = (obj: any): obj is StateObject => isObject(obj['__stateObjectCache']); /** @@ -142,7 +142,7 @@ export class State { * into `$stateProvider.state()`, (c) the fully-qualified name of a state as a string. * @returns Returns `true` if `ref` matches the current `State` instance. */ - is(ref: State|StateDeclaration|string): boolean { + is(ref: StateObject|StateDeclaration|string): boolean { return this === ref || this.self === ref || this.fqn() === ref; } @@ -161,7 +161,7 @@ export class State { * * @returns The root of this state's tree. */ - root(): State { + root(): StateObject { return this.parent && this.parent.root() || this; } diff --git a/src/state/stateQueueManager.ts b/src/state/stateQueueManager.ts index a868a276..789e9a3f 100644 --- a/src/state/stateQueueManager.ts +++ b/src/state/stateQueueManager.ts @@ -2,7 +2,7 @@ import { inArray } from "../common/common"; import { isString } from "../common/predicates"; import { StateDeclaration } from "./interface"; -import { State } from "./stateObject"; +import { StateObject } from "./stateObject"; import { StateBuilder } from "./stateBuilder"; import { StateRegistryListener, StateRegistry } from "./stateRegistry"; import { Disposable } from "../interface"; @@ -12,13 +12,13 @@ import { StateMatcher } from "./stateMatcher"; /** @internalapi */ export class StateQueueManager implements Disposable { - queue: State[]; + queue: StateObject[]; matcher: StateMatcher; constructor( private $registry: StateRegistry, private $urlRouter: UrlRouter, - public states: { [key: string]: State; }, + public states: { [key: string]: StateObject; }, public builder: StateBuilder, public listeners: StateRegistryListener[]) { this.queue = []; @@ -32,7 +32,7 @@ export class StateQueueManager implements Disposable { register(stateDecl: StateDeclaration) { let queue = this.queue; - let state = State.create(stateDecl); + let state = StateObject.create(stateDecl); let name = state.name; if (!isString(name)) throw new Error("State must have a valid name"); @@ -47,16 +47,16 @@ export class StateQueueManager implements Disposable { flush() { let {queue, states, builder} = this; - let registered: State[] = [], // states that got registered - orphans: State[] = [], // states that don't yet have a parent registered + let registered: StateObject[] = [], // states that got registered + orphans: StateObject[] = [], // states that don't yet have a parent registered previousQueueLength = {}; // keep track of how long the queue when an orphan was first encountered const getState = (name) => this.states.hasOwnProperty(name) && this.states[name]; while (queue.length > 0) { - let state: State = queue.shift(); + let state: StateObject = queue.shift(); let name = state.name; - let result: State = builder.build(state); + let result: StateObject = builder.build(state); let orphanIdx: number = orphans.indexOf(state); if (result) { @@ -99,7 +99,7 @@ export class StateQueueManager implements Disposable { return states; } - attachRoute(state: State) { + attachRoute(state: StateObject) { if (state.abstract || !state.url) return; this.$urlRouter.rule(this.$urlRouter.urlRuleFactory.create(state)); diff --git a/src/state/stateRegistry.ts b/src/state/stateRegistry.ts index b42278e9..e737576a 100644 --- a/src/state/stateRegistry.ts +++ b/src/state/stateRegistry.ts @@ -3,7 +3,7 @@ * @module state */ /** for typedoc */ -import { State } from "./stateObject"; +import { StateObject } from "./stateObject"; import { StateMatcher } from "./stateMatcher"; import { StateBuilder } from "./stateBuilder"; import { StateQueueManager } from "./stateQueueManager"; @@ -25,8 +25,8 @@ import { propEq } from "../common/hof"; export type StateRegistryListener = (event: "registered"|"deregistered", states: StateDeclaration[]) => void; export class StateRegistry { - private _root: State; - private states: { [key: string]: State } = {}; + private _root: StateObject; + private states: { [key: string]: StateObject } = {}; matcher: StateMatcher; private builder: StateBuilder; @@ -127,20 +127,20 @@ export class StateRegistry { * If the state was successfully registered, then the object is fully built (See: [[StateBuilder]]). * If the state was only queued, then the object is not fully built. */ - register(stateDefinition: StateDeclaration): State { + register(stateDefinition: StateDeclaration): StateObject { return this.stateQueue.register(stateDefinition); } /** @hidden */ - private _deregisterTree(state: State) { + private _deregisterTree(state: StateObject) { let all = this.get().map(s => s.$$state()); - const getChildren = (states: State[]) => { + const getChildren = (states: StateObject[]) => { let children = all.filter(s => states.indexOf(s.parent) !== -1); return children.length === 0 ? children : children.concat(getChildren(children)); }; let children = getChildren([state]); - let deregistered: State[] = [state].concat(children).reverse(); + let deregistered: StateObject[] = [state].concat(children).reverse(); deregistered.forEach(state => { let $ur = this._router.urlRouter; @@ -160,7 +160,7 @@ export class StateRegistry { * If the state has children, they are are also removed from the registry. * * @param stateOrName the state's name or object representation - * @returns {State[]} a list of removed states + * @returns {StateObject[]} a list of removed states */ deregister(stateOrName: StateOrName) { let _state = this.get(stateOrName); diff --git a/src/state/stateService.ts b/src/state/stateService.ts index 613e174d..f9eaf250 100644 --- a/src/state/stateService.ts +++ b/src/state/stateService.ts @@ -18,7 +18,7 @@ import {Rejection, RejectType} from "../transition/rejectFactory"; import {Transition} from "../transition/transition"; import {StateOrName, StateDeclaration, TransitionPromise, LazyLoadResult} from "./interface"; -import {State} from "./stateObject"; +import {StateObject} from "./stateObject"; import {TargetState} from "./targetState"; import {RawParams} from "../params/interface"; @@ -204,7 +204,7 @@ export class StateService { * * @returns A promise representing the state of the new transition. See [[StateService.go]] */ - reload(reloadState?: StateOrName): Promise { + reload(reloadState?: StateOrName): Promise { return this.transitionTo(this.current, this.params, { reload: isDefined(reloadState) ? reloadState : true, inherit: false, diff --git a/src/state/targetState.ts b/src/state/targetState.ts index ef95035d..75eecf68 100644 --- a/src/state/targetState.ts +++ b/src/state/targetState.ts @@ -6,7 +6,7 @@ import { StateDeclaration, StateOrName, TargetStateDef } from "./interface"; import { ParamsOrArray } from "../params/interface"; import { TransitionOptions } from "../transition/interface"; -import { State } from "./stateObject"; +import { StateObject } from "./stateObject"; import { toJson } from "../common/common"; import { isString } from "../common/predicates"; @@ -58,7 +58,7 @@ export class TargetState { */ constructor( private _identifier: StateOrName, - private _definition?: State, + private _definition?: StateObject, _params?: ParamsOrArray, private _options: TransitionOptions = {} ) { @@ -81,7 +81,7 @@ export class TargetState { } /** The internal state object (if it was found) */ - $state(): State { + $state(): StateObject { return this._definition; } diff --git a/src/transition/hookBuilder.ts b/src/transition/hookBuilder.ts index 3ca2e16d..286c8a78 100644 --- a/src/transition/hookBuilder.ts +++ b/src/transition/hookBuilder.ts @@ -13,7 +13,7 @@ import { import {Transition} from "./transition"; import {TransitionHook} from "./transitionHook"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {PathNode} from "../path/node"; import {TransitionService} from "./transitionService"; import {TransitionEventType} from "./transitionEventType"; diff --git a/src/transition/hookRegistry.ts b/src/transition/hookRegistry.ts index ef4c359d..0fed443c 100644 --- a/src/transition/hookRegistry.ts +++ b/src/transition/hookRegistry.ts @@ -14,7 +14,7 @@ import { HookMatchCriterion, IMatchingNodes, HookFn } from "./interface"; import {Glob} from "../common/glob"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {TransitionEventType} from "./transitionEventType"; import { TransitionService } from "./transitionService"; @@ -31,10 +31,10 @@ import { TransitionService } from "./transitionService"; * - If a function, matchState calls the function with the state and returns true if the function's result is truthy. * @returns {boolean} */ -export function matchState(state: State, criterion: HookMatchCriterion) { +export function matchState(state: StateObject, criterion: HookMatchCriterion) { let toMatch = isString(criterion) ? [criterion] : criterion; - function matchGlobs(_state: State) { + function matchGlobs(_state: StateObject) { let globStrings = toMatch; for (let i = 0; i < globStrings.length; i++) { let glob = new Glob(globStrings[i]); diff --git a/src/transition/interface.ts b/src/transition/interface.ts index fcb425a2..b65a221f 100644 --- a/src/transition/interface.ts +++ b/src/transition/interface.ts @@ -6,7 +6,7 @@ import {StateDeclaration} from "../state/interface"; import {Predicate} from "../common/common"; import {Transition} from "./transition"; -import {State} from "../state/stateObject"; +import {StateObject} from "../state/stateObject"; import {PathNode} from "../path/node"; import {TargetState} from "../state/targetState"; import {RegisteredHook} from "./hookRegistry"; @@ -33,7 +33,7 @@ export interface TransitionOptions { * When transitioning to relative path (e.g '`^`'), this option defines which state to be relative from. * @default `$state.current` */ - relative ?: (string|StateDeclaration|State); + relative ?: (string|StateDeclaration|StateObject); /** * This option sets whether or not the transition's parameter values should be inherited from @@ -71,13 +71,13 @@ export interface TransitionOptions { * * @default `false` */ - reload ?: (boolean|string|StateDeclaration|State); + reload ?: (boolean|string|StateDeclaration|StateObject); /** * You can define your own Transition Options inside this property and use them, e.g., from a Transition Hook */ custom ?: any; /** @internalapi */ - reloadState ?: (State); + reloadState ?: (StateObject); /** @internalapi * If this transition is a redirect, this property should be the original Transition (which was redirected to this one) */ @@ -210,7 +210,7 @@ export interface TransitionHookFn { * - [[IHookRegistry.onEnter]] */ export interface TransitionStateHookFn { - (transition: Transition, state: State) : HookResult + (transition: Transition, state: StateObject) : HookResult } /** @@ -701,7 +701,7 @@ export interface IHookRegistry { } /** A predicate type which takes a [[State]] and returns a boolean */ -export type IStateMatch = Predicate +export type IStateMatch = Predicate /** * This object is used to configure whether or not a Transition Hook is invoked for a particular transition, * based on the Transition's "to state" and "from state". diff --git a/src/transition/transition.ts b/src/transition/transition.ts index 4002b890..b00138c1 100644 --- a/src/transition/transition.ts +++ b/src/transition/transition.ts @@ -20,7 +20,7 @@ import { matchState, makeEvent, RegisteredHook } from './hookRegistry'; import { HookBuilder } from './hookBuilder'; import { PathNode } from '../path/node'; import { PathFactory } from '../path/pathFactory'; -import { State } from '../state/stateObject'; +import { StateObject } from '../state/stateObject'; import { TargetState } from '../state/targetState'; import { Param } from '../params/param'; import { Resolvable } from '../resolve/resolvable'; @@ -32,7 +32,7 @@ import { RawParams } from '../params/interface'; import { ResolvableLiteral } from '../resolve/interface'; /** @hidden */ -const stateSelf: (_state: State) => StateDeclaration = prop("self"); +const stateSelf: (_state: StateObject) => StateDeclaration = prop("self"); /** * Represents a transition between two states. @@ -475,7 +475,7 @@ export class Transition implements IHookRegistry { * * @returns a list of ViewConfig objects for the given path. */ - views(pathname: string = "entering", state?: State): ViewConfig[] { + views(pathname: string = "entering", state?: StateObject): ViewConfig[] { let path = this._treeChanges[pathname]; path = !state ? path : path.filter(propEq('state', state)); return path.map(prop("views")).filter(identity).reduce(unnestR, []); @@ -540,7 +540,7 @@ export class Transition implements IHookRegistry { // The redirected transition does not have to re-fetch the resolve. // --------------------------------------------------------- - const nodeIsReloading = (reloadState: State) => (node: PathNode) => { + const nodeIsReloading = (reloadState: StateObject) => (node: PathNode) => { return reloadState && node.state.includes[reloadState.name]; }; @@ -677,7 +677,7 @@ export class Transition implements IHookRegistry { * @returns an error message explaining why the transition is invalid, or the reason the transition failed. */ error() { - let state: State = this.$to(); + let state: StateObject = this.$to(); if (state.self.abstract) return `Cannot transition to abstract state '${state.name}'`; diff --git a/src/transition/transitionHook.ts b/src/transition/transitionHook.ts index 9c087762..ef48c5ca 100644 --- a/src/transition/transitionHook.ts +++ b/src/transition/transitionHook.ts @@ -13,7 +13,7 @@ import { services } from '../common/coreservices'; import { Rejection } from './rejectFactory'; import { TargetState } from '../state/targetState'; import { Transition } from './transition'; -import { State } from '../state/stateObject'; +import { StateObject } from '../state/stateObject'; import { TransitionEventType } from './transitionEventType'; import { RegisteredHook } from './hookRegistry'; // has or is using @@ -34,7 +34,7 @@ export type ErrorHandler = (error) => Promise; export class TransitionHook { type: TransitionEventType; constructor(private transition: Transition, - private stateContext: State, + private stateContext: StateObject, private registeredHook: RegisteredHook, private options: TransitionHookOptions) { this.options = defaults(options, defaultOptions); diff --git a/src/url/interface.ts b/src/url/interface.ts index 6de5b4d8..5894d91a 100644 --- a/src/url/interface.ts +++ b/src/url/interface.ts @@ -16,7 +16,7 @@ import { UIRouter } from "../router"; import { TargetState } from "../state/targetState"; import { TargetStateDef } from "../state/interface"; import { UrlMatcher } from "./urlMatcher"; -import { State } from "../state/stateObject"; +import { StateObject } from "../state/stateObject"; import { ParamTypeDefinition } from "../params/interface"; /** @internalapi */ @@ -476,7 +476,7 @@ export interface MatcherUrlRule extends UrlRule { /** @internalapi */ export interface StateRule extends MatcherUrlRule { type: "STATE"; - state: State; + state: StateObject; } /** @internalapi */ diff --git a/src/url/urlRule.ts b/src/url/urlRule.ts index 2952c303..34318233 100644 --- a/src/url/urlRule.ts +++ b/src/url/urlRule.ts @@ -7,7 +7,7 @@ import { isString, isDefined, isFunction, isState } from "../common/predicates"; import { UIRouter } from "../router"; import { identity, extend } from "../common/common"; import { is, pattern } from "../common/hof"; -import { State } from "../state/stateObject"; +import { StateObject } from "../state/stateObject"; import { RawParams } from "../params/interface"; import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn, UrlRuleType, UrlParts, MatcherUrlRule, StateRule, RegExpRule @@ -34,11 +34,11 @@ export class UrlRuleFactory { static isUrlRule = obj => obj && ['type', 'match', 'handler'].every(key => isDefined(obj[key])); - create(what: string|UrlMatcher|State|RegExp|UrlRuleMatchFn, handler?: string|UrlRuleHandlerFn): UrlRule { + create(what: string|UrlMatcher|StateObject|RegExp|UrlRuleMatchFn, handler?: string|UrlRuleHandlerFn): UrlRule { const makeRule = pattern([ [isString, (_what: string) => makeRule(this.compile(_what))], [is(UrlMatcher), (_what: UrlMatcher) => this.fromUrlMatcher(_what, handler)], - [isState, (_what: State) => this.fromState(_what, this.router)], + [isState, (_what: StateObject) => this.fromState(_what, this.router)], [is(RegExp), (_what: RegExp) => this.fromRegExp(_what, handler)], [isFunction, (_what: UrlRuleMatchFn) => new BaseUrlRule(_what, handler as UrlRuleHandlerFn)], ]); @@ -122,7 +122,7 @@ export class UrlRuleFactory { * // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' } * ``` */ - fromState(state: State, router: UIRouter): StateRule { + fromState(state: StateObject, router: UIRouter): StateRule { /** * Handles match by transitioning to matched state * diff --git a/test/hookBuilderSpec.ts b/test/hookBuilderSpec.ts index dfa1b438..db2b0a74 100644 --- a/test/hookBuilderSpec.ts +++ b/test/hookBuilderSpec.ts @@ -1,4 +1,4 @@ -import { UIRouter, TransitionService, StateService, State, PathNode } from "../src/index"; +import { UIRouter, TransitionService, StateService, StateObject, PathNode } from "../src/index"; import { tree2Array } from "./_testUtils"; import { TransitionHookPhase } from "../src/transition/interface"; import { TestingPlugin } from "./_testingPlugin"; @@ -7,7 +7,7 @@ describe('HookBuilder:', function() { let uiRouter: UIRouter = null; let $trans: TransitionService = null; let $state: StateService = null; - let root: State = null; + let root: StateObject = null; let log = ""; let hookNames = [ "onBefore", "onStart", "onExit", "onRetain", "onEnter", "onFinish", "onSuccess", "onError" ]; diff --git a/test/resolveSpec.ts b/test/resolveSpec.ts index db6c1ccf..63ad8399 100644 --- a/test/resolveSpec.ts +++ b/test/resolveSpec.ts @@ -1,4 +1,4 @@ -import { ResolveContext, State, PathNode, Resolvable, copy } from "../src/index"; +import { ResolveContext, StateObject, PathNode, Resolvable, copy } from "../src/index"; import { services } from "../src/common/coreservices"; import * as vanilla from "../src/vanilla"; import { tree2Array } from "./_testUtils"; @@ -13,7 +13,7 @@ import { tail } from "../src/common/common"; /////////////////////////////////////////////// -let router: UIRouter, states, statesMap: { [key:string]: State } = {}; +let router: UIRouter, states, statesMap: { [key:string]: StateObject } = {}; let $state: StateService; let $transitions: TransitionService; let $registry: StateRegistry; diff --git a/test/stateBuilderSpec.ts b/test/stateBuilderSpec.ts index 32403aa3..60164d89 100644 --- a/test/stateBuilderSpec.ts +++ b/test/stateBuilderSpec.ts @@ -1,7 +1,7 @@ import { StateMatcher, StateBuilder, UrlMatcher, extend } from "../src/index"; import { ParamTypes } from "../src/params/paramTypes"; import { UIRouter } from "../src/router"; -import { State } from "../src/state/stateObject"; +import { StateObject } from "../src/state/stateObject"; let paramTypes = new ParamTypes(); describe('StateBuilder', function() { @@ -68,15 +68,15 @@ describe('StateBuilder', function() { describe('state building', function() { it('should build parent property', function() { - let about = State.create({ name: 'home.about' }); + let about = StateObject.create({ name: 'home.about' }); expect(builder.builder('parent')(about)).toBe(states['home'].$$state()); }); it('should inherit parent data', function() { - let state = State.create(states['home.withData.child']); + let state = StateObject.create(states['home.withData.child']); expect(builder.builder('data')(state)).toEqualData({ val1: "foo", val2: "baz" }); - state = State.create(states['home.withData']); + state = StateObject.create(states['home.withData']); expect(builder.builder('data')(state)).toEqualData({ val1: "foo", val2: "bar" }); }); @@ -97,7 +97,7 @@ describe('StateBuilder', function() { let root = states[''].$$state(); spyOn(root.url, 'append').and.callThrough(); - let childstate = State.create({ name: 'asdf', url: "/foo" }); + let childstate = StateObject.create({ name: 'asdf', url: "/foo" }); builder.builder('url')(childstate); expect(root.url.append).toHaveBeenCalled(); @@ -159,7 +159,7 @@ describe('StateBuilder', function() { MyNestedStateClass.prototype = nestedProto; function MyNestedStateClass () { } - let router, myBuiltState: State, myNestedBuiltState: State; + let router, myBuiltState: StateObject, myNestedBuiltState: StateObject; beforeEach(() => { router = new UIRouter(); myNestedBuiltState = router.stateRegistry.register(new MyNestedStateClass()); diff --git a/test/stateServiceSpec.ts b/test/stateServiceSpec.ts index 58af5f07..de964e61 100644 --- a/test/stateServiceSpec.ts +++ b/test/stateServiceSpec.ts @@ -5,7 +5,7 @@ import { TransitionOptions } from "../src/transition/interface"; import { LocationServices, services } from "../src/common/coreservices"; import { isFunction } from "../src/common/predicates"; import { StateRegistry } from "../src/state/stateRegistry"; -import { State } from "../src/state/stateObject"; +import { StateObject } from "../src/state/stateObject"; import { Transition } from "../src/transition/transition"; import { Param } from "../src/params/param"; import { RejectType } from "../src/transition/rejectFactory"; @@ -209,7 +209,7 @@ describe('stateService', function () { router.stateRegistry.register(childNoParam); function logChangedParams(prefix, suffix) { - return (trans: Transition, state: State) => { + return (trans: Transition, state: StateObject) => { trans.onSuccess({}, () => { let changed = Param.changed(state.parameters({ inherit: true }), trans.params("to"), trans.params("from")) .map(param => param.id + "=" + trans.params("to")[param.id])