refactor(core): removed getter and changed to readonly variable (#19117)

PR Close #19117
This commit is contained in:
Yuan Gao 2017-09-08 18:06:33 -07:00 committed by Igor Minar
parent 549f2254b4
commit b14c2d1568
9 changed files with 62 additions and 77 deletions

View File

@ -27,11 +27,11 @@ export class ApplicationInitStatus {
private resolve: Function; private resolve: Function;
private reject: Function; private reject: Function;
private initialized = false; private initialized = false;
private _donePromise: Promise<any>; public readonly donePromise: Promise<any>;
private _done = false; public readonly done = false;
constructor(@Inject(APP_INITIALIZER) @Optional() private appInits: (() => any)[]) { constructor(@Inject(APP_INITIALIZER) @Optional() private appInits: (() => any)[]) {
this._donePromise = new Promise((res, rej) => { this.donePromise = new Promise((res, rej) => {
this.resolve = res; this.resolve = res;
this.reject = rej; this.reject = rej;
}); });
@ -46,7 +46,7 @@ export class ApplicationInitStatus {
const asyncInitPromises: Promise<any>[] = []; const asyncInitPromises: Promise<any>[] = [];
const complete = () => { const complete = () => {
this._done = true; (this as{done: boolean}).done = true;
this.resolve(); this.resolve();
}; };
@ -66,8 +66,4 @@ export class ApplicationInitStatus {
} }
this.initialized = true; this.initialized = true;
} }
get done(): boolean { return this._done; }
get donePromise(): Promise<any> { return this._donePromise; }
} }

View File

@ -335,14 +335,27 @@ export class ApplicationRef {
/** @internal */ /** @internal */
static _tickScope: WtfScopeFn = wtfCreateScope('ApplicationRef#tick()'); static _tickScope: WtfScopeFn = wtfCreateScope('ApplicationRef#tick()');
private _bootstrapListeners: ((compRef: ComponentRef<any>) => void)[] = []; private _bootstrapListeners: ((compRef: ComponentRef<any>) => void)[] = [];
private _rootComponents: ComponentRef<any>[] = [];
private _rootComponentTypes: Type<any>[] = [];
private _views: InternalViewRef[] = []; private _views: InternalViewRef[] = [];
private _runningTick: boolean = false; private _runningTick: boolean = false;
private _enforceNoNewChanges: boolean = false; private _enforceNoNewChanges: boolean = false;
private _isStable: Observable<boolean>;
private _stable = true; private _stable = true;
/**
* Get a list of component types registered to this application.
* This list is populated even before the component is created.
*/
public readonly componentTypes: Type<any>[] = [];
/**
* Get a list of components registered to this application.
*/
public readonly components: ComponentRef<any>[] = [];
/**
* Returns an Observable that indicates when the application is stable or unstable.
*/
public readonly isStable: Observable<boolean>;
/** @internal */ /** @internal */
constructor( constructor(
private _zone: NgZone, private _console: Console, private _injector: Injector, private _zone: NgZone, private _console: Console, private _injector: Injector,
@ -397,7 +410,8 @@ export class ApplicationRef {
}; };
}); });
this._isStable = merge(isCurrentlyStable, share.call(isStable)); (this as{isStable: Observable<boolean>}).isStable =
merge(isCurrentlyStable, share.call(isStable));
} }
/** /**
@ -428,7 +442,7 @@ export class ApplicationRef {
componentFactory = componentFactory =
this._componentFactoryResolver.resolveComponentFactory(componentOrFactory) !; this._componentFactoryResolver.resolveComponentFactory(componentOrFactory) !;
} }
this._rootComponentTypes.push(componentFactory.componentType); this.componentTypes.push(componentFactory.componentType);
// Create a factory associated with the current module if it's not bound to some other // Create a factory associated with the current module if it's not bound to some other
const ngModule = componentFactory instanceof ComponentFactoryBoundToModule ? const ngModule = componentFactory instanceof ComponentFactoryBoundToModule ?
@ -483,17 +497,6 @@ export class ApplicationRef {
} }
} }
/**
* Get a list of component types registered to this application.
* This list is populated even before the component is created.
*/
get componentTypes(): Type<any>[] { return this._rootComponentTypes; }
/**
* Get a list of components registered to this application.
*/
get components(): ComponentRef<any>[] { return this._rootComponents; }
/** /**
* Attaches a view so that it will be dirty checked. * Attaches a view so that it will be dirty checked.
* The view will be automatically detached when it is destroyed. * The view will be automatically detached when it is destroyed.
@ -517,7 +520,7 @@ export class ApplicationRef {
private _loadComponent(componentRef: ComponentRef<any>): void { private _loadComponent(componentRef: ComponentRef<any>): void {
this.attachView(componentRef.hostView); this.attachView(componentRef.hostView);
this.tick(); this.tick();
this._rootComponents.push(componentRef); this.components.push(componentRef);
// Get the listeners lazily to prevent DI cycles. // Get the listeners lazily to prevent DI cycles.
const listeners = const listeners =
this._injector.get(APP_BOOTSTRAP_LISTENER, []).concat(this._bootstrapListeners); this._injector.get(APP_BOOTSTRAP_LISTENER, []).concat(this._bootstrapListeners);
@ -526,7 +529,7 @@ export class ApplicationRef {
private _unloadComponent(componentRef: ComponentRef<any>): void { private _unloadComponent(componentRef: ComponentRef<any>): void {
this.detachView(componentRef.hostView); this.detachView(componentRef.hostView);
remove(this._rootComponents, componentRef); remove(this.components, componentRef);
} }
/** @internal */ /** @internal */
@ -539,11 +542,6 @@ export class ApplicationRef {
* Returns the number of attached views. * Returns the number of attached views.
*/ */
get viewCount() { return this._views.length; } get viewCount() { return this._views.length; }
/**
* Returns an Observable that indicates when the application is stable or unstable.
*/
get isStable(): Observable<boolean> { return this._isStable; }
} }
function remove<T>(list: T[], el: T): void { function remove<T>(list: T[], el: T): void {

View File

@ -26,8 +26,8 @@ const trackByIdentity = (index: number, item: any) => item;
* @deprecated v4.0.0 - Should not be part of public API. * @deprecated v4.0.0 - Should not be part of public API.
*/ */
export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChanges<V> { export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChanges<V> {
private _length: number = 0; public readonly length: number = 0;
private _collection: NgIterable<V>|null = null; public readonly collection: V[]|Iterable<V>|null;
// Keeps track of the used records at any point in time (during & across `_check()` calls) // Keeps track of the used records at any point in time (during & across `_check()` calls)
private _linkedRecords: _DuplicateMap<V>|null = null; private _linkedRecords: _DuplicateMap<V>|null = null;
// Keeps track of the removed records at any point in time during `_check()` calls. // Keeps track of the removed records at any point in time during `_check()` calls.
@ -48,10 +48,6 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
constructor(trackByFn?: TrackByFunction<V>) { this._trackByFn = trackByFn || trackByIdentity; } constructor(trackByFn?: TrackByFunction<V>) { this._trackByFn = trackByFn || trackByIdentity; }
get collection() { return this._collection; }
get length(): number { return this._length; }
forEachItem(fn: (record: IterableChangeRecord_<V>) => void) { forEachItem(fn: (record: IterableChangeRecord_<V>) => void) {
let record: IterableChangeRecord_<V>|null; let record: IterableChangeRecord_<V>|null;
for (record = this._itHead; record !== null; record = record._next) { for (record = this._itHead; record !== null; record = record._next) {
@ -171,9 +167,9 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
let item: V; let item: V;
let itemTrackBy: any; let itemTrackBy: any;
if (Array.isArray(collection)) { if (Array.isArray(collection)) {
this._length = collection.length; (this as{length: number}).length = collection.length;
for (let index = 0; index < this._length; index++) { for (let index = 0; index < this.length; index++) {
item = collection[index]; item = collection[index];
itemTrackBy = this._trackByFn(index, item); itemTrackBy = this._trackByFn(index, item);
if (record === null || !looseIdentical(record.trackById, itemTrackBy)) { if (record === null || !looseIdentical(record.trackById, itemTrackBy)) {
@ -206,11 +202,11 @@ export class DefaultIterableDiffer<V> implements IterableDiffer<V>, IterableChan
record = record._next; record = record._next;
index++; index++;
}); });
this._length = index; (this as{length: number}).length = index;
} }
this._truncate(record); this._truncate(record);
this._collection = collection; (this as{collection: V[] | Iterable<V>}).collection = collection;
return this.isDirty; return this.isDirty;
} }

View File

@ -29,10 +29,10 @@
* @stable * @stable
*/ */
export class InjectionToken<T> { export class InjectionToken<T> {
/** @internal */
readonly ngMetadataName = 'InjectionToken';
constructor(protected _desc: string) {} constructor(protected _desc: string) {}
toString(): string { return `InjectionToken ${this._desc}`; } toString(): string { return `InjectionToken ${this._desc}`; }
/** @internal */
get ngMetadataName() { return 'InjectionToken'; }
} }

View File

@ -282,8 +282,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
_constructionCounter: number = 0; _constructionCounter: number = 0;
/** @internal */ /** @internal */
public _providers: ResolvedReflectiveProvider[]; public _providers: ResolvedReflectiveProvider[];
/** @internal */ public readonly parent: Injector|null;
public _parent: Injector|null;
keyIds: number[]; keyIds: number[];
objs: any[]; objs: any[];
@ -292,7 +291,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
*/ */
constructor(_providers: ResolvedReflectiveProvider[], _parent?: Injector) { constructor(_providers: ResolvedReflectiveProvider[], _parent?: Injector) {
this._providers = _providers; this._providers = _providers;
this._parent = _parent || null; this.parent = _parent || null;
const len = _providers.length; const len = _providers.length;
@ -309,8 +308,6 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
return this._getByKey(ReflectiveKey.get(token), null, notFoundValue); return this._getByKey(ReflectiveKey.get(token), null, notFoundValue);
} }
get parent(): Injector|null { return this._parent; }
resolveAndCreateChild(providers: Provider[]): ReflectiveInjector { resolveAndCreateChild(providers: Provider[]): ReflectiveInjector {
const ResolvedReflectiveProviders = ReflectiveInjector.resolve(providers); const ResolvedReflectiveProviders = ReflectiveInjector.resolve(providers);
return this.createChildFromResolved(ResolvedReflectiveProviders); return this.createChildFromResolved(ResolvedReflectiveProviders);
@ -318,7 +315,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector { createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector {
const inj = new ReflectiveInjector_(providers); const inj = new ReflectiveInjector_(providers);
inj._parent = this; (inj as{parent: Injector | null}).parent = this;
return inj; return inj;
} }
@ -436,7 +433,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
let inj: Injector|null; let inj: Injector|null;
if (visibility instanceof SkipSelf) { if (visibility instanceof SkipSelf) {
inj = this._parent; inj = this.parent;
} else { } else {
inj = this; inj = this;
} }
@ -445,7 +442,7 @@ export class ReflectiveInjector_ implements ReflectiveInjector {
const inj_ = <ReflectiveInjector_>inj; const inj_ = <ReflectiveInjector_>inj;
const obj = inj_._getObjByKeyId(key.id); const obj = inj_._getObjByKeyId(key.id);
if (obj !== UNDEFINED) return obj; if (obj !== UNDEFINED) return obj;
inj = inj_._parent; inj = inj_.parent;
} }
if (inj !== null) { if (inj !== null) {
return inj.get(key.token, notFoundValue); return inj.get(key.token, notFoundValue);

View File

@ -27,6 +27,7 @@ import {resolveForwardRef} from './forward_ref';
* @deprecated No replacement * @deprecated No replacement
*/ */
export class ReflectiveKey { export class ReflectiveKey {
public readonly displayName: string;
/** /**
* Private * Private
*/ */
@ -34,13 +35,9 @@ export class ReflectiveKey {
if (!token) { if (!token) {
throw new Error('Token must be defined!'); throw new Error('Token must be defined!');
} }
this.displayName = stringify(this.token);
} }
/**
* Returns a stringified token.
*/
get displayName(): string { return stringify(this.token); }
/** /**
* Retrieves a `Key` for a token. * Retrieves a `Key` for a token.
*/ */

View File

@ -37,11 +37,10 @@ import {getSymbolIterator} from '../util';
* @stable * @stable
*/ */
export class QueryList<T>/* implements Iterable<T> */ { export class QueryList<T>/* implements Iterable<T> */ {
private _dirty = true; public readonly dirty = true;
private _results: Array<T> = []; private _results: Array<T> = [];
private _emitter = new EventEmitter(); public readonly changes: Observable<any> = new EventEmitter();
get changes(): Observable<any> { return this._emitter; }
get length(): number { return this._results.length; } get length(): number { return this._results.length; }
get first(): T { return this._results[0]; } get first(): T { return this._results[0]; }
get last(): T { return this._results[this.length - 1]; } get last(): T { return this._results[this.length - 1]; }
@ -98,21 +97,18 @@ export class QueryList<T>/* implements Iterable<T> */ {
reset(res: Array<T|any[]>): void { reset(res: Array<T|any[]>): void {
this._results = flatten(res); this._results = flatten(res);
this._dirty = false; (this as{dirty: boolean}).dirty = false;
} }
notifyOnChanges(): void { this._emitter.emit(this); } notifyOnChanges(): void { (this.changes as EventEmitter<any>).emit(this); }
/** internal */ /** internal */
setDirty() { this._dirty = true; } setDirty() { (this as{dirty: boolean}).dirty = true; }
/** internal */
get dirty() { return this._dirty; }
/** internal */ /** internal */
destroy(): void { destroy(): void {
this._emitter.complete(); (this.changes as EventEmitter<any>).complete();
this._emitter.unsubscribe(); (this.changes as EventEmitter<any>).unsubscribe();
} }
} }

View File

@ -12,13 +12,15 @@
* @stable * @stable
*/ */
export class Version { export class Version {
constructor(public full: string) {} public readonly major: string;
public readonly minor: string;
public readonly patch: string;
get major(): string { return this.full.split('.')[0]; } constructor(public full: string) {
this.major = full.split('.')[0];
get minor(): string { return this.full.split('.')[1]; } this.minor = full.split('.')[1];
this.patch = full.split('.').slice(2).join('.');
get patch(): string { return this.full.split('.').slice(2).join('.'); } }
} }
/** /**

View File

@ -101,18 +101,21 @@ class ComponentFactory_ extends ComponentFactory<any> {
} }
class ComponentRef_ extends ComponentRef<any> { class ComponentRef_ extends ComponentRef<any> {
public readonly hostView: ViewRef;
public readonly instance: any;
public readonly changeDetectorRef: ChangeDetectorRef;
private _elDef: NodeDef; private _elDef: NodeDef;
constructor(private _view: ViewData, private _viewRef: ViewRef, private _component: any) { constructor(private _view: ViewData, private _viewRef: ViewRef, private _component: any) {
super(); super();
this._elDef = this._view.def.nodes[0]; this._elDef = this._view.def.nodes[0];
this.hostView = _viewRef;
this.changeDetectorRef = _viewRef;
this.instance = _component;
} }
get location(): ElementRef { get location(): ElementRef {
return new ElementRef(asElementData(this._view, this._elDef.index).renderElement); return new ElementRef(asElementData(this._view, this._elDef.index).renderElement);
} }
get injector(): Injector { return new Injector_(this._view, this._elDef); } get injector(): Injector { return new Injector_(this._view, this._elDef); }
get instance(): any { return this._component; };
get hostView(): ViewRef { return this._viewRef; };
get changeDetectorRef(): ChangeDetectorRef { return this._viewRef; };
get componentType(): Type<any> { return <any>this._component.constructor; } get componentType(): Type<any> { return <any>this._component.constructor; }
destroy(): void { this._viewRef.destroy(); } destroy(): void { this._viewRef.destroy(); }