From 8719334c52a8c177a96b1bc18cb3446a3168f6cb Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Sat, 21 Jan 2017 11:21:32 -0600 Subject: [PATCH] feat(globals): Removed `UIRouterGlobals` interface. Renamed `Globals` class to `UIRouterGlobals` BREAKING CHANGE: This change will likely only affect a small subset of typescript users and probably only those using `ui-router-ng2`. If you're injecting the `Globals` class somewhere, e.g.: ``` @Injectable() class MyService { _globals: UIRouterGlobals; constructor(globals: Globals) { this._globals = globals; } } ``` you should now inject `UIRouterGlobals`, e.g.: ``` @Injectable() class MyService { constructor(public globals: UIRouterGlobals) { } } ``` Likewise, if you were casting the `UIRouter.globals` object as a `UIRouterGlobals`, it is no longer necessary: ```js function myHook(trans: Transition) { let globals: UIRouterGlobals = trans.router.globals; // cast is no longer necessary } ``` Closes https://github.com/ui-router/core/issues/31 --- .npmignore | 1 + src/globals.ts | 28 +++++++--------------------- src/hooks/updateGlobals.ts | 3 +-- src/router.ts | 6 +++--- src/state/stateService.ts | 19 +++++++++---------- src/transition/transition.ts | 3 +-- 6 files changed, 22 insertions(+), 38 deletions(-) diff --git a/.npmignore b/.npmignore index 5bd94e87..da0aa453 100644 --- a/.npmignore +++ b/.npmignore @@ -11,6 +11,7 @@ karma.**.js tslint.json tsconfig.json tsconfig.*.json +yarn.lock *.iml *.ipr diff --git a/src/globals.ts b/src/globals.ts index b9aa7c84..d432355b 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -16,47 +16,33 @@ import {copy} from "./common/common"; * This is where we hold the global mutable state such as current state, current * params, current transition, etc. */ -export interface UIRouterGlobals { +export class UIRouterGlobals { /** * Current parameter values * * The parameter values from the latest successful transition */ - params: StateParams; + params: StateParams = new StateParams(); + /** * Current state * * The to-state from the latest successful transition */ current: StateDeclaration; + /** - * Current state + * Current state (internal object) * * The to-state from the latest successful transition + * @internalapi */ $current: State; + /** * The current transition (in progress) */ transition: Transition; -} - - -/** - * Global router state - * - * This is where we hold the global mutable state such as current state, current - * params, current transition, etc. - */ -export class Globals implements UIRouterGlobals { - /** @inheritdoc */ - params: StateParams = new StateParams(); - /** @inheritdoc */ - current: StateDeclaration; - /** @inheritdoc */ - $current: State; - /** @inheritdoc */ - transition: Transition; /** @internalapi */ transitionHistory = new Queue([], 1); diff --git a/src/hooks/updateGlobals.ts b/src/hooks/updateGlobals.ts index b177ff0d..d7acbdf4 100644 --- a/src/hooks/updateGlobals.ts +++ b/src/hooks/updateGlobals.ts @@ -1,7 +1,6 @@ /** @module hooks */ /** for typedoc */ import { Transition } from "../transition/transition"; import { copy } from "../common/common"; -import { Globals } from "../globals"; import { TransitionService } from "../transition/transitionService"; /** @@ -17,7 +16,7 @@ import { TransitionService } from "../transition/transitionService"; * [[StateService.transition]], [[StateService.current]], [[StateService.params]] */ const updateGlobalState = (trans: Transition) => { - let globals = trans.router.globals as Globals; + let globals = trans.router.globals; globals.transition = trans; globals.transitionHistory.enqueue(trans); diff --git a/src/router.ts b/src/router.ts index ecfb926f..59dd1020 100644 --- a/src/router.ts +++ b/src/router.ts @@ -8,7 +8,7 @@ import { TransitionService } from "./transition/transitionService"; import { ViewService } from "./view/view"; import { StateRegistry } from "./state/stateRegistry"; import { StateService } from "./state/stateService"; -import { UIRouterGlobals, Globals } from "./globals"; +import { UIRouterGlobals } from "./globals"; import { UIRouterPlugin, Disposable } from "./interface"; import { values, removeFrom } from "./common/common"; import { isFunction } from "./common/predicates"; @@ -48,7 +48,7 @@ export class UIRouter { transitionService: TransitionService = new TransitionService(this); /** Global router state */ - globals: UIRouterGlobals = new Globals(); + globals: UIRouterGlobals = new UIRouterGlobals(); /** * Deprecated for public use. Use [[urlService]] instead. @@ -204,8 +204,8 @@ export class UIRouter { * @param pluginName (optional) the name of the plugin to get * @return the named plugin (undefined if not found), or all plugins (if `pluginName` is omitted) */ + getPlugin(pluginName: string): UIRouterPlugin; getPlugin(): UIRouterPlugin[]; - getPlugin(pluginName?: string): UIRouterPlugin; getPlugin(pluginName?: string): UIRouterPlugin|UIRouterPlugin[] { return pluginName ? this._plugins[pluginName] : values(this._plugins); } diff --git a/src/state/stateService.ts b/src/state/stateService.ts index 309e1d27..fd261b21 100644 --- a/src/state/stateService.ts +++ b/src/state/stateService.ts @@ -26,7 +26,6 @@ import {ParamsOrArray} from "../params/interface"; import {Param} from "../params/param"; import {Glob} from "../common/glob"; import {HrefOptions} from "./interface"; -import {Globals} from "../globals"; import {UIRouter} from "../router"; import {UIInjector} from "../interface"; import {ResolveContext} from "../resolve/resolveContext"; @@ -98,7 +97,7 @@ export class StateService { */ private _handleInvalidTargetState(fromPath: PathNode[], toState: TargetState) { let fromState = PathFactory.makeTargetState(fromPath); - let globals = this.router.globals; + let globals = this.router.globals; const latestThing = () => globals.transitionHistory.peekTail(); let latest = latestThing(); let callbackQueue = new Queue(this.invalidCallbacks.slice()); @@ -294,7 +293,7 @@ export class StateService { }; private getCurrentPath(): PathNode[] { - let globals = this.router.globals; + let globals = this.router.globals; let latestSuccess: Transition = globals.successfulTransitions.peekTail(); const rootPath = () => [ new PathNode(this.router.stateRegistry.root()) ]; return latestSuccess ? latestSuccess.treeChanges().to : rootPath(); @@ -325,7 +324,7 @@ export class StateService { */ transitionTo(to: StateOrName, toParams: RawParams = {}, options: TransitionOptions = {}): TransitionPromise { let router = this.router; - let globals = router.globals; + let globals = router.globals; let transHistory = globals.transitionHistory; options = defaults(options, defaultTransOpts); options = extend(options, { current: transHistory.peekTail.bind(transHistory)}); @@ -370,7 +369,7 @@ export class StateService { } } - var errorHandler = this.defaultErrorHandler(); + let errorHandler = this.defaultErrorHandler(); errorHandler(error); return services.$q.reject(error); @@ -579,15 +578,15 @@ export class StateService { * * Returns the state declaration object for any specific state, or for all registered states. * - * @param stateOrName (absolute or relative) If provided, will only get the config for - * the requested state. If not provided, returns an array of ALL state configs. - * @param base When stateOrName is a relative state reference, the state will be retrieved relative to context. + * @param stateOrName (absolute or relative) If provided, will only get the declaration object for the requested state. + * If not provided, returns an array of ALL states. + * @param base When `stateOrName` is a relative state reference (such as `.bar.baz`), the state will be retrieved relative to this state. * * @returns a [[StateDeclaration]] object (or array of all registered [[StateDeclaration]] objects.) */ - get(): StateDeclaration[]; - get(stateOrName: StateOrName): StateDeclaration; get(stateOrName: StateOrName, base: StateOrName): StateDeclaration; + get(stateOrName: StateOrName): StateDeclaration; + get(): StateDeclaration[]; get(stateOrName?: StateOrName, base?: StateOrName): any { let reg = this.router.stateRegistry; if (arguments.length === 0) return reg.get(); diff --git a/src/transition/transition.ts b/src/transition/transition.ts index 2924e76b..3f9bd375 100644 --- a/src/transition/transition.ts +++ b/src/transition/transition.ts @@ -33,7 +33,6 @@ import {ViewConfig} from "../view/interface"; import {Rejection} from "./rejectFactory"; import {ResolveContext} from "../resolve/resolveContext"; import {UIRouter} from "../router"; -import {Globals} from "../globals"; import {UIInjector} from "../interface"; import {RawParams} from "../params/interface"; import { ResolvableLiteral } from "../resolve/interface"; @@ -628,7 +627,7 @@ export class Transition implements IHookRegistry { run(): Promise { let runAllHooks = TransitionHook.runAllHooks; let hookBuilder = this.hookBuilder(); - let globals = this.router.globals; + let globals = this.router.globals; globals.transitionHistory.enqueue(this); let onBeforeHooks = hookBuilder.buildHooksForPhase(TransitionHookPhase.BEFORE);