style(ChangeDetection): idiomatic TS

This commit is contained in:
Victor Berchet 2015-06-16 08:47:24 +02:00
parent cdfb635737
commit 598a75ec1c
16 changed files with 128 additions and 228 deletions

View File

@ -5,41 +5,38 @@ import {ChangeDetector} from './interfaces';
import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED, ON_PUSH} from './constants';
export class AbstractChangeDetector extends ChangeDetector {
lightDomChildren: List<any>;
shadowDomChildren: List<any>;
lightDomChildren: List<any> = [];
shadowDomChildren: List<any> = [];
parent: ChangeDetector;
mode: string;
mode: string = null;
ref: ChangeDetectorRef;
constructor() {
super();
this.lightDomChildren = [];
this.shadowDomChildren = [];
this.ref = new ChangeDetectorRef(this);
this.mode = null;
}
addChild(cd: ChangeDetector) {
addChild(cd: ChangeDetector): void {
ListWrapper.push(this.lightDomChildren, cd);
cd.parent = this;
}
removeChild(cd: ChangeDetector) { ListWrapper.remove(this.lightDomChildren, cd); }
removeChild(cd: ChangeDetector): void { ListWrapper.remove(this.lightDomChildren, cd); }
addShadowDomChild(cd: ChangeDetector) {
addShadowDomChild(cd: ChangeDetector): void {
ListWrapper.push(this.shadowDomChildren, cd);
cd.parent = this;
}
removeShadowDomChild(cd: ChangeDetector) { ListWrapper.remove(this.shadowDomChildren, cd); }
removeShadowDomChild(cd: ChangeDetector): void { ListWrapper.remove(this.shadowDomChildren, cd); }
remove() { this.parent.removeChild(this); }
remove(): void { this.parent.removeChild(this); }
detectChanges() { this._detectChanges(false); }
detectChanges(): void { this._detectChanges(false); }
checkNoChanges() { this._detectChanges(true); }
checkNoChanges(): void { this._detectChanges(true); }
_detectChanges(throwOnChange: boolean) {
_detectChanges(throwOnChange: boolean): void {
if (this.mode === DETACHED || this.mode === CHECKED) return;
this.detectChangesInRecords(throwOnChange);
@ -53,26 +50,26 @@ export class AbstractChangeDetector extends ChangeDetector {
if (this.mode === CHECK_ONCE) this.mode = CHECKED;
}
detectChangesInRecords(throwOnChange: boolean) {}
callOnAllChangesDone() {}
detectChangesInRecords(throwOnChange: boolean): void {}
callOnAllChangesDone(): void {}
_detectChangesInLightDomChildren(throwOnChange: boolean) {
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
var c = this.lightDomChildren;
for (var i = 0; i < c.length; ++i) {
c[i]._detectChanges(throwOnChange);
}
}
_detectChangesInShadowDomChildren(throwOnChange: boolean) {
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
var c = this.shadowDomChildren;
for (var i = 0; i < c.length; ++i) {
c[i]._detectChanges(throwOnChange);
}
}
markAsCheckOnce() { this.mode = CHECK_ONCE; }
markAsCheckOnce(): void { this.mode = CHECK_ONCE; }
markPathToRootAsCheckOnce() {
markPathToRootAsCheckOnce(): void {
var c: ChangeDetector = this;
while (isPresent(c) && c.mode != DETACHED) {
if (c.mode === CHECKED) c.mode = CHECK_ONCE;

View File

@ -13,50 +13,53 @@ export class BindingRecord {
public elementIndex: number, public propertyName: string, public setter: SetterFn,
public lifecycleEvent: string, public directiveRecord: DirectiveRecord) {}
callOnChange() { return isPresent(this.directiveRecord) && this.directiveRecord.callOnChange; }
callOnChange(): boolean {
return isPresent(this.directiveRecord) && this.directiveRecord.callOnChange;
}
isOnPushChangeDetection() {
isOnPushChangeDetection(): boolean {
return isPresent(this.directiveRecord) && this.directiveRecord.isOnPushChangeDetection();
}
isDirective() { return this.mode === DIRECTIVE; }
isDirective(): boolean { return this.mode === DIRECTIVE; }
isDirectiveLifecycle() { return this.mode === DIRECTIVE_LIFECYCLE; }
isDirectiveLifecycle(): boolean { return this.mode === DIRECTIVE_LIFECYCLE; }
isElement() { return this.mode === ELEMENT; }
isElement(): boolean { return this.mode === ELEMENT; }
isTextNode() { return this.mode === TEXT_NODE; }
isTextNode(): boolean { return this.mode === TEXT_NODE; }
static createForDirective(ast: AST, propertyName: string, setter: SetterFn,
directiveRecord: DirectiveRecord) {
directiveRecord: DirectiveRecord): BindingRecord {
return new BindingRecord(DIRECTIVE, 0, ast, 0, propertyName, setter, null, directiveRecord);
}
static createDirectiveOnCheck(directiveRecord: DirectiveRecord) {
static createDirectiveOnCheck(directiveRecord: DirectiveRecord): BindingRecord {
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onCheck",
directiveRecord);
}
static createDirectiveOnInit(directiveRecord: DirectiveRecord) {
static createDirectiveOnInit(directiveRecord: DirectiveRecord): BindingRecord {
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onInit",
directiveRecord);
}
static createDirectiveOnChange(directiveRecord: DirectiveRecord) {
static createDirectiveOnChange(directiveRecord: DirectiveRecord): BindingRecord {
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onChange",
directiveRecord);
}
static createForElement(ast: AST, elementIndex: number, propertyName: string) {
static createForElement(ast: AST, elementIndex: number, propertyName: string): BindingRecord {
return new BindingRecord(ELEMENT, 0, ast, elementIndex, propertyName, null, null, null);
}
static createForHostProperty(directiveIndex: DirectiveIndex, ast: AST, propertyName: string) {
static createForHostProperty(directiveIndex: DirectiveIndex, ast: AST,
propertyName: string): BindingRecord {
return new BindingRecord(ELEMENT, directiveIndex, ast, directiveIndex.elementIndex,
propertyName, null, null, null);
}
static createForTextNode(ast: AST, elementIndex: number) {
static createForTextNode(ast: AST, elementIndex: number): BindingRecord {
return new BindingRecord(TEXT_NODE, 0, ast, elementIndex, null, null, null, null);
}
}
}

View File

@ -16,14 +16,14 @@ export class ChangeDetectorRef {
/**
* Request to check all ON_PUSH ancestors.
*/
requestCheck() { this._cd.markPathToRootAsCheckOnce(); }
requestCheck(): void { this._cd.markPathToRootAsCheckOnce(); }
/**
* Detaches the change detector from the change detector tree.
*
* The detached change detector will not be checked until it is reattached.
*/
detach() { this._cd.mode = DETACHED; }
detach(): void { this._cd.mode = DETACHED; }
/**
* Reattach the change detector to the change detector tree.
@ -32,7 +32,7 @@ export class ChangeDetectorRef {
*checked during the
* next change detection run.
*/
reattach() {
reattach(): void {
this._cd.mode = CHECK_ALWAYS;
this.requestCheck();
}

View File

@ -13,8 +13,8 @@ import {RecordType, ProtoRecord} from './proto_record';
* replaced with very cheap SELF records.
*/
export function coalesce(records: List<ProtoRecord>): List<ProtoRecord> {
var res = ListWrapper.create();
var indexMap = MapWrapper.create();
var res: List<ProtoRecord> = ListWrapper.create();
var indexMap: Map<number, number> = MapWrapper.create();
for (var i = 0; i < records.length; ++i) {
var r = records[i];

View File

@ -16,12 +16,10 @@ export class ExpressionChangedAfterItHasBeenChecked extends BaseException {
export class ChangeDetectionError extends BaseException {
message: string;
originalException: any;
location: string;
constructor(proto: ProtoRecord, originalException: any) {
constructor(proto: ProtoRecord, public originalException: any) {
super();
this.originalException = originalException;
this.location = proto.expressionAsString;
this.message = `${this.originalException} in [${this.location}]`;
}

View File

@ -47,17 +47,17 @@ export class ChangeDetector {
parent: ChangeDetector;
mode: string;
addChild(cd: ChangeDetector) {}
addShadowDomChild(cd: ChangeDetector) {}
removeChild(cd: ChangeDetector) {}
removeShadowDomChild(cd: ChangeDetector) {}
remove() {}
hydrate(context: any, locals: Locals, directives: any) {}
dehydrate() {}
markPathToRootAsCheckOnce() {}
addChild(cd: ChangeDetector): void {}
addShadowDomChild(cd: ChangeDetector): void {}
removeChild(cd: ChangeDetector): void {}
removeShadowDomChild(cd: ChangeDetector): void {}
remove(): void {}
hydrate(context: any, locals: Locals, directives: any): void {}
dehydrate(): void {}
markPathToRootAsCheckOnce(): void {}
detectChanges() {}
checkNoChanges() {}
detectChanges(): void {}
checkNoChanges(): void {}
}
export class ChangeDetectorDefinition {

View File

@ -8,12 +8,12 @@ import {
isPresent
} from "angular2/src/facade/lang";
export const TOKEN_TYPE_CHARACTER = 1;
export const TOKEN_TYPE_IDENTIFIER = 2;
export const TOKEN_TYPE_KEYWORD = 3;
export const TOKEN_TYPE_STRING = 4;
export const TOKEN_TYPE_OPERATOR = 5;
export const TOKEN_TYPE_NUMBER = 6;
const TOKEN_TYPE_CHARACTER = 1;
const TOKEN_TYPE_IDENTIFIER = 2;
const TOKEN_TYPE_KEYWORD = 3;
const TOKEN_TYPE_STRING = 4;
const TOKEN_TYPE_OPERATOR = 5;
const TOKEN_TYPE_NUMBER = 6;
@Injectable()
export class Lexer {
@ -160,24 +160,18 @@ const $NBSP = 160;
export class ScannerError extends BaseException {
message: string;
constructor(message) {
super();
this.message = message;
}
constructor(public message) { super(); }
toString() { return this.message; }
toString(): string { return this.message; }
}
class _Scanner {
length: number;
peek: number;
index: number;
peek: number = 0;
index: number = -1;
constructor(public input: string) {
this.length = input.length;
this.peek = 0;
this.index = -1;
this.advance();
}

View File

@ -55,10 +55,9 @@ var INTERPOLATION_REGEXP = RegExpWrapper.create('\\{\\{(.*?)\\}\\}');
@Injectable()
export class Parser {
_lexer: Lexer;
_reflector: Reflector;
constructor(lexer: Lexer, providedReflector: Reflector = null) {
this._lexer = lexer;
constructor(public _lexer: Lexer, providedReflector: Reflector = null) {
this._reflector = isPresent(providedReflector) ? providedReflector : reflector;
}
@ -116,11 +115,9 @@ export class Parser {
}
class _ParseAST {
index: int;
index: int = 0;
constructor(public input: string, public location: any, public tokens: List<any>,
public reflector: Reflector, public parseAction: boolean) {
this.index = 0;
}
public reflector: Reflector, public parseAction: boolean) {}
peek(offset: int): Token {
var i = this.index + offset;

View File

@ -30,39 +30,23 @@ export class IterableChangesFactory extends PipeFactory {
* @exportedAs angular2/pipes
*/
export class IterableChanges extends Pipe {
private _collection;
private _length: int;
private _linkedRecords: _DuplicateMap;
private _unlinkedRecords: _DuplicateMap;
private _previousItHead: CollectionChangeRecord;
private _itHead: CollectionChangeRecord;
private _itTail: CollectionChangeRecord;
private _additionsHead: CollectionChangeRecord;
private _additionsTail: CollectionChangeRecord;
private _movesHead: CollectionChangeRecord;
private _movesTail: CollectionChangeRecord;
private _removalsHead: CollectionChangeRecord;
private _removalsTail: CollectionChangeRecord;
private _collection = null;
private _length: int = null;
// Keeps track of the used records at any point in time (during & across `_check()` calls)
private _linkedRecords: _DuplicateMap = null;
// Keeps track of the removed records at any point in time during `_check()` calls.
private _unlinkedRecords: _DuplicateMap = null;
private _previousItHead: CollectionChangeRecord = null;
private _itHead: CollectionChangeRecord = null;
private _itTail: CollectionChangeRecord = null;
private _additionsHead: CollectionChangeRecord = null;
private _additionsTail: CollectionChangeRecord = null;
private _movesHead: CollectionChangeRecord = null;
private _movesTail: CollectionChangeRecord = null;
private _removalsHead: CollectionChangeRecord = null;
private _removalsTail: CollectionChangeRecord = null;
constructor() {
super();
this._collection = null;
this._length = null;
/// Keeps track of the used records at any point in time (during & across `_check()` calls)
this._linkedRecords = null;
/// Keeps track of the removed records at any point in time during `_check()` calls.
this._unlinkedRecords = null;
this._previousItHead = null;
this._itHead = null;
this._itTail = null;
this._additionsHead = null;
this._additionsTail = null;
this._movesHead = null;
this._movesTail = null;
this._removalsHead = null;
this._removalsTail = null;
}
constructor() { super(); }
static supportsObj(obj): boolean { return isListLikeIterable(obj); }
@ -496,35 +480,20 @@ export class IterableChanges extends Pipe {
* @exportedAs angular2/pipes
*/
export class CollectionChangeRecord {
currentIndex: int;
previousIndex: int;
item;
currentIndex: int = null;
previousIndex: int = null;
_nextPrevious: CollectionChangeRecord;
_prev: CollectionChangeRecord;
_next: CollectionChangeRecord;
_prevDup: CollectionChangeRecord;
_nextDup: CollectionChangeRecord;
_prevRemoved: CollectionChangeRecord;
_nextRemoved: CollectionChangeRecord;
_nextAdded: CollectionChangeRecord;
_nextMoved: CollectionChangeRecord;
_nextPrevious: CollectionChangeRecord = null;
_prev: CollectionChangeRecord = null;
_next: CollectionChangeRecord = null;
_prevDup: CollectionChangeRecord = null;
_nextDup: CollectionChangeRecord = null;
_prevRemoved: CollectionChangeRecord = null;
_nextRemoved: CollectionChangeRecord = null;
_nextAdded: CollectionChangeRecord = null;
_nextMoved: CollectionChangeRecord = null;
constructor(item) {
this.currentIndex = null;
this.previousIndex = null;
this.item = item;
this._nextPrevious = null;
this._prev = null;
this._next = null;
this._prevDup = null;
this._nextDup = null;
this._prevRemoved = null;
this._nextRemoved = null;
this._nextAdded = null;
this._nextMoved = null;
}
constructor(public item: any) {}
toString(): string {
return this.previousIndex === this.currentIndex ?
@ -536,20 +505,15 @@ export class CollectionChangeRecord {
// A linked list of CollectionChangeRecords with the same CollectionChangeRecord.item
class _DuplicateItemRecordList {
_head: CollectionChangeRecord;
_tail: CollectionChangeRecord;
constructor() {
this._head = null;
this._tail = null;
}
_head: CollectionChangeRecord = null;
_tail: CollectionChangeRecord = null;
/**
* Append the record to the list of duplicates.
*
* Note: by design all records in the list of duplicates hold the same value in record.item.
*/
add(record: CollectionChangeRecord) {
add(record: CollectionChangeRecord): void {
if (this._head === null) {
this._head = this._tail = record;
record._nextDup = null;
@ -610,8 +574,7 @@ class _DuplicateItemRecordList {
}
class _DuplicateMap {
map: Map<any, _DuplicateItemRecordList>;
constructor() { this.map = MapWrapper.create(); }
map: Map<any, _DuplicateItemRecordList> = MapWrapper.create();
put(record: CollectionChangeRecord) {
// todo(vicb) handle corner cases

View File

@ -19,28 +19,15 @@ export class KeyValueChangesFactory extends PipeFactory {
* @exportedAs angular2/pipes
*/
export class KeyValueChanges extends Pipe {
private _records: Map<any, any>;
private _mapHead: KVChangeRecord;
private _previousMapHead: KVChangeRecord;
private _changesHead: KVChangeRecord;
private _changesTail: KVChangeRecord;
private _additionsHead: KVChangeRecord;
private _additionsTail: KVChangeRecord;
private _removalsHead: KVChangeRecord;
private _removalsTail: KVChangeRecord;
constructor() {
super();
this._records = MapWrapper.create();
this._mapHead = null;
this._previousMapHead = null;
this._changesHead = null;
this._changesTail = null;
this._additionsHead = null;
this._additionsTail = null;
this._removalsHead = null;
this._removalsTail = null;
}
private _records: Map<any, any> = MapWrapper.create();
private _mapHead: KVChangeRecord = null;
private _previousMapHead: KVChangeRecord = null;
private _changesHead: KVChangeRecord = null;
private _changesTail: KVChangeRecord = null;
private _additionsHead: KVChangeRecord = null;
private _additionsTail: KVChangeRecord = null;
private _removalsHead: KVChangeRecord = null;
private _removalsTail: KVChangeRecord = null;
static supportsObj(obj): boolean { return obj instanceof Map || isJsObject(obj); }
@ -347,29 +334,17 @@ export class KeyValueChanges extends Pipe {
* @exportedAs angular2/pipes
*/
export class KVChangeRecord {
key;
previousValue;
currentValue;
previousValue: any = null;
currentValue: any = null;
_nextPrevious: KVChangeRecord;
_next: KVChangeRecord;
_nextAdded: KVChangeRecord;
_nextRemoved: KVChangeRecord;
_prevRemoved: KVChangeRecord;
_nextChanged: KVChangeRecord;
_nextPrevious: KVChangeRecord = null;
_next: KVChangeRecord = null;
_nextAdded: KVChangeRecord = null;
_nextRemoved: KVChangeRecord = null;
_prevRemoved: KVChangeRecord = null;
_nextChanged: KVChangeRecord = null;
constructor(key) {
this.key = key;
this.previousValue = null;
this.currentValue = null;
this._nextPrevious = null;
this._next = null;
this._nextAdded = null;
this._nextRemoved = null;
this._prevRemoved = null;
this._nextChanged = null;
}
constructor(public key: any) {}
toString(): string {
return looseIdentical(this.previousValue, this.currentValue) ?

View File

@ -24,11 +24,8 @@ import {Pipe} from './pipe';
* @exportedAs angular2/pipes
*/
export class LowerCasePipe extends Pipe {
_latestValue: string;
constructor() {
super();
this._latestValue = null;
}
_latestValue: string = null;
supports(str): boolean { return isString(str); }
onDestroy(): void { this._latestValue = null; }

View File

@ -17,11 +17,7 @@ export class NullPipeFactory extends PipeFactory {
* @exportedAs angular2/pipes
*/
export class NullPipe extends Pipe {
called: boolean;
constructor() {
super();
this.called = false;
}
called: boolean = false;
static supportsObj(obj): boolean { return isBlank(obj); }

View File

@ -30,22 +30,13 @@ import {ChangeDetectorRef} from '../change_detector_ref';
* @exportedAs angular2/pipes
*/
export class ObservablePipe extends Pipe {
_ref: ChangeDetectorRef;
_latestValue: Object = null;
_latestReturnedValue: Object = null;
_latestValue: Object;
_latestReturnedValue: Object;
_subscription: Object = null;
_observable: Observable = null;
_subscription: Object;
_observable: Observable;
constructor(ref: ChangeDetectorRef) {
super();
this._ref = ref;
this._latestValue = null;
this._latestReturnedValue = null;
this._subscription = null;
this._observable = null;
}
constructor(public _ref: ChangeDetectorRef) { super(); }
supports(obs): boolean { return ObservableWrapper.isObservable(obs); }
@ -106,4 +97,4 @@ export class ObservablePipeFactory extends PipeFactory {
supports(obs): boolean { return ObservableWrapper.isObservable(obs); }
create(cdRef): Pipe { return new ObservablePipe(cdRef); }
}
}

View File

@ -29,17 +29,11 @@ import {ChangeDetectorRef} from '../change_detector_ref';
* @exportedAs angular2/pipes
*/
export class PromisePipe extends Pipe {
_ref: ChangeDetectorRef;
_latestValue: Object;
_latestReturnedValue: Object;
_latestValue: Object = null;
_latestReturnedValue: Object = null;
_sourcePromise: Promise<any>;
constructor(ref: ChangeDetectorRef) {
super();
this._ref = ref;
this._latestValue = null;
this._latestReturnedValue = null;
}
constructor(public _ref: ChangeDetectorRef) { super(); }
supports(promise): boolean { return isPromise(promise); }

View File

@ -24,11 +24,8 @@ import {Pipe} from './pipe';
* @exportedAs angular2/pipes
*/
export class UpperCasePipe extends Pipe {
_latestValue: string;
constructor() {
super();
this._latestValue = null;
}
_latestValue: string = null;
supports(str): boolean { return isString(str); }
onDestroy(): void { this._latestValue = null; }

View File

@ -6,8 +6,6 @@ import {coalesce} from './coalesce';
export {Function as PregenProtoChangeDetectorFactory};
export class PregenProtoChangeDetector extends ProtoChangeDetector {
constructor() { super(); }
static isSupported(): boolean { return false; }
instantiate(dispatcher: any): ChangeDetector {