refactor(ChangeDetector): pass formatters when instantiating a watch group

This commit is contained in:
vsavkin
2014-11-14 12:34:35 -08:00
parent 03410850b4
commit e15bcf0ffd
5 changed files with 32 additions and 29 deletions

View File

@ -1,6 +1,6 @@
import {ProtoWatchGroup, WatchGroup} from './watch_group';
import {FIELD, isPresent, int, StringWrapper, FunctionWrapper, BaseException} from 'facade/lang';
import {ListWrapper} from 'facade/collection';
import {ListWrapper, MapWrapper} from 'facade/collection';
import {ClosureMap} from 'change_detection/parser/closure_map';
var _fresh = new Object();
@ -8,6 +8,7 @@ var _fresh = new Object();
export const PROTO_RECORD_CONST = 'const';
export const PROTO_RECORD_PURE_FUNCTION = 'func';
export const PROTO_RECORD_CLOSURE = 'closure';
export const PROTO_RECORD_FORMATTTER = 'formatter';
export const PROTO_RECORD_METHOD = 'method';
export const PROTO_RECORD_PROPERTY = 'property';
@ -79,7 +80,7 @@ export class Record {
// Otherwise it is the context used by WatchGroupDispatcher.
@FIELD('dest')
constructor(watchGroup:WatchGroup, protoRecord:ProtoRecord) {
constructor(watchGroup:WatchGroup, protoRecord:ProtoRecord, formatters:Map) {
this.watchGroup = watchGroup;
this.protoRecord = protoRecord;
@ -105,6 +106,11 @@ export class Record {
this.funcOrValue = protoRecord.funcOrValue;
this.args = ListWrapper.createFixedSize(protoRecord.arity);
} else if (type === PROTO_RECORD_FORMATTTER) {
this.mode = MODE_STATE_INVOKE_PURE_FUNCTION;
this.funcOrValue = MapWrapper.get(formatters, protoRecord.funcOrValue);
this.args = ListWrapper.createFixedSize(protoRecord.arity);
} else if (type === PROTO_RECORD_METHOD) {
this.mode = MODE_STATE_INVOKE_METHOD;
this.funcOrValue = protoRecord.funcOrValue;

View File

@ -1,5 +1,5 @@
import {ProtoRecord, Record, PROTO_RECORD_CONST, PROTO_RECORD_PURE_FUNCTION,
PROTO_RECORD_PROPERTY, PROTO_RECORD_METHOD, PROTO_RECORD_CLOSURE} from './record';
PROTO_RECORD_PROPERTY, PROTO_RECORD_METHOD, PROTO_RECORD_CLOSURE, PROTO_RECORD_FORMATTTER} from './record';
import {FIELD, IMPLEMENTS, isBlank, isPresent, int, toBool, autoConvertAdd, BaseException} from 'facade/lang';
import {ListWrapper, MapWrapper} from 'facade/collection';
import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive,
@ -9,9 +9,7 @@ import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive,
export class ProtoWatchGroup {
@FIELD('headRecord:ProtoRecord')
@FIELD('tailRecord:ProtoRecord')
constructor(formatters=null) {
this.formatters = formatters;
constructor() {
this.headRecord = null;
this.tailRecord = null;
}
@ -47,25 +45,25 @@ export class ProtoWatchGroup {
// TODO(rado): the type annotation should be dispatcher:WatchGroupDispatcher.
// but @Implements is not ready yet.
instantiate(dispatcher):WatchGroup {
instantiate(dispatcher, formatters:Map):WatchGroup {
var watchGroup:WatchGroup = new WatchGroup(this, dispatcher);
if (this.headRecord !== null) {
this._createRecords(watchGroup);
this._createRecords(watchGroup, formatters);
this._setDestination();
}
return watchGroup;
}
_createRecords(watchGroup:WatchGroup) {
_createRecords(watchGroup:WatchGroup, formatters:Map) {
var tail, prevRecord;
watchGroup.headRecord = tail = new Record(watchGroup, this.headRecord);
watchGroup.headRecord = tail = new Record(watchGroup, this.headRecord, formatters);
this.headRecord.recordInConstruction = watchGroup.headRecord;
for (var proto = this.headRecord.next; proto != null; proto = proto.next) {
prevRecord = tail;
tail = new Record(watchGroup, proto);
tail = new Record(watchGroup, proto, formatters);
proto.recordInConstruction = tail;
tail.prev = prevRecord;
@ -178,8 +176,7 @@ class ProtoRecordCreator {
}
visitFormatter(ast:Formatter, dest) {
var formatter = this.protoWatchGroup.formatters[ast.name];
var record = this.construct(PROTO_RECORD_PURE_FUNCTION, formatter, ast.allArgs.length, dest);
var record = this.construct(PROTO_RECORD_FORMATTTER, ast.name, ast.allArgs.length, dest);
for (var i = 0; i < ast.allArgs.length; ++i) {
ast.allArgs[i].visit(this, new Destination(record, i));
}

View File

@ -22,11 +22,11 @@ export function main() {
}
function createChangeDetector(memo:string, exp:string, context = null, formatters = null) {
var pwg = new ProtoWatchGroup(formatters);
var pwg = new ProtoWatchGroup();
pwg.watch(ast(exp), memo, false);
var dispatcher = new LoggingDispatcher();
var wg = pwg.instantiate(dispatcher);
var wg = pwg.instantiate(dispatcher, formatters);
wg.setContext(context);
var cd = new ChangeDetector(wg);
@ -153,10 +153,10 @@ export function main() {
});
it("should support formatters", () => {
var formatters = {
"uppercase" : (v) => v.toUpperCase(),
"wrap" : (v, before, after) => `${before}${v}${after}`
};
var formatters = MapWrapper.createFromPairs([
["uppercase", (v) => v.toUpperCase()],
["wrap", (v, before, after) => `${before}${v}${after}`]
]);
expect(executeWatch('str', '"aBc" | uppercase', null, formatters)).toEqual(['str=ABC']);
expect(executeWatch('str', '"b" | wrap:"a":"c"', null, formatters)).toEqual(['str=abc']);
});