fix(change_detection): do not coalesce records with different directive indices

This commit is contained in:
vsavkin 2015-07-06 11:50:40 -07:00
parent 8681f79182
commit d2774421e8
2 changed files with 44 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import {isPresent} from 'angular2/src/facade/lang'; import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {List, ListWrapper, Map} from 'angular2/src/facade/collection'; import {List, ListWrapper, Map} from 'angular2/src/facade/collection';
import {RecordType, ProtoRecord} from './proto_record'; import {RecordType, ProtoRecord} from './proto_record';
@ -45,11 +45,22 @@ function _selfRecord(r: ProtoRecord, contextIndex: number, selfIndex: number): P
function _findMatching(r: ProtoRecord, rs: List<ProtoRecord>) { function _findMatching(r: ProtoRecord, rs: List<ProtoRecord>) {
return ListWrapper.find(rs, (rr) => rr.mode !== RecordType.DIRECTIVE_LIFECYCLE && return ListWrapper.find(rs, (rr) => rr.mode !== RecordType.DIRECTIVE_LIFECYCLE &&
rr.mode === r.mode && rr.funcOrValue === r.funcOrValue && _sameDirIndex(rr, r) && rr.mode === r.mode &&
rr.funcOrValue === r.funcOrValue &&
rr.contextIndex === r.contextIndex && rr.name === r.name && rr.contextIndex === r.contextIndex && rr.name === r.name &&
ListWrapper.equals(rr.args, r.args)); ListWrapper.equals(rr.args, r.args));
} }
function _sameDirIndex(a: ProtoRecord, b: ProtoRecord): boolean {
var di1 = isBlank(a.directiveIndex) ? null : a.directiveIndex.directiveIndex;
var ei1 = isBlank(a.directiveIndex) ? null : a.directiveIndex.elementIndex;
var di2 = isBlank(b.directiveIndex) ? null : b.directiveIndex.directiveIndex;
var ei2 = isBlank(b.directiveIndex) ? null : b.directiveIndex.elementIndex;
return di1 === di2 && ei1 === ei2;
}
function _replaceIndices(r: ProtoRecord, selfIndex: number, indexMap: Map<any, any>) { function _replaceIndices(r: ProtoRecord, selfIndex: number, indexMap: Map<any, any>) {
var args = ListWrapper.map(r.args, (a) => _map(indexMap, a)); var args = ListWrapper.map(r.args, (a) => _map(indexMap, a));
var contextIndex = _map(indexMap, r.contextIndex); var contextIndex = _map(indexMap, r.contextIndex);

View File

@ -1,13 +1,21 @@
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib'; import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
import {isBlank} from 'angular2/src/facade/lang';
import {coalesce} from 'angular2/src/change_detection/coalesce'; import {coalesce} from 'angular2/src/change_detection/coalesce';
import {RecordType, ProtoRecord} from 'angular2/src/change_detection/proto_record'; import {RecordType, ProtoRecord} from 'angular2/src/change_detection/proto_record';
import {DirectiveIndex} from 'angular2/src/change_detection/directive_record';
export function main() { export function main() {
function r(funcOrValue, args, contextIndex, selfIndex, lastInBinding = false, function r(
mode = RecordType.PROPERTY, name = "name") { funcOrValue, args, contextIndex, selfIndex,
return new ProtoRecord(mode, name, funcOrValue, args, null, contextIndex, null, selfIndex, null, {lastInBinding, mode, name,
null, lastInBinding, false); directiveIndex}: {lastInBinding?: any, mode?: any, name?: any, directiveIndex?: any} = {}) {
if (isBlank(lastInBinding)) lastInBinding = false;
if (isBlank(mode)) mode = RecordType.PROPERTY;
if (isBlank(name)) name = "name";
if (isBlank(directiveIndex)) directiveIndex = null;
return new ProtoRecord(mode, name, funcOrValue, args, null, contextIndex, directiveIndex,
selfIndex, null, null, lastInBinding, false);
} }
describe("change detection - coalesce", () => { describe("change detection - coalesce", () => {
@ -44,9 +52,9 @@ export function main() {
[r("user1", [], 0, 1), r("user2", [], 0, 2), r("hi", [1], 0, 3), r("hi", [2], 0, 4)]); [r("user1", [], 0, 1), r("user2", [], 0, 2), r("hi", [1], 0, 3), r("hi", [2], 0, 4)]);
}); });
it("should replace duplicate terminal records with" + " self records", () => { it("should replace duplicate terminal records with self records", () => {
var rs = coalesce(
var rs = coalesce([r("user", [], 0, 1, true), r("user", [], 0, 2, true)]); [r("user", [], 0, 1, {lastInBinding: true}), r("user", [], 0, 2, {lastInBinding: true})]);
expect(rs[1]).toEqual(new ProtoRecord(RecordType.SELF, "self", null, [], null, 1, null, 2, expect(rs[1]).toEqual(new ProtoRecord(RecordType.SELF, "self", null, [], null, 1, null, 2,
null, null, true, false)); null, null, true, false));
@ -54,8 +62,8 @@ export function main() {
it("should not coalesce directive lifecycle records", () => { it("should not coalesce directive lifecycle records", () => {
var rs = coalesce([ var rs = coalesce([
r("onCheck", [], 0, 1, true, RecordType.DIRECTIVE_LIFECYCLE), r("onCheck", [], 0, 1, {mode: RecordType.DIRECTIVE_LIFECYCLE}),
r("onCheck", [], 0, 1, true, RecordType.DIRECTIVE_LIFECYCLE) r("onCheck", [], 0, 1, {mode: RecordType.DIRECTIVE_LIFECYCLE})
]); ]);
expect(rs.length).toEqual(2); expect(rs.length).toEqual(2);
@ -64,10 +72,22 @@ export function main() {
it("should not coalesce protos with different names but same value", () => { it("should not coalesce protos with different names but same value", () => {
var nullFunc = () => {}; var nullFunc = () => {};
var rs = coalesce([ var rs = coalesce([
r(nullFunc, [], 0, 1, false, RecordType.PROPERTY, "foo"), r(nullFunc, [], 0, 1, {name: "foo"}),
r(nullFunc, [], 0, 1, false, RecordType.PROPERTY, "bar"), r(nullFunc, [], 0, 1, {name: "bar"}),
]); ]);
expect(rs.length).toEqual(2); expect(rs.length).toEqual(2);
}); });
it("should not coalesce protos with the same context index but different directive indices",
() => {
var nullFunc = () => {};
var rs = coalesce([
r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(0, 0)}),
r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(0, 1)}),
r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(1, 0)}),
r(nullFunc, [], 0, 1, {directiveIndex: null}),
]);
expect(rs.length).toEqual(4);
});
}); });
} }