Skip to content

Commit 58ba700

Browse files
committed
feat(change_detection): change binding syntax to explicitly specify pipes
1 parent 69e02ee commit 58ba700

File tree

20 files changed

+236
-101
lines changed

20 files changed

+236
-101
lines changed

modules/angular2/change_detection.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export * from './src/change_detection/pipes/pipe';
1818
import {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector}
1919
from './src/change_detection/proto_change_detector';
2020
import {PipeRegistry} from './src/change_detection/pipes/pipe_registry';
21-
import {ArrayChanges} from './src/change_detection/pipes/array_changes';
22-
import {NullPipe} from './src/change_detection/pipes/null_pipe';
21+
import {ArrayChangesFactory} from './src/change_detection/pipes/array_changes';
22+
import {NullPipeFactory} from './src/change_detection/pipes/null_pipe';
2323

2424
export class ChangeDetection {
2525
createProtoChangeDetector(name:string):ProtoChangeDetector{
@@ -29,15 +29,9 @@ export class ChangeDetection {
2929
}
3030

3131
export var defaultPipes = {
32-
"[]" : [
33-
{
34-
"supports" : ArrayChanges.supportsObj,
35-
"pipe" : () => new ArrayChanges()
36-
},
37-
{
38-
"supports" : NullPipe.supportsObj,
39-
"pipe" : () => new NullPipe()
40-
}
32+
"iterableDiff" : [
33+
new ArrayChangesFactory(),
34+
new NullPipeFactory()
4135
]
4236
};
4337

modules/angular2/src/change_detection/change_detection_jit_generator.es6

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
RECORD_TYPE_PRIMITIVE_OP,
1616
RECORD_TYPE_KEYED_ACCESS,
1717
RECORD_TYPE_INVOKE_FORMATTER,
18-
RECORD_TYPE_STRUCTURAL_CHECK,
18+
RECORD_TYPE_PIPE,
1919
RECORD_TYPE_INTERPOLATE
2020
} from './proto_record';
2121

@@ -163,11 +163,11 @@ if (${CHANGES_LOCAL} && ${CHANGES_LOCAL}.length > 0) {
163163
`;
164164
}
165165

166-
function pipeCheckTemplate(context:string, pipe:string,
166+
function pipeCheckTemplate(context:string, pipe:string, pipeType:string,
167167
value:string, change:string, addRecord:string, notify:string):string{
168168
return `
169169
if (${pipe} === ${UTIL}.unitialized() || !${pipe}.supports(${context})) {
170-
${pipe} = ${PIPE_REGISTRY_ACCESSOR}.get('[]', ${context});
170+
${pipe} = ${PIPE_REGISTRY_ACCESSOR}.get('${pipeType}', ${context});
171171
}
172172
173173
${CHANGE_LOCAL} = ${pipe}.transform(${context});
@@ -283,7 +283,7 @@ export class ChangeDetectorJITGenerator {
283283
fields = fields.concat(this.fieldNames);
284284

285285
this.records.forEach((r) => {
286-
if (r.mode === RECORD_TYPE_STRUCTURAL_CHECK) {
286+
if (r.mode === RECORD_TYPE_PIPE) {
287287
fields.push(this.pipeNames[r.selfIndex]);
288288
}
289289
});
@@ -314,7 +314,7 @@ export class ChangeDetectorJITGenerator {
314314
}
315315

316316
genRecord(r:ProtoRecord):string {
317-
if (r.mode === RECORD_TYPE_STRUCTURAL_CHECK) {
317+
if (r.mode === RECORD_TYPE_PIPE) {
318318
return this.genPipeCheck (r);
319319
} else {
320320
return this.genReferenceCheck(r);
@@ -331,7 +331,7 @@ export class ChangeDetectorJITGenerator {
331331
var addRecord = addSimpleChangeRecordTemplate(r.selfIndex - 1, oldValue, newValue);
332332
var notify = this.genNotify(r);
333333

334-
return pipeCheckTemplate(context, pipe, newValue, change, addRecord, notify);
334+
return pipeCheckTemplate(context, pipe, r.name, newValue, change, addRecord, notify);
335335
}
336336

337337
genReferenceCheck(r:ProtoRecord):string {

modules/angular2/src/change_detection/dynamic_change_detector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
RECORD_TYPE_PRIMITIVE_OP,
1818
RECORD_TYPE_KEYED_ACCESS,
1919
RECORD_TYPE_INVOKE_FORMATTER,
20-
RECORD_TYPE_STRUCTURAL_CHECK,
20+
RECORD_TYPE_PIPE,
2121
RECORD_TYPE_INTERPOLATE
2222
} from './proto_record';
2323

@@ -81,7 +81,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
8181

8282
_check(proto:ProtoRecord) {
8383
try {
84-
if (proto.mode == RECORD_TYPE_STRUCTURAL_CHECK) {
84+
if (proto.mode == RECORD_TYPE_PIPE) {
8585
return this._pipeCheck(proto);
8686
} else {
8787
return this._referenceCheck(proto);
@@ -184,7 +184,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
184184
if (isPresent(storedPipe) && storedPipe.supports(context)) {
185185
return storedPipe;
186186
} else {
187-
var pipe = this.pipeRegistry.get("[]", context);
187+
var pipe = this.pipeRegistry.get(proto.name, context);
188188
this._writePipe(proto, pipe);
189189
return pipe;
190190
}

modules/angular2/src/change_detection/parser/ast.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@ export class EmptyExpr extends AST {
3333
}
3434
}
3535

36-
export class Structural extends AST {
37-
value:AST;
38-
constructor(value:AST) {
39-
super();
40-
this.value = value;
41-
}
42-
43-
eval(context) {
44-
return value.eval(context);
45-
}
46-
47-
visit(visitor) {
48-
return visitor.visitStructural(this);
49-
}
50-
}
51-
5236
export class ImplicitReceiver extends AST {
5337
eval(context) {
5438
return context;
@@ -204,6 +188,20 @@ export class Formatter extends AST {
204188
}
205189
}
206190

191+
export class Pipe extends AST {
192+
exp:AST;
193+
name:string;
194+
constructor(exp:AST, name:string) {
195+
super();
196+
this.exp = exp;
197+
this.name = name;
198+
}
199+
200+
visit(visitor) {
201+
return visitor.visitPipe(this);
202+
}
203+
}
204+
207205
export class LiteralPrimitive extends AST {
208206
value;
209207
constructor(value) {
@@ -460,9 +458,9 @@ export class AstVisitor {
460458
visitAssignment(ast:Assignment) {}
461459
visitBinary(ast:Binary) {}
462460
visitChain(ast:Chain){}
463-
visitStructural(ast:Structural) {}
464461
visitConditional(ast:Conditional) {}
465462
visitFormatter(ast:Formatter) {}
463+
visitPipe(ast:Pipe) {}
466464
visitFunctionCall(ast:FunctionCall) {}
467465
visitImplicitReceiver(ast:ImplicitReceiver) {}
468466
visitKeyedAccess(ast:KeyedAccess) {}

modules/angular2/src/change_detection/parser/parser.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
PrefixNot,
1515
Conditional,
1616
Formatter,
17+
Pipe,
1718
Assignment,
1819
Chain,
1920
KeyedAccess,
@@ -52,6 +53,15 @@ export class Parser {
5253
return new ASTWithSource(ast, input, location);
5354
}
5455

56+
addPipes(bindingAst:ASTWithSource, pipes:List<String>):ASTWithSource {
57+
if (ListWrapper.isEmpty(pipes)) return bindingAst;
58+
59+
var res = ListWrapper.reduce(pipes,
60+
(result, currentPipeName) => new Pipe(result, currentPipeName),
61+
bindingAst.ast);
62+
return new ASTWithSource(res, bindingAst.source, bindingAst.location);
63+
}
64+
5565
parseTemplateBindings(input:string, location:any):List<TemplateBinding> {
5666
var tokens = this._lexer.tokenize(input);
5767
return new _ParseAST(input, location, tokens, this._reflector, false).parseTemplateBindings();

modules/angular2/src/change_detection/pipes/array_changes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import {
1616

1717
import {NO_CHANGE, Pipe} from './pipe';
1818

19+
export class ArrayChangesFactory {
20+
supports(obj):boolean {
21+
return ArrayChanges.supportsObj(obj);
22+
}
23+
24+
create():Pipe {
25+
return new ArrayChanges();
26+
}
27+
}
28+
1929
export class ArrayChanges extends Pipe {
2030
_collection;
2131
_length:int;

modules/angular2/src/change_detection/pipes/null_pipe.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import {isBlank} from 'angular2/src/facade/lang';
22
import {Pipe, NO_CHANGE} from './pipe';
33

4+
export class NullPipeFactory {
5+
supports(obj):boolean {
6+
return NullPipe.supportsObj(obj);
7+
}
8+
9+
create():Pipe {
10+
return new NullPipe();
11+
}
12+
}
13+
414
export class NullPipe extends Pipe {
515
called:boolean;
616
constructor() {

modules/angular2/src/change_detection/pipes/pipe_registry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export class PipeRegistry {
1616
}
1717

1818
var matchingConfig = ListWrapper.find(listOfConfigs,
19-
(pipeConfig) => pipeConfig["supports"](obj));
19+
(pipeConfig) => pipeConfig.supports(obj));
2020

2121
if (isBlank(matchingConfig)) {
2222
throw new BaseException(`Cannot find a pipe for type '${type}' object '${obj}'`);
2323
}
2424

25-
return matchingConfig["pipe"]();
25+
return matchingConfig.create();
2626
}
2727
}

modules/angular2/src/change_detection/proto_change_detector.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
AstVisitor,
1010
Binary,
1111
Chain,
12-
Structural,
1312
Conditional,
1413
Formatter,
14+
Pipe,
1515
FunctionCall,
1616
ImplicitReceiver,
1717
Interpolation,
@@ -41,12 +41,12 @@ import {
4141
RECORD_TYPE_PRIMITIVE_OP,
4242
RECORD_TYPE_KEYED_ACCESS,
4343
RECORD_TYPE_INVOKE_FORMATTER,
44-
RECORD_TYPE_STRUCTURAL_CHECK,
44+
RECORD_TYPE_PIPE,
4545
RECORD_TYPE_INTERPOLATE
4646
} from './proto_record';
4747

4848
export class ProtoChangeDetector {
49-
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false){}
49+
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null){}
5050
instantiate(dispatcher:any, formatters:Map):ChangeDetector{
5151
return null;
5252
}
@@ -64,8 +64,8 @@ export class DynamicProtoChangeDetector extends ProtoChangeDetector {
6464
this._recordBuilder = new ProtoRecordBuilder();
6565
}
6666

67-
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
68-
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento, structural);
67+
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null) {
68+
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento);
6969
}
7070

7171
instantiate(dispatcher:any, formatters:Map) {
@@ -95,8 +95,8 @@ export class JitProtoChangeDetector extends ProtoChangeDetector {
9595
this._recordBuilder = new ProtoRecordBuilder();
9696
}
9797

98-
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
99-
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento, structural);
98+
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null) {
99+
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento);
100100
}
101101

102102
instantiate(dispatcher:any, formatters:Map) {
@@ -121,9 +121,7 @@ class ProtoRecordBuilder {
121121
this.records = [];
122122
}
123123

124-
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
125-
if (structural) ast = new Structural(ast);
126-
124+
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null) {
127125
var last = ListWrapper.last(this.records);
128126
if (isPresent(last) && last.directiveMemento == directiveMemento) {
129127
last.lastInDirective = false;
@@ -228,9 +226,9 @@ class _ConvertAstIntoProtoRecords {
228226
ChangeDetectionUtil.cond, [c,t,f], null, 0);
229227
}
230228

231-
visitStructural(ast:Structural) {
232-
var value = ast.value.visit(this);
233-
return this._addRecord(RECORD_TYPE_STRUCTURAL_CHECK, "structural", null, [], null, value);
229+
visitPipe(ast:Pipe) {
230+
var value = ast.exp.visit(this);
231+
return this._addRecord(RECORD_TYPE_PIPE, ast.name, ast.name, [], null, value);
234232
}
235233

236234
visitKeyedAccess(ast:KeyedAccess) {

modules/angular2/src/change_detection/proto_record.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const RECORD_TYPE_INVOKE_METHOD = 4;
88
export const RECORD_TYPE_INVOKE_CLOSURE = 5;
99
export const RECORD_TYPE_KEYED_ACCESS = 6;
1010
export const RECORD_TYPE_INVOKE_FORMATTER = 7;
11-
export const RECORD_TYPE_STRUCTURAL_CHECK = 8;
11+
export const RECORD_TYPE_PIPE = 8;
1212
export const RECORD_TYPE_INTERPOLATE = 9;
1313

1414
export class ProtoRecord {

0 commit comments

Comments
 (0)