X Tutup
Skip to content

Commit 1316c3e

Browse files
committed
fix(ChangeDetector): support for NaN
Closes #4853
1 parent 7580628 commit 1316c3e

File tree

8 files changed

+43
-26
lines changed

8 files changed

+43
-26
lines changed

modules/angular2/src/core/change_detection/change_detection_jit_generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export class ChangeDetectorJITGenerator {
284284
var condition = `!${pipe}.pure || (${contexOrArgCheck.join(" || ")})`;
285285

286286
var check = `
287-
if (${oldValue} !== ${newValue}) {
287+
if (${this.changeDetectionUtilVarName}.looseNotIdentical(${oldValue}, ${newValue})) {
288288
${newValue} = ${this.changeDetectionUtilVarName}.unwrapValue(${newValue})
289289
${this._genChangeMarker(r)}
290290
${this._genUpdateDirectiveOrElement(r)}
@@ -311,7 +311,7 @@ export class ChangeDetectorJITGenerator {
311311
`;
312312

313313
var check = `
314-
if (${newValue} !== ${oldValue}) {
314+
if (${this.changeDetectionUtilVarName}.looseNotIdentical(${oldValue}, ${newValue})) {
315315
${this._genChangeMarker(r)}
316316
${this._genUpdateDirectiveOrElement(r)}
317317
${this._genAddToChanges(r)}

modules/angular2/src/core/change_detection/change_detection_util.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import {CONST_EXPR, isPresent, isBlank, Type, StringWrapper} from 'angular2/src/core/facade/lang';
1+
import {
2+
CONST_EXPR,
3+
isPresent,
4+
isBlank,
5+
Type,
6+
StringWrapper,
7+
looseIdentical
8+
} from 'angular2/src/core/facade/lang';
29
import {BaseException} from 'angular2/src/core/facade/exceptions';
310
import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
411
import {ProtoRecord} from './proto_record';
@@ -202,4 +209,6 @@ export class ChangeDetectionUtil {
202209
static directiveIndex(elementIndex: number, directiveIndex: number): DirectiveIndex {
203210
return new DirectiveIndex(elementIndex, directiveIndex);
204211
}
212+
213+
static looseNotIdentical(a: any, b: any): boolean { return !looseIdentical(a, b); }
205214
}

modules/angular2/src/core/change_detection/coalesce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isPresent, isBlank, looseIdentical} from 'angular2/src/core/facade/lang';
1+
import {isPresent, isBlank, looseIdentical, StringWrapper} from 'angular2/src/core/facade/lang';
22
import {ListWrapper, Map} from 'angular2/src/core/facade/collection';
33
import {RecordType, ProtoRecord} from './proto_record';
44

@@ -52,7 +52,7 @@ function _findMatching(r: ProtoRecord, rs: ProtoRecord[]) {
5252
return ListWrapper.find(
5353
rs, (rr) => rr.mode !== RecordType.DirectiveLifecycle && _sameDirIndex(rr, r) &&
5454
rr.mode === r.mode && looseIdentical(rr.funcOrValue, r.funcOrValue) &&
55-
rr.contextIndex === r.contextIndex && looseIdentical(rr.name, r.name) &&
55+
rr.contextIndex === r.contextIndex && StringWrapper.equals(rr.name, r.name) &&
5656
ListWrapper.equals(rr.args, r.args));
5757
}
5858

modules/angular2/src/core/change_detection/dynamic_change_detector.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
245245

246246
if (proto.shouldBeChecked()) {
247247
var prevValue = this._readSelf(proto, values);
248-
if (!isSame(prevValue, currValue)) {
248+
if (ChangeDetectionUtil.looseNotIdentical(prevValue, currValue)) {
249249
if (proto.lastInBinding) {
250250
var change = ChangeDetectionUtil.simpleChange(prevValue, currValue);
251251
if (throwOnChange) this.throwOnChangeError(prevValue, currValue);
@@ -348,7 +348,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
348348

349349
if (proto.shouldBeChecked()) {
350350
var prevValue = this._readSelf(proto, values);
351-
if (!isSame(prevValue, currValue)) {
351+
if (ChangeDetectionUtil.looseNotIdentical(prevValue, currValue)) {
352352
currValue = ChangeDetectionUtil.unwrapValue(currValue);
353353

354354
if (proto.lastInBinding) {
@@ -443,10 +443,3 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
443443
return res;
444444
}
445445
}
446-
447-
function isSame(a, b): boolean {
448-
if (a === b) return true;
449-
if (a instanceof String && b instanceof String && a == b) return true;
450-
if ((a !== a) && (b !== b)) return true;
451-
return false;
452-
}

modules/angular2/src/core/change_detection/pregen_proto_change_detector.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,3 @@ class PregenProtoChangeDetector extends ProtoChangeDetector {
4949
@override
5050
instantiate(dynamic dispatcher) => _instantiateMethod(dispatcher);
5151
}
52-
53-
/// Provided as an optimization to cut down on '!' characters in generated
54-
/// change detectors. See https://github.com/angular/angular/issues/3248 for
55-
/// for details.
56-
bool looseNotIdentical(a, b) => !looseIdentical(a, b);

modules/angular2/test/core/change_detection/change_detector_config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ var _availableDefinitions = [
418418
'a()(99)',
419419
'a.sayHi("Jim")',
420420
'passThrough([12])',
421-
'invalidFn(1)'
421+
'invalidFn(1)',
422+
'age'
422423
];
423424

424425
var _availableEventDefinitions = [

modules/angular2/test/core/change_detection/change_detector_spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import {
1717
CONST_EXPR,
1818
isPresent,
1919
isBlank,
20+
isNumber,
2021
isJsObject,
2122
FunctionWrapper,
23+
NumberWrapper,
2224
normalizeBool
2325
} from 'angular2/src/core/facade/lang';
2426
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
@@ -258,6 +260,19 @@ export function main() {
258260
expect(_bindSimpleValue('a.sayHi("Jim")', td)).toEqual(['propName=Hi, Jim']);
259261
});
260262

263+
it('should support NaN', () => {
264+
var person = new Person('misko');
265+
person.age = NumberWrapper.NaN;
266+
var val = _createChangeDetector('age', person);
267+
268+
val.changeDetector.detectChanges();
269+
expect(val.dispatcher.log).toEqual(['propName=NaN']);
270+
val.dispatcher.clear();
271+
272+
val.changeDetector.detectChanges();
273+
expect(val.dispatcher.log).toEqual([]);
274+
});
275+
261276
it('should do simple watching', () => {
262277
var person = new Person('misko');
263278
var val = _createChangeDetector('name', person);
@@ -1420,7 +1435,13 @@ class TestDispatcher implements ChangeDispatcher {
14201435

14211436
getDebugContext(a, b) { return null; }
14221437

1423-
_asString(value) { return (isBlank(value) ? 'null' : value.toString()); }
1438+
_asString(value) {
1439+
if (isNumber(value) && NumberWrapper.isNaN(value)) {
1440+
return 'NaN';
1441+
}
1442+
1443+
return isBlank(value) ? 'null' : value.toString();
1444+
}
14241445
}
14251446

14261447
class _ChangeDetectorAndDispatcher {

modules_dart/transform/lib/src/transform/template_compiler/change_detector_codegen.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ class _CodegenState {
354354
var pipeType = r.name;
355355

356356
var init = '''
357-
if (${_genPrefix}$_IDENTICAL_CHECK_FN($pipe, ${_genPrefix}$_UTIL.uninitialized)) {
357+
if ($pipe == ${_genPrefix}$_UTIL.uninitialized) {
358358
$pipe = ${_names.getPipesAccessorName()}.get('$pipeType');
359359
}
360360
''';
@@ -368,7 +368,7 @@ class _CodegenState {
368368
var condition = '''!${pipe}.pure || (${contexOrArgCheck.join(" || ")})''';
369369

370370
var check = '''
371-
if (${_genPrefix}$_NOT_IDENTICAL_CHECK_FN($oldValue, $newValue)) {
371+
if (${_genPrefix}$_UTIL.looseNotIdentical($oldValue, $newValue)) {
372372
$newValue = ${_genPrefix}$_UTIL.unwrapValue($newValue);
373373
${_genChangeMarker(r)}
374374
${_genUpdateDirectiveOrElement(r)}
@@ -394,7 +394,7 @@ class _CodegenState {
394394
''';
395395

396396
var check = '''
397-
if (${_genPrefix}$_NOT_IDENTICAL_CHECK_FN($newValue, $oldValue)) {
397+
if (${_genPrefix}$_UTIL.looseNotIdentical($newValue, $oldValue)) {
398398
${_genChangeMarker(r)}
399399
${_genUpdateDirectiveOrElement(r)}
400400
${_genAddToChanges(r)}
@@ -532,8 +532,6 @@ const _CHANGES_LOCAL = 'changes';
532532
const _GEN_PREFIX = '_gen';
533533
const _GEN_PREFIX_WITH_DOT = _GEN_PREFIX + '.';
534534
const _GEN_RECORDS_METHOD_NAME = '_createRecords';
535-
const _IDENTICAL_CHECK_FN = 'looseIdentical';
536-
const _NOT_IDENTICAL_CHECK_FN = 'looseNotIdentical';
537535
const _IS_CHANGED_LOCAL = 'isChanged';
538536
const _PREGEN_PROTO_CHANGE_DETECTOR_IMPORT =
539537
'package:angular2/src/core/change_detection/pregen_proto_change_detector.dart';

0 commit comments

Comments
 (0)
X Tutup