@@ -3,7 +3,9 @@ import {Request} from 'angular2/src/http/static_request';
33import { Response } from 'angular2/src/http/static_response' ;
44import { ReadyStates } from 'angular2/src/http/enums' ;
55import { Connection , ConnectionBackend } from 'angular2/src/http/interfaces' ;
6- import * as Rx from 'rx' ;
6+ import { ObservableWrapper , EventEmitter } from 'angular2/src/facade/async' ;
7+ import { isPresent } from 'angular2/src/facade/lang' ;
8+ import { IMPLEMENTS , BaseException } from 'angular2/src/facade/lang' ;
79
810/**
911 *
@@ -14,7 +16,8 @@ import * as Rx from 'rx';
1416 * {@link MockBackend} in order to mock responses to requests.
1517 *
1618 **/
17- export class MockConnection implements Connection {
19+ @IMPLEMENTS ( Connection )
20+ export class MockConnection {
1821 // TODO Name `readyState` should change to be more generic, and states could be made to be more
1922 // descriptive than XHR states.
2023 /**
@@ -33,18 +36,12 @@ export class MockConnection implements Connection {
3336 * Observable](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md)
3437 * of {@link Response}. Can be subscribed to in order to be notified when a response is available.
3538 */
36- response : Rx . Subject < Response > ;
39+ response : EventEmitter ;
3740
3841 constructor ( req : Request ) {
39- if ( Rx . hasOwnProperty ( 'default' ) ) {
40- this . response = new ( ( < any > Rx ) . default . Rx . Subject ) ( ) ;
41- } else {
42- this . response = new Rx . Subject < Response > ( ) ;
43- }
44-
42+ this . response = new EventEmitter ( ) ;
4543 this . readyState = ReadyStates . OPEN ;
4644 this . request = req ;
47- this . dispose = this . dispose . bind ( this ) ;
4845 }
4946
5047 /**
@@ -71,12 +68,12 @@ export class MockConnection implements Connection {
7168 *
7269 */
7370 mockRespond ( res : Response ) {
74- if ( this . readyState >= ReadyStates . DONE ) {
75- throw new Error ( 'Connection has already been resolved' ) ;
71+ if ( this . readyState === ReadyStates . DONE || this . readyState === ReadyStates . CANCELLED ) {
72+ throw new BaseException ( 'Connection has already been resolved' ) ;
7673 }
7774 this . readyState = ReadyStates . DONE ;
78- this . response . onNext ( res ) ;
79- this . response . onCompleted ( ) ;
75+ ObservableWrapper . callNext ( this . response , res ) ;
76+ ObservableWrapper . callReturn ( this . response ) ;
8077 }
8178
8279 /**
@@ -100,8 +97,8 @@ export class MockConnection implements Connection {
10097 mockError ( err ?) {
10198 // Matches XHR semantics
10299 this . readyState = ReadyStates . DONE ;
103- this . response . onError ( err ) ;
104- this . response . onCompleted ( ) ;
100+ ObservableWrapper . callThrow ( this . response , err ) ;
101+ ObservableWrapper . callReturn ( this . response ) ;
105102 }
106103}
107104
@@ -137,7 +134,8 @@ export class MockConnection implements Connection {
137134 * This method only exists in the mock implementation, not in real Backends.
138135 **/
139136@Injectable ( )
140- export class MockBackend implements ConnectionBackend {
137+ @IMPLEMENTS ( ConnectionBackend )
138+ export class MockBackend {
141139 /**
142140 * [RxJS
143141 * Subject](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md)
@@ -171,7 +169,7 @@ export class MockBackend implements ConnectionBackend {
171169 *
172170 * This property only exists in the mock implementation, not in real Backends.
173171 */
174- connections : Rx . Subject < MockConnection > ;
172+ connections : EventEmitter ; // <MockConnection>
175173
176174 /**
177175 * An array representation of `connections`. This array will be updated with each connection that
@@ -188,20 +186,14 @@ export class MockBackend implements ConnectionBackend {
188186 *
189187 * This property only exists in the mock implementation, not in real Backends.
190188 */
191- pendingConnections : Rx . Observable < MockConnection > ;
189+ pendingConnections : EventEmitter ; // <MockConnection>
192190 constructor ( ) {
193- var Observable ;
194191 this . connectionsArray = [ ] ;
195- if ( Rx . hasOwnProperty ( 'default' ) ) {
196- this . connections = new ( < any > Rx ) . default . Rx . Subject ( ) ;
197- Observable = ( < any > Rx ) . default . Rx . Observable ;
198- } else {
199- this . connections = new Rx . Subject < MockConnection > ( ) ;
200- Observable = Rx . Observable ;
201- }
202- this . connections . subscribe ( connection => this . connectionsArray . push ( connection ) ) ;
203- this . pendingConnections =
204- Observable . fromArray ( this . connectionsArray ) . filter ( ( c ) => c . readyState < ReadyStates . DONE ) ;
192+ this . connections = new EventEmitter ( ) ;
193+ ObservableWrapper . subscribe ( this . connections ,
194+ connection => this . connectionsArray . push ( connection ) ) ;
195+ this . pendingConnections = new EventEmitter ( ) ;
196+ // Observable.fromArray(this.connectionsArray).filter((c) => c.readyState < ReadyStates.DONE);
205197 }
206198
207199 /**
@@ -211,8 +203,8 @@ export class MockBackend implements ConnectionBackend {
211203 */
212204 verifyNoPendingRequests ( ) {
213205 let pending = 0 ;
214- this . pendingConnections . subscribe ( ( c ) => pending ++ ) ;
215- if ( pending > 0 ) throw new Error ( `${ pending } pending connections to be resolved` ) ;
206+ ObservableWrapper . subscribe ( this . pendingConnections , c => pending ++ ) ;
207+ if ( pending > 0 ) throw new BaseException ( `${ pending } pending connections to be resolved` ) ;
216208 }
217209
218210 /**
@@ -221,20 +213,20 @@ export class MockBackend implements ConnectionBackend {
221213 *
222214 * This method only exists in the mock implementation, not in real Backends.
223215 */
224- resolveAllConnections ( ) { this . connections . subscribe ( ( c ) => c . readyState = 4 ) ; }
216+ resolveAllConnections ( ) { ObservableWrapper . subscribe ( this . connections , c => c . readyState = 4 ) ; }
225217
226218 /**
227219 * Creates a new {@link MockConnection}. This is equivalent to calling `new
228220 * MockConnection()`, except that it also will emit the new `Connection` to the `connections`
229221 * observable of this `MockBackend` instance. This method will usually only be used by tests
230222 * against the framework itself, not by end-users.
231223 */
232- createConnection ( req : Request ) : Connection {
233- if ( ! req || ! ( req instanceof Request ) ) {
234- throw new Error ( `createConnection requires an instance of Request, got ${ req } ` ) ;
224+ createConnection ( req : Request ) {
225+ if ( ! isPresent ( req ) || ! ( req instanceof Request ) ) {
226+ throw new BaseException ( `createConnection requires an instance of Request, got ${ req } ` ) ;
235227 }
236228 let connection = new MockConnection ( req ) ;
237- this . connections . onNext ( connection ) ;
229+ ObservableWrapper . callNext ( this . connections , connection ) ;
238230 return connection ;
239231 }
240232}
0 commit comments