style(DI): idiomatic TS

This commit is contained in:
Victor Berchet 2015-06-17 10:12:06 +02:00
parent edd01615c3
commit ffd1ac425e
5 changed files with 13 additions and 19 deletions

View File

@ -15,7 +15,7 @@ import {CONST, stringify} from "angular2/src/facade/lang";
@CONST()
export class Inject {
constructor(public token) {}
toString() { return `@Inject(${stringify(this.token)})`; }
toString(): string { return `@Inject(${stringify(this.token)})`; }
}
/**
@ -34,7 +34,7 @@ export class Inject {
@CONST()
export class InjectPromise {
constructor(public token) {}
toString() { return `@InjectPromise(${stringify(this.token)})`; }
toString(): string { return `@InjectPromise(${stringify(this.token)})`; }
}
/**
@ -53,7 +53,7 @@ export class InjectPromise {
@CONST()
export class InjectLazy {
constructor(public token) {}
toString() { return `@InjectLazy(${stringify(this.token)})`; }
toString(): string { return `@InjectLazy(${stringify(this.token)})`; }
}
/**
@ -72,7 +72,7 @@ export class InjectLazy {
*/
@CONST()
export class Optional {
toString() { return `@Optional()`; }
toString(): string { return `@Optional()`; }
}
/**

View File

@ -3,6 +3,7 @@ import {
isBlank,
isPresent,
CONST,
CONST_EXPR,
BaseException,
stringify,
isArray
@ -30,7 +31,7 @@ export class Dependency {
static fromKey(key: Key) { return new Dependency(key, false, false, false, []); }
}
var _EMPTY_LIST = []; // TODO: make const when supported
const _EMPTY_LIST = CONST_EXPR([]);
/**
* Describes how the {@link Injector} should instantiate a given token.

View File

@ -140,6 +140,7 @@ export class CyclicDependencyError extends AbstractBindingError {
export class InstantiationError extends AbstractBindingError {
cause;
causeKey;
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(cause, key) {
super(key, function(keys: List<any>) {

View File

@ -1,4 +1,4 @@
import {Type, stringify} from 'angular2/src/facade/lang';
import {Type, stringify, isFunction} from 'angular2/src/facade/lang';
export interface ForwardRefFn { (): any; }
@ -42,7 +42,7 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type {
* @exportedAs angular2/di
*/
export function resolveForwardRef(type: any): any {
if (typeof type == 'function' && type.hasOwnProperty('__forward_ref__') &&
if (isFunction(type) && type.hasOwnProperty('__forward_ref__') &&
type.__forward_ref__ === forwardRef) {
return (<ForwardRefFn>type)();
} else {

View File

@ -5,8 +5,6 @@ import {resolveForwardRef} from './forward_ref';
export {TypeLiteral} from './type_literal';
// TODO: uncoment `int` once https://github.com/angular/angular/issues/1414 is fixed
/**
* A unique object used for retrieving items from the {@link Injector}.
*
@ -20,21 +18,16 @@ export {TypeLiteral} from './type_literal';
* @exportedAs angular2/di
*/
export class Key {
token: Object;
id: number;
/**
* @private
*/
constructor(token: Object, id: number) {
constructor(public token: Object, public id: number) {
if (isBlank(token)) {
throw new BaseException('Token must be defined!');
}
this.token = token;
this.id = id;
}
get displayName() { return stringify(this.token); }
get displayName(): string { return stringify(this.token); }
/**
* Retrieves a `Key` for a token.
@ -51,8 +44,7 @@ export class Key {
* @private
*/
export class KeyRegistry {
_allKeys: Map<Object, Key>;
constructor() { this._allKeys = MapWrapper.create(); }
private _allKeys: Map<Object, Key> = MapWrapper.create();
get(token: Object): Key {
if (token instanceof Key) return token;
@ -73,7 +65,7 @@ export class KeyRegistry {
return newKey;
}
get numberOfKeys() /* :int */ { return MapWrapper.size(this._allKeys); }
get numberOfKeys(): number { return MapWrapper.size(this._allKeys); }
}
var _globalKeyRegistry = new KeyRegistry();