Skip to content

Commit 8719334

Browse files
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 = <UIRouterGlobals> 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 #31
1 parent 7f078c4 commit 8719334

File tree

6 files changed

+22
-38
lines changed

6 files changed

+22
-38
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ karma.**.js
1111
tslint.json
1212
tsconfig.json
1313
tsconfig.*.json
14+
yarn.lock
1415

1516
*.iml
1617
*.ipr

src/globals.ts

+7-21
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,33 @@ import {copy} from "./common/common";
1616
* This is where we hold the global mutable state such as current state, current
1717
* params, current transition, etc.
1818
*/
19-
export interface UIRouterGlobals {
19+
export class UIRouterGlobals {
2020
/**
2121
* Current parameter values
2222
*
2323
* The parameter values from the latest successful transition
2424
*/
25-
params: StateParams;
25+
params: StateParams = new StateParams();
26+
2627
/**
2728
* Current state
2829
*
2930
* The to-state from the latest successful transition
3031
*/
3132
current: StateDeclaration;
33+
3234
/**
33-
* Current state
35+
* Current state (internal object)
3436
*
3537
* The to-state from the latest successful transition
38+
* @internalapi
3639
*/
3740
$current: State;
41+
3842
/**
3943
* The current transition (in progress)
4044
*/
4145
transition: Transition;
42-
}
43-
44-
45-
/**
46-
* Global router state
47-
*
48-
* This is where we hold the global mutable state such as current state, current
49-
* params, current transition, etc.
50-
*/
51-
export class Globals implements UIRouterGlobals {
52-
/** @inheritdoc */
53-
params: StateParams = new StateParams();
54-
/** @inheritdoc */
55-
current: StateDeclaration;
56-
/** @inheritdoc */
57-
$current: State;
58-
/** @inheritdoc */
59-
transition: Transition;
6046

6147
/** @internalapi */
6248
transitionHistory = new Queue<Transition>([], 1);

src/hooks/updateGlobals.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @module hooks */ /** for typedoc */
22
import { Transition } from "../transition/transition";
33
import { copy } from "../common/common";
4-
import { Globals } from "../globals";
54
import { TransitionService } from "../transition/transitionService";
65

76
/**
@@ -17,7 +16,7 @@ import { TransitionService } from "../transition/transitionService";
1716
* [[StateService.transition]], [[StateService.current]], [[StateService.params]]
1817
*/
1918
const updateGlobalState = (trans: Transition) => {
20-
let globals = trans.router.globals as Globals;
19+
let globals = trans.router.globals;
2120
globals.transition = trans;
2221
globals.transitionHistory.enqueue(trans);
2322

src/router.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { TransitionService } from "./transition/transitionService";
88
import { ViewService } from "./view/view";
99
import { StateRegistry } from "./state/stateRegistry";
1010
import { StateService } from "./state/stateService";
11-
import { UIRouterGlobals, Globals } from "./globals";
11+
import { UIRouterGlobals } from "./globals";
1212
import { UIRouterPlugin, Disposable } from "./interface";
1313
import { values, removeFrom } from "./common/common";
1414
import { isFunction } from "./common/predicates";
@@ -48,7 +48,7 @@ export class UIRouter {
4848
transitionService: TransitionService = new TransitionService(this);
4949

5050
/** Global router state */
51-
globals: UIRouterGlobals = new Globals();
51+
globals: UIRouterGlobals = new UIRouterGlobals();
5252

5353
/**
5454
* Deprecated for public use. Use [[urlService]] instead.
@@ -204,8 +204,8 @@ export class UIRouter {
204204
* @param pluginName (optional) the name of the plugin to get
205205
* @return the named plugin (undefined if not found), or all plugins (if `pluginName` is omitted)
206206
*/
207+
getPlugin(pluginName: string): UIRouterPlugin;
207208
getPlugin(): UIRouterPlugin[];
208-
getPlugin(pluginName?: string): UIRouterPlugin;
209209
getPlugin(pluginName?: string): UIRouterPlugin|UIRouterPlugin[] {
210210
return pluginName ? this._plugins[pluginName] : values(this._plugins);
211211
}

src/state/stateService.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {ParamsOrArray} from "../params/interface";
2626
import {Param} from "../params/param";
2727
import {Glob} from "../common/glob";
2828
import {HrefOptions} from "./interface";
29-
import {Globals} from "../globals";
3029
import {UIRouter} from "../router";
3130
import {UIInjector} from "../interface";
3231
import {ResolveContext} from "../resolve/resolveContext";
@@ -98,7 +97,7 @@ export class StateService {
9897
*/
9998
private _handleInvalidTargetState(fromPath: PathNode[], toState: TargetState) {
10099
let fromState = PathFactory.makeTargetState(fromPath);
101-
let globals = <Globals> this.router.globals;
100+
let globals = this.router.globals;
102101
const latestThing = () => globals.transitionHistory.peekTail();
103102
let latest = latestThing();
104103
let callbackQueue = new Queue<OnInvalidCallback>(this.invalidCallbacks.slice());
@@ -294,7 +293,7 @@ export class StateService {
294293
};
295294

296295
private getCurrentPath(): PathNode[] {
297-
let globals = <Globals> this.router.globals;
296+
let globals = this.router.globals;
298297
let latestSuccess: Transition = globals.successfulTransitions.peekTail();
299298
const rootPath = () => [ new PathNode(this.router.stateRegistry.root()) ];
300299
return latestSuccess ? latestSuccess.treeChanges().to : rootPath();
@@ -325,7 +324,7 @@ export class StateService {
325324
*/
326325
transitionTo(to: StateOrName, toParams: RawParams = {}, options: TransitionOptions = {}): TransitionPromise {
327326
let router = this.router;
328-
let globals = <Globals> router.globals;
327+
let globals = router.globals;
329328
let transHistory = globals.transitionHistory;
330329
options = defaults(options, defaultTransOpts);
331330
options = extend(options, { current: transHistory.peekTail.bind(transHistory)});
@@ -370,7 +369,7 @@ export class StateService {
370369
}
371370
}
372371

373-
var errorHandler = this.defaultErrorHandler();
372+
let errorHandler = this.defaultErrorHandler();
374373
errorHandler(error);
375374

376375
return services.$q.reject(error);
@@ -579,15 +578,15 @@ export class StateService {
579578
*
580579
* Returns the state declaration object for any specific state, or for all registered states.
581580
*
582-
* @param stateOrName (absolute or relative) If provided, will only get the config for
583-
* the requested state. If not provided, returns an array of ALL state configs.
584-
* @param base When stateOrName is a relative state reference, the state will be retrieved relative to context.
581+
* @param stateOrName (absolute or relative) If provided, will only get the declaration object for the requested state.
582+
* If not provided, returns an array of ALL states.
583+
* @param base When `stateOrName` is a relative state reference (such as `.bar.baz`), the state will be retrieved relative to this state.
585584
*
586585
* @returns a [[StateDeclaration]] object (or array of all registered [[StateDeclaration]] objects.)
587586
*/
588-
get(): StateDeclaration[];
589-
get(stateOrName: StateOrName): StateDeclaration;
590587
get(stateOrName: StateOrName, base: StateOrName): StateDeclaration;
588+
get(stateOrName: StateOrName): StateDeclaration;
589+
get(): StateDeclaration[];
591590
get(stateOrName?: StateOrName, base?: StateOrName): any {
592591
let reg = this.router.stateRegistry;
593592
if (arguments.length === 0) return reg.get();

src/transition/transition.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {ViewConfig} from "../view/interface";
3333
import {Rejection} from "./rejectFactory";
3434
import {ResolveContext} from "../resolve/resolveContext";
3535
import {UIRouter} from "../router";
36-
import {Globals} from "../globals";
3736
import {UIInjector} from "../interface";
3837
import {RawParams} from "../params/interface";
3938
import { ResolvableLiteral } from "../resolve/interface";
@@ -628,7 +627,7 @@ export class Transition implements IHookRegistry {
628627
run(): Promise<any> {
629628
let runAllHooks = TransitionHook.runAllHooks;
630629
let hookBuilder = this.hookBuilder();
631-
let globals = <Globals> this.router.globals;
630+
let globals = this.router.globals;
632631
globals.transitionHistory.enqueue(this);
633632

634633
let onBeforeHooks = hookBuilder.buildHooksForPhase(TransitionHookPhase.BEFORE);

0 commit comments

Comments
 (0)