Skip to content

Commit

Permalink
refactor(common): simplify references to angular.* functions
Browse files Browse the repository at this point in the history
refactor(*): for typescript 1.8: export all interfaces/classes/enums exposed externally
refactor(*): for typescript 1.8: switch up /// <reference paths
refactor(Router): rename to UIRouter to avoid same name as component Router
refactor(*): Switch types from angular IPromise to ES6 Promise
  • Loading branch information
christopherthielen committed Feb 19, 2016
1 parent b3bd2fe commit 002e8d1
Show file tree
Hide file tree
Showing 30 changed files with 65 additions and 75 deletions.
22 changes: 14 additions & 8 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import {isFunction, isString, isArray, isRegExp, isDate} from "./predicates";
import { all, pattern, any, not, prop, curry, val } from "./hof";

let angular = (<any> window).angular;
export const fromJson = angular && angular.fromJson || _fromJson;
export const toJson = angular && angular.toJson || _toJson;
export const copy = angular && angular.copy || _copy;
export const forEach = angular && angular.forEach || _forEach;
export const extend = angular && angular.extend || _extend;
export const equals = angular && angular.equals || _equals;
let angular = (<any> window).angular || {};
export const fromJson = angular.fromJson || _fromJson;
export const toJson = angular.toJson || _toJson;
export const copy = angular.copy || _copy;
export const forEach = angular.forEach || _forEach;
export const extend = angular.extend || _extend;
export const equals = angular.equals || _equals;
export const identity = (x) => x;
export const noop = () => undefined;

type Mapper<X, T> = (x: X, key?: (string|number)) => T;
export type Mapper<X, T> = (x: X, key?: (string|number)) => T;
export interface TypedMap<T> { [key: string]: T; }
export type Predicate<X> = (X) => boolean;
export type IInjectable = (Function|any[]);
Expand Down Expand Up @@ -570,3 +570,9 @@ function _arraysEq(a1, a2) {
if (a1.length !== a2.length) return false;
return arrayTuples(a1, a2).reduce((b, t) => b && _equals(t[0], t[1]), true);
}
//
//const _addToGroup = (result, keyFn) => (item) =>
// (result[keyFn(item)] = result[keyFn(item)] || []).push(item) && result;
//const groupBy = (array, keyFn) => array.reduce((memo, item) => _addToGroup(memo, keyFn), {});
//
//
8 changes: 4 additions & 4 deletions src/common/coreservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let services: CoreServices = {
["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix" ]
.forEach(key => services.locationConfig[key] = notImplemented(key));

interface CoreServices {
export interface CoreServices {
$q; // : IQService;
$injector; // : IInjectorService;
/** Services related to getting or setting the browser location (url) */
Expand All @@ -37,7 +37,7 @@ interface CoreServices {
template: TemplateServices;
}

interface LocationServices {
export interface LocationServices {
replace(): void;
url(newurl: string): string;
url(): string;
Expand All @@ -47,7 +47,7 @@ interface LocationServices {
onChange(callback: Function): Function;
}

interface LocationConfig {
export interface LocationConfig {
port(): number;
protocol(): string;
host(): string;
Expand All @@ -58,7 +58,7 @@ interface LocationConfig {
hashPrefix(newprefix: string): string;
}

interface TemplateServices {
export interface TemplateServices {
get(url: string): string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ function stringify(o) {
return JSON.stringify(o, (key, val) => format(val)).replace(/\\"/g, '"');
}

enum Category {
export enum Category {
RESOLVE, TRANSITION, HOOK, INVOKE, UIVIEW, VIEWCONFIG
}

class Trace {
export class Trace {
approximateDigests: number;

constructor() {
Expand Down
2 changes: 2 additions & 0 deletions src/ng1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path='../typings/angularjs/angular.d.ts' />
/// <reference path='../typings/es6-shim/es6-shim.d.ts' />
/**
* Main entry point for angular 1.x build
*/
Expand Down
10 changes: 6 additions & 4 deletions src/ng1/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
* @preferred
*/

/** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />
import {Router} from "../router";
/// <reference path='../../typings/es6-shim/es6-shim.d.ts' />

/** for typedoc */
import {UIRouter} from "../router";
import {services} from "../common/coreservices";
import {map, bindFunctions, removeFrom, find, noop} from "../common/common";
import {prop, propEq} from "../common/hof";
Expand Down Expand Up @@ -148,14 +150,14 @@ function runBlock($injector, $q) {

app.run(runBlock);

let router: Router = null;
let router: UIRouter = null;

ng1UIRouter.$inject = ['$locationProvider'];
/** This angular 1 provider instantiates a Router and exposes its services via the angular injector */
function ng1UIRouter($locationProvider) {

// Create a new instance of the Router when the ng1UIRouterProvider is initialized
router = new Router();
router = new UIRouter();

// Bind LocationConfig.hashPrefix to $locationProvider.hashPrefix
bindFunctions($locationProvider, services.locationConfig, $locationProvider, ['hashPrefix']);
Expand Down
2 changes: 0 additions & 2 deletions src/ng1/stateEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/

/** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />

import {IServiceProviderFactory} from "angular";
import {StateService, StateProvider} from "../state/interface";
import {TargetState} from "../state/module";
Expand Down
1 change: 0 additions & 1 deletion src/ng1/stateFilters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module state */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />

/**
* @ngdoc filter
Expand Down
2 changes: 0 additions & 2 deletions src/ng1/viewDirective.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var ngMajorVer = angular.version.major;
var ngMinorVer = angular.version.minor;
/** @module view */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />

import {extend} from "../common/common";
import {isDefined} from "../common/predicates";
import {trace} from "../common/trace";
Expand Down
1 change: 0 additions & 1 deletion src/ng1/viewScroll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module view */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />
import {IServiceProviderFactory} from "angular";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/params/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export interface ParamDeclaration {
dynamic: boolean;
}

interface Replace {
export interface Replace {
from: string;
to: string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/params/paramTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Type} from "./type";
function valToString(val) { return val != null ? val.toString().replace(/~/g, "~~").replace(/\//g, "~2F") : val; }
function valFromString(val) { return val != null ? val.toString().replace(/~2F/g, "/").replace(/~~/g, "~") : val; }

class ParamTypes {
export class ParamTypes {
types: any;
enqueue: boolean = true;
typeQueue: any[] = [];
Expand Down
6 changes: 2 additions & 4 deletions src/resolve/resolvable.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/** @module path */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />
import {extend, pick, map, filter} from "../common/common";
import {not} from "../common/hof";
import {isInjectable} from "../common/predicates";

import {services} from "../common/coreservices";
import {trace} from "../common/trace";
import {IPromise} from "angular";
import {Resolvables, IOptions1} from "./interface";

import {ResolveContext} from "./resolveContext";
Expand All @@ -32,7 +30,7 @@ export class Resolvable {
resolveFn: Function;
deps: string[];

promise: IPromise<any> = undefined;
promise: Promise<any> = undefined;
data: any;

// synchronous part:
Expand Down Expand Up @@ -80,7 +78,7 @@ export class Resolvable {
});
}

get(resolveContext: ResolveContext, options?: IOptions1): IPromise<any> {
get(resolveContext: ResolveContext, options?: IOptions1): Promise<any> {
return this.promise || this.resolveResolvable(resolveContext, options);
}

Expand Down
14 changes: 6 additions & 8 deletions src/resolve/resolveContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/** @module path */ /** for typedoc */
/// <reference path='../../typings/angularjs/angular.d.ts' />
import {IInjectable, find, filter, map, tail, defaults, extend, pick, omit} from "../common/common";
import {prop, propEq} from "../common/hof";
import {isString, isObject} from "../common/predicates";
import {trace} from "../common/trace";
import {services} from "../common/coreservices";
import {IPromise} from "angular";
import {Resolvables, ResolvePolicy, IOptions1} from "./interface";

import {Node} from "../path/module";
Expand All @@ -17,7 +15,7 @@ import {mergeR} from "../common/common";
let defaultResolvePolicy = ResolvePolicy[ResolvePolicy.LAZY];

interface IPolicies { [key: string]: string; }
interface IPromises { [key: string]: IPromise<any>; }
interface Promises { [key: string]: Promise<any>; }

export class ResolveContext {

Expand Down Expand Up @@ -93,7 +91,7 @@ export class ResolveContext {
}

// Returns a promise for an array of resolved path Element promises
resolvePath(options: IOptions1 = {}): IPromise<any> {
resolvePath(options: IOptions1 = {}): Promise<any> {
trace.traceResolvePath(this._path, options);
const promiseForNode = (node: Node) => this.resolvePathElement(node.state, options);
return services.$q.all(<any> map(this._path, promiseForNode)).then(all => all.reduce(mergeR, {}));
Expand All @@ -103,7 +101,7 @@ export class ResolveContext {
// options.resolvePolicy: only return promises for those Resolvables which are at
// the specified policy, or above. i.e., options.resolvePolicy === 'lazy' will
// resolve both 'lazy' and 'eager' resolves.
resolvePathElement(state: State, options: IOptions1 = {}): IPromise<any> {
resolvePathElement(state: State, options: IOptions1 = {}): Promise<any> {
// The caller can request the path be resolved for a given policy and "below"
let policy: string = options && options.resolvePolicy;
let policyOrdinal: number = ResolvePolicy[policy || defaultResolvePolicy];
Expand All @@ -114,7 +112,7 @@ export class ResolveContext {
let matchingResolves = filter(resolvables, matchesRequestedPolicy);

const getResolvePromise = (resolvable: Resolvable) => resolvable.get(this.isolateRootTo(state), options);
let resolvablePromises: IPromises = <any> map(matchingResolves, getResolvePromise);
let resolvablePromises: Promises = <any> map(matchingResolves, getResolvePromise);

trace.traceResolvePathElement(this, matchingResolves, options);

Expand All @@ -135,11 +133,11 @@ export class ResolveContext {
* @param locals: are the angular $injector-style locals to inject
* @param options: options (TODO: document)
*/
invokeLater(fn: IInjectable, locals: any = {}, options: IOptions1 = {}): IPromise<any> {
invokeLater(fn: IInjectable, locals: any = {}, options: IOptions1 = {}): Promise<any> {
let resolvables = this.getResolvablesForFn(fn);
trace.tracePathElementInvoke(tail(this._path), fn, Object.keys(resolvables), extend({when: "Later"}, options));
const getPromise = (resolvable: Resolvable) => resolvable.get(this, options);
let promises: IPromises = <any> map(resolvables, getPromise);
let promises: Promises = <any> map(resolvables, getPromise);

return services.$q.all(promises).then(() => {
try {
Expand Down
1 change: 1 addition & 0 deletions src/resolve/resolveInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {map} from "../common/common";
import {Resolvable} from "./resolvable";
import {ResolveContext} from "./resolveContext";
import {State} from "../state/module";
import {TypedMap} from "../common/common";

export class ResolveInjector {
constructor(private _resolveContext: ResolveContext, private _state: State) { }
Expand Down
5 changes: 3 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {UrlMatcherFactory} from "./url/urlMatcherFactory";
import {UrlRouterProvider} from "./url/urlRouter";
import {StateProvider} from "./state/state";
import {stateParamsFactory} from "./params/stateParams";
import {StateParams} from "./params/stateParams";
import {UrlRouter} from "./url/urlRouter";
import {TransitionService} from "./transition/transitionService";
import {TemplateFactory} from "./view/templateFactory";
Expand All @@ -18,7 +19,7 @@ import {StateService} from "./state/stateService";
* your app states with the [[stateRegistry]] (and set url options using ...). Then, tell UI-Router to monitor
* the URL by calling `urlRouter.listen()` ([[URLRouter.listen]])
*/
class Router {
class UIRouter {

stateParams = stateParamsFactory();

Expand Down Expand Up @@ -46,4 +47,4 @@ class Router {
}
}

export { Router };
export { UIRouter };
4 changes: 2 additions & 2 deletions src/state/hooks/resolveHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class ResolveHooks {
let treeChanges = this.transition.treeChanges();

/** a function which resolves any EAGER Resolvables for a Path */
$eagerResolvePath.$inject = ['$transition$'];
(<any> $eagerResolvePath).$inject = ['$transition$'];
function $eagerResolvePath($transition$) {
return tail(<any[]> treeChanges.to).resolveContext.resolvePath(extend({ transition: $transition$ }, { resolvePolicy: EAGER }));
}

/** Returns a function which pre-resolves any LAZY Resolvables for a Node in a Path */
$lazyResolveEnteringState.$inject = ['$state$', '$transition$'];
(<any> $lazyResolveEnteringState).$inject = ['$state$', '$transition$'];
function $lazyResolveEnteringState($state$, $transition$) {
let node = find(<any[]> treeChanges.entering, propEq('state', $state$));
return node.resolveContext.resolvePathElement(node.state, extend({transition: $transition$}, { resolvePolicy: LAZY }));
Expand Down
7 changes: 3 additions & 4 deletions src/state/hooks/transitionManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module state */ /** for typedoc */
import {IPromise, IQService} from "angular";
import {copy} from "../../common/common";
import {prop} from "../../common/hof";
import {Queue} from "../../common/queue";
Expand Down Expand Up @@ -45,7 +44,7 @@ export class TransitionManager {
private $view, // service
private $state: StateService,
private $stateParams, // service/obj
private $q: IQService, // TODO: get from runtime.$q
private $q, // TODO: get from runtime.$q
private activeTransQ: Queue<Transition>,
private changeHistory: Queue<TreeChanges>
) {
Expand All @@ -61,7 +60,7 @@ export class TransitionManager {
this.resolveHooks.registerHooks();
}

runTransition(): IPromise<any> {
runTransition(): Promise<any> {
this.activeTransQ.clear(); // TODO: nuke this
this.activeTransQ.enqueue(this.transition);
this.$state.transition = this.transition;
Expand Down Expand Up @@ -92,7 +91,7 @@ export class TransitionManager {
this.updateStateParams();
}

transRejected(error): (StateDeclaration|IPromise<any>) {
transRejected(error): (StateDeclaration|Promise<any>) {
let {transition, $state, $stateParams, $q} = this;
// Handle redirect and abort
if (error instanceof TransitionRejection) {
Expand Down
5 changes: 2 additions & 3 deletions src/state/hooks/viewHooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module state */ /** for typedoc */
import {IPromise} from "angular";
import {find, noop} from "../../common/common";
import {propEq} from "../../common/hof";
import {services} from "../../common/coreservices";
Expand All @@ -15,7 +14,7 @@ export class ViewHooks {
private enteringViews: ViewConfig[];
private exitingViews: ViewConfig[];
private transition: Transition;
private $view; // service
private $view: ViewService; // service

constructor(transition: Transition, $view: ViewService) {
this.transition = transition;
Expand All @@ -29,7 +28,7 @@ export class ViewHooks {
loadAllEnteringViews() {
const loadView = (vc: ViewConfig) => {
let resolveInjector = find(this.treeChanges.to, propEq('state', vc.context)).resolveInjector;
return <IPromise<ViewConfig>> this.$view.load(vc, resolveInjector);
return <Promise<ViewConfig>> this.$view.load(vc, resolveInjector);
};
return services.$q.all(this.enteringViews.map(loadView)).then(noop);
}
Expand Down
8 changes: 3 additions & 5 deletions src/state/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/** @module state */ /** for typedoc */
import {IPromise} from "angular";

import {TransitionOptions} from "../transition/interface";
import {ParamDeclaration, RawParams, ParamsOrArray} from "../params/interface";

Expand Down Expand Up @@ -417,10 +415,10 @@ export interface StateService {
current: StateDeclaration;
$current: State;
transition: Transition;
reload (stateOrName: StateOrName): IPromise<State>;
reload (stateOrName: StateOrName): Promise<State>;
target (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
go (to: StateOrName, params: RawParams, options: TransitionOptions): IPromise<State>;
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): IPromise<State>;
go (to: StateOrName, params: RawParams, options: TransitionOptions): Promise<State>;
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): Promise<State>;
is (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
includes (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
href (stateOrName: StateOrName, params?: RawParams, options?: HrefOptions): string;
Expand Down
1 change: 1 addition & 0 deletions src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {isObject} from "../common/predicates";
import {bindFunctions} from "../common/common";
import {BuilderFunction} from "./module";
import {StateRegistry} from "./stateRegistry";
import {State} from "./stateObject";

/**
* @ngdoc object
Expand Down
Loading

0 comments on commit 002e8d1

Please sign in to comment.