X Tutup
Skip to content

Commit abc4ef3

Browse files
committed
fix(test): AngularProfiler should check before using modern APIs
1 parent 55290b9 commit abc4ef3

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

modules/angular2/src/core/dom/browser_adapter.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
474474
cancelAnimationFrame(id) {
475475
window.cancelAnimationFrame(id);
476476
}
477+
478+
num performanceNow() {
479+
return window.performance.now();
480+
}
477481
}
478482

479483
var baseElement = null;

modules/angular2/src/core/dom/browser_adapter.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import {MapWrapper, ListWrapper} from 'angular2/src/core/facade/collection';
2-
import {isBlank, isPresent, global, setValueOnPath} from 'angular2/src/core/facade/lang';
2+
import {
3+
isBlank,
4+
isPresent,
5+
global,
6+
setValueOnPath,
7+
DateWrapper
8+
} from 'angular2/src/core/facade/lang';
39
import {setRootDomAdapter} from './dom_adapter';
410
import {GenericBrowserDomAdapter} from './generic_browser_adapter';
511

@@ -322,6 +328,15 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
322328
setGlobalVar(path: string, value: any) { setValueOnPath(global, path, value); }
323329
requestAnimationFrame(callback): number { return window.requestAnimationFrame(callback); }
324330
cancelAnimationFrame(id: number) { window.cancelAnimationFrame(id); }
331+
performanceNow(): number {
332+
// performance.now() is not available in all browsers, see
333+
// http://caniuse.com/#search=performance.now
334+
if (isPresent(window.performance) && isPresent(window.performance.now)) {
335+
return window.performance.now();
336+
} else {
337+
return DateWrapper.toMillis(DateWrapper.now());
338+
}
339+
}
325340
}
326341

327342

modules/angular2/src/core/dom/dom_adapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,5 @@ export class DomAdapter {
138138
setGlobalVar(name: string, value: any) { throw _abstract(); }
139139
requestAnimationFrame(callback): number { throw _abstract(); }
140140
cancelAnimationFrame(id) { throw _abstract(); }
141+
performanceNow(): number { throw _abstract(); }
141142
}

modules/angular2/src/core/dom/html_adapter.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,8 @@ class Html5LibDomAdapter implements DomAdapter {
431431
cancelAnimationFrame(id) {
432432
throw 'not implemented';
433433
}
434+
435+
performanceNow() {
436+
throw 'not implemented';
437+
}
434438
}

modules/angular2/src/core/dom/parse5_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ var url = require('url');
99

1010
import {MapWrapper, ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
1111
import {DomAdapter, setRootDomAdapter} from './dom_adapter';
12-
import {isPresent, isBlank, global, setValueOnPath} from 'angular2/src/core/facade/lang';
12+
import {
13+
isPresent,
14+
isBlank,
15+
global,
16+
setValueOnPath,
17+
DateWrapper
18+
} from 'angular2/src/core/facade/lang';
1319
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
1420
import {SelectorMatcher, CssSelector} from 'angular2/src/core/render/dom/compiler/selector';
1521

@@ -545,6 +551,7 @@ export class Parse5DomAdapter extends DomAdapter {
545551
setGlobalVar(path: string, value: any) { setValueOnPath(global, path, value); }
546552
requestAnimationFrame(callback): number { return setTimeout(callback, 0); }
547553
cancelAnimationFrame(id: number) { clearTimeout(id); }
554+
performanceNow(): number { return DateWrapper.toMillis(DateWrapper.now()); }
548555
}
549556

550557
// TODO: build a proper list, this one is all the keys of a HTMLInputElement

modules/angular2/src/tools/common_tools.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ApplicationRef, LifeCycle} from 'angular2/angular2';
22
import {isPresent, NumberWrapper} from 'angular2/src/core/facade/lang';
33
import {performance, window} from 'angular2/src/core/facade/browser';
4+
import {DOM} from 'angular2/src/core/dom/dom_adapter';
45

56
/**
67
* Entry point for all Angular debug tools. This object corresponds to the `ng`
@@ -40,17 +41,19 @@ export class AngularProfiler {
4041
timeChangeDetection(config: any) {
4142
var record = isPresent(config) && config['record'];
4243
var profileName = 'Change Detection';
43-
if (record) {
44+
// Profiler is not available in Android browsers, nor in IE 9 without dev tools opened
45+
var isProfilerAvailable = isPresent(window.console.profile);
46+
if (record && isProfilerAvailable) {
4447
window.console.profile(profileName);
4548
}
46-
var start = window.performance.now();
49+
var start = DOM.performanceNow();
4750
var numTicks = 0;
48-
while (numTicks < 5 || (window.performance.now() - start) < 500) {
51+
while (numTicks < 5 || (DOM.performanceNow() - start) < 500) {
4952
this.lifeCycle.tick();
5053
numTicks++;
5154
}
52-
var end = window.performance.now();
53-
if (record) {
55+
var end = DOM.performanceNow();
56+
if (record && isProfilerAvailable) {
5457
// need to cast to <any> because type checker thinks there's no argument
5558
// while in fact there is:
5659
//

0 commit comments

Comments
 (0)
X Tutup