X Tutup
Skip to content

Commit d7ab5d4

Browse files
committed
fix(testing): let DOM adapter dictate XHR implementation for tests
The test injector now uses an XHR implementation based on DOM.getXHR, which allows the current DOM adapter to dictate which XHR impl should be used. To prevent the changes to DOM adapter from introducing undesired new dependencies into the benchmarks, separate the async facade into a promise facade which is reexported by facade/async. See #4539
1 parent 65c737f commit d7ab5d4

File tree

12 files changed

+136
-116
lines changed

12 files changed

+136
-116
lines changed

modules/angular2/src/core/compiler/xhr_impl.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {Injectable} from 'angular2/src/core/di';
2-
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/core/facade/async';
1+
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/core/facade/promise';
32
import {isPresent} from 'angular2/src/core/facade/lang';
43
import {XHR} from './xhr';
54

6-
@Injectable()
75
export class XHRImpl extends XHR {
86
get(url: string): Promise<string> {
97
var completer: PromiseCompleter < string >= PromiseWrapper.completer();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:html/dom.dart';
55

66
import 'dom_adapter.dart';
77
import 'emulated_css.dart';
8+
import '../compiler/xhr.dart';
89

910
const _attrToPropMap = const {
1011
'innerHtml': 'innerHTML',
@@ -66,6 +67,9 @@ abstract class AbstractHtml5LibAdapter implements DomAdapter {
6667
throw 'not implemented';
6768
}
6869

70+
@override
71+
Type getXHR() => XHR;
72+
6973
Element parse(String templateHtml) => parser.parse(templateHtml).firstChild;
7074
query(selector) {
7175
throw 'not implemented';

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isBlank} from 'angular2/src/core/facade/lang';
1+
import {isBlank, Type} from 'angular2/src/core/facade/lang';
22

33
export var DOM: DomAdapter;
44

@@ -23,6 +23,8 @@ export abstract class DomAdapter {
2323
abstract logGroup(error);
2424
abstract logGroupEnd();
2525

26+
abstract getXHR(): Type;
27+
2628
/**
2729
* Maps attribute names to their corresponding property names for cases
2830
* where attribute name doesn't match property name.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
2-
import {isPresent, isFunction, StringWrapper} from 'angular2/src/core/facade/lang';
2+
import {isPresent, isFunction, StringWrapper, Type} from 'angular2/src/core/facade/lang';
33
import {DomAdapter} from './dom_adapter';
4+
import {XHRImpl} from 'angular2/src/core/compiler/xhr_impl';
5+
46

57
/**
68
* Provides DOM operations in any browser environment.
@@ -39,6 +41,8 @@ export abstract class GenericBrowserDomAdapter extends DomAdapter {
3941
this._transitionEnd = null;
4042
}
4143
}
44+
45+
getXHR(): Type { return XHRImpl; }
4246
getDistributedNodes(el: HTMLElement): Node[] { return (<any>el).getDistributedNodes(); }
4347
resolveAndSetHref(el: HTMLAnchorElement, baseUrl: string, href: string) {
4448
el.href = href == null ? baseUrl : baseUrl + '/../' + href;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import {
1111
isPresent,
1212
isBlank,
1313
global,
14+
Type,
1415
setValueOnPath,
1516
DateWrapper
1617
} from 'angular2/src/core/facade/lang';
1718
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
1819
import {SelectorMatcher, CssSelector} from 'angular2/src/core/compiler/selector';
20+
import {XHR} from 'angular2/src/core/compiler/xhr';
1921

2022
var _attrToPropMap: {[key: string]: string} = {
2123
'class': 'className',
@@ -61,6 +63,8 @@ export class Parse5DomAdapter extends DomAdapter {
6163

6264
logGroupEnd() {}
6365

66+
getXHR(): Type { return XHR; }
67+
6468
get attrToPropMap() { return _attrToPropMap; }
6569

6670
query(selector) { throw _notImplemented('query'); }

modules/angular2/src/core/facade/async.dart

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
11
library angular2.core.facade.async;
22

33
import 'dart:async';
4-
export 'dart:async' show Future, Stream, StreamController, StreamSubscription;
4+
export 'dart:async' show Stream, StreamController, StreamSubscription;
55

6-
class PromiseWrapper {
7-
static Future resolve(obj) => new Future.value(obj);
8-
9-
static Future reject(obj, stackTrace) => new Future.error(obj,
10-
stackTrace != null ? stackTrace : obj is Error ? obj.stackTrace : null);
11-
12-
static Future<List> all(List<dynamic> promises) {
13-
return Future
14-
.wait(promises.map((p) => p is Future ? p : new Future.value(p)));
15-
}
16-
17-
static Future then(Future promise, success(value), [Function onError]) {
18-
if (success == null) return promise.catchError(onError);
19-
return promise.then(success, onError: onError);
20-
}
21-
22-
static Future wrap(Function fn) {
23-
return new Future(fn);
24-
}
25-
26-
// Note: We can't rename this method to `catch`, as this is not a valid
27-
// method name in Dart.
28-
static Future catchError(Future promise, Function onError) {
29-
return promise.catchError(onError);
30-
}
31-
32-
static PromiseCompleter<dynamic> completer() =>
33-
new PromiseCompleter(new Completer());
34-
}
6+
export 'promise.dart';
357

368
class TimerWrapper {
379
static Timer setTimeout(fn(), int millis) =>
@@ -105,22 +77,3 @@ class EventEmitter extends Stream {
10577
_controller.close();
10678
}
10779
}
108-
109-
class PromiseCompleter<T> {
110-
final Completer<T> c;
111-
112-
PromiseCompleter(this.c);
113-
114-
Future get promise => c.future;
115-
116-
void resolve(v) {
117-
c.complete(v);
118-
}
119-
120-
void reject(error, stack) {
121-
if (stack == null && error is Error) {
122-
stack = error.stackTrace;
123-
}
124-
c.completeError(error, stack);
125-
}
126-
}

modules/angular2/src/core/facade/async.ts

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,11 @@
11
import {global, isPresent} from 'angular2/src/core/facade/lang';
2+
// We make sure promises are in a separate file so that we can use promises
3+
// without depending on rxjs.
4+
import {PromiseWrapper, Promise, PromiseCompleter} from 'angular2/src/core/facade/promise';
5+
export {PromiseWrapper, Promise, PromiseCompleter} from 'angular2/src/core/facade/promise';
26
// TODO(jeffbcross): use ES6 import once typings are available
37
var Subject = require('@reactivex/rxjs/dist/cjs/Subject');
48

5-
export {Promise};
6-
7-
export interface PromiseCompleter<R> {
8-
promise: Promise<R>;
9-
resolve: (value?: R | PromiseLike<R>) => void;
10-
reject: (error?: any, stackTrace?: string) => void;
11-
}
12-
13-
export class PromiseWrapper {
14-
static resolve<T>(obj: T): Promise<T> { return Promise.resolve(obj); }
15-
16-
static reject(obj: any, _): Promise<any> { return Promise.reject(obj); }
17-
18-
// Note: We can't rename this method into `catch`, as this is not a valid
19-
// method name in Dart.
20-
static catchError<T>(promise: Promise<T>,
21-
onError: (error: any) => T | PromiseLike<T>): Promise<T> {
22-
return promise.catch(onError);
23-
}
24-
25-
static all(promises: any[]): Promise<any> {
26-
if (promises.length == 0) return Promise.resolve([]);
27-
return Promise.all(promises);
28-
}
29-
30-
static then<T, U>(promise: Promise<T>, success: (value: T) => U | PromiseLike<U>,
31-
rejection?: (error: any, stack?: any) => U | PromiseLike<U>): Promise<U> {
32-
return promise.then(success, rejection);
33-
}
34-
35-
static wrap<T>(computation: () => T): Promise<T> {
36-
return new Promise((res, rej) => {
37-
try {
38-
res(computation());
39-
} catch (e) {
40-
rej(e);
41-
}
42-
});
43-
}
44-
45-
static completer(): PromiseCompleter<any> {
46-
var resolve;
47-
var reject;
48-
49-
var p = new Promise(function(res, rej) {
50-
resolve = res;
51-
reject = rej;
52-
});
53-
54-
return {promise: p, resolve: resolve, reject: reject};
55-
}
56-
}
57-
589
export namespace NodeJS {
5910
export interface Timer {}
6011
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
library angular2.core.facade.promise;
2+
3+
import 'dart:async';
4+
export 'dart:async' show Future;
5+
6+
class PromiseWrapper {
7+
static Future resolve(obj) => new Future.value(obj);
8+
9+
static Future reject(obj, stackTrace) => new Future.error(obj,
10+
stackTrace != null ? stackTrace : obj is Error ? obj.stackTrace : null);
11+
12+
static Future<List> all(List<dynamic> promises) {
13+
return Future
14+
.wait(promises.map((p) => p is Future ? p : new Future.value(p)));
15+
}
16+
17+
static Future then(Future promise, success(value), [Function onError]) {
18+
if (success == null) return promise.catchError(onError);
19+
return promise.then(success, onError: onError);
20+
}
21+
22+
static Future wrap(Function fn) {
23+
return new Future(fn);
24+
}
25+
26+
// Note: We can't rename this method to `catch`, as this is not a valid
27+
// method name in Dart.
28+
static Future catchError(Future promise, Function onError) {
29+
return promise.catchError(onError);
30+
}
31+
32+
static PromiseCompleter<dynamic> completer() =>
33+
new PromiseCompleter(new Completer());
34+
}
35+
36+
class PromiseCompleter<T> {
37+
final Completer<T> c;
38+
39+
PromiseCompleter(this.c);
40+
41+
Future get promise => c.future;
42+
43+
void resolve(v) {
44+
c.complete(v);
45+
}
46+
47+
void reject(error, stack) {
48+
if (stack == null && error is Error) {
49+
stack = error.stackTrace;
50+
}
51+
c.completeError(error, stack);
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Promises are put into their own facade file so that they can be used without
2+
// introducing a dependency on rxjs. They are re-exported through facade/async.
3+
export {Promise};
4+
5+
export interface PromiseCompleter<R> {
6+
promise: Promise<R>;
7+
resolve: (value?: R | PromiseLike<R>) => void;
8+
reject: (error?: any, stackTrace?: string) => void;
9+
}
10+
11+
export class PromiseWrapper {
12+
static resolve<T>(obj: T): Promise<T> { return Promise.resolve(obj); }
13+
14+
static reject(obj: any, _): Promise<any> { return Promise.reject(obj); }
15+
16+
// Note: We can't rename this method into `catch`, as this is not a valid
17+
// method name in Dart.
18+
static catchError<T>(promise: Promise<T>,
19+
onError: (error: any) => T | PromiseLike<T>): Promise<T> {
20+
return promise.catch(onError);
21+
}
22+
23+
static all(promises: any[]): Promise<any> {
24+
if (promises.length == 0) return Promise.resolve([]);
25+
return Promise.all(promises);
26+
}
27+
28+
static then<T, U>(promise: Promise<T>, success: (value: T) => U | PromiseLike<U>,
29+
rejection?: (error: any, stack?: any) => U | PromiseLike<U>): Promise<U> {
30+
return promise.then(success, rejection);
31+
}
32+
33+
static wrap<T>(computation: () => T): Promise<T> {
34+
return new Promise((res, rej) => {
35+
try {
36+
res(computation());
37+
} catch (e) {
38+
rej(e);
39+
}
40+
});
41+
}
42+
43+
static completer(): PromiseCompleter<any> {
44+
var resolve;
45+
var reject;
46+
47+
var p = new Promise(function(res, rej) {
48+
resolve = res;
49+
reject = rej;
50+
});
51+
52+
return {promise: p, resolve: resolve, reject: reject};
53+
}
54+
}

modules/angular2/src/testing/test_injector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function _getAppBindings() {
115115
PipeResolver,
116116
provide(ExceptionHandler, {useValue: new ExceptionHandler(DOM)}),
117117
provide(LocationStrategy, {useClass: MockLocationStrategy}),
118-
XHR,
118+
provide(XHR, {useClass: DOM.getXHR()}),
119119
TestComponentBuilder,
120120
provide(NgZone, {useClass: MockNgZone}),
121121
provide(AnimationBuilder, {useClass: MockAnimationBuilder}),

0 commit comments

Comments
 (0)
X Tutup