Skip to content

Commit

Permalink
feat(globals): Removed UIRouterGlobals interface. Renamed Globals
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
christopherthielen committed Jan 21, 2017
1 parent 7f078c4 commit 8719334
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 38 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ karma.**.js
tslint.json
tsconfig.json
tsconfig.*.json
yarn.lock

*.iml
*.ipr
Expand Down
28 changes: 7 additions & 21 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transition>([], 1);
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/updateGlobals.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 9 additions & 10 deletions src/state/stateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -98,7 +97,7 @@ export class StateService {
*/
private _handleInvalidTargetState(fromPath: PathNode[], toState: TargetState) {
let fromState = PathFactory.makeTargetState(fromPath);
let globals = <Globals> this.router.globals;
let globals = this.router.globals;
const latestThing = () => globals.transitionHistory.peekTail();
let latest = latestThing();
let callbackQueue = new Queue<OnInvalidCallback>(this.invalidCallbacks.slice());
Expand Down Expand Up @@ -294,7 +293,7 @@ export class StateService {
};

private getCurrentPath(): PathNode[] {
let globals = <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();
Expand Down Expand Up @@ -325,7 +324,7 @@ export class StateService {
*/
transitionTo(to: StateOrName, toParams: RawParams = {}, options: TransitionOptions = {}): TransitionPromise {
let router = this.router;
let globals = <Globals> router.globals;
let globals = router.globals;
let transHistory = globals.transitionHistory;
options = defaults(options, defaultTransOpts);
options = extend(options, { current: transHistory.peekTail.bind(transHistory)});
Expand Down Expand Up @@ -370,7 +369,7 @@ export class StateService {
}
}

var errorHandler = this.defaultErrorHandler();
let errorHandler = this.defaultErrorHandler();
errorHandler(error);

return services.$q.reject(error);
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/transition/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -628,7 +627,7 @@ export class Transition implements IHookRegistry {
run(): Promise<any> {
let runAllHooks = TransitionHook.runAllHooks;
let hookBuilder = this.hookBuilder();
let globals = <Globals> this.router.globals;
let globals = this.router.globals;
globals.transitionHistory.enqueue(this);

let onBeforeHooks = hookBuilder.buildHooksForPhase(TransitionHookPhase.BEFORE);
Expand Down

0 comments on commit 8719334

Please sign in to comment.