chore(http): Use Observables in Http

- Remove ObservableWrapper/EventEmitter from Http.
- Temporarily use complete Rx build w/ all operators.
This commit is contained in:
Rob Wormald
2015-10-01 16:04:20 -07:00
committed by Jeff Cross
parent c9901c5fe0
commit 01fb06a377
9 changed files with 223 additions and 95 deletions

View File

@ -8,8 +8,9 @@ import {BrowserJsonp} from './browser_jsonp';
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
import {makeTypeError} from 'angular2/src/core/facade/exceptions';
import {StringWrapper, isPresent} from 'angular2/src/core/facade/lang';
var Observable = require('@reactivex/rxjs/dist/cjs/Observable');
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
export class JSONPConnection implements Connection {
readyState: ReadyStates;
request: Request;

View File

@ -3,9 +3,10 @@ import {Request} from '../static_request';
import {Response} from '../static_response';
import {ReadyStates} from '../enums';
import {Connection, ConnectionBackend} from '../interfaces';
import {ObservableWrapper, EventEmitter} from 'angular2/src/core/facade/async';
import {isPresent} from 'angular2/src/core/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
let{Subject, ReplaySubject} = Rx;
/**
*
@ -30,23 +31,14 @@ export class MockConnection implements Connection {
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
* response is available.
*/
response: EventEmitter;
response: any; // Subject<Response>
constructor(req: Request) {
this.response = new EventEmitter();
this.response = new ReplaySubject(1).take(1);
this.readyState = ReadyStates.Open;
this.request = req;
}
/**
* Changes the `readyState` of the connection to a custom state of 5 (cancelled).
*/
dispose() {
if (this.readyState !== ReadyStates.Done) {
this.readyState = ReadyStates.Cancelled;
}
}
/**
* Sends a mock response to the connection. This response is the value that is emitted to the
* {@link EventEmitter} returned by {@link Http}.
@ -66,8 +58,8 @@ export class MockConnection implements Connection {
throw new BaseException('Connection has already been resolved');
}
this.readyState = ReadyStates.Done;
ObservableWrapper.callNext(this.response, res);
ObservableWrapper.callReturn(this.response);
this.response.next(res);
this.response.complete();
}
/**
@ -92,8 +84,7 @@ export class MockConnection implements Connection {
mockError(err?: Error) {
// Matches XHR semantics
this.readyState = ReadyStates.Done;
ObservableWrapper.callThrow(this.response, err);
ObservableWrapper.callReturn(this.response);
this.response.error(err);
}
}
@ -162,7 +153,7 @@ export class MockBackend implements ConnectionBackend {
*
* This property only exists in the mock implementation, not in real Backends.
*/
connections: EventEmitter; //<MockConnection>
connections: any; //<MockConnection>
/**
* An array representation of `connections`. This array will be updated with each connection that
@ -179,13 +170,12 @@ export class MockBackend implements ConnectionBackend {
*
* This property only exists in the mock implementation, not in real Backends.
*/
pendingConnections: EventEmitter; //<MockConnection>
pendingConnections: any; // Subject<MockConnection>
constructor() {
this.connectionsArray = [];
this.connections = new EventEmitter();
ObservableWrapper.subscribe<MockConnection>(
this.connections, connection => this.connectionsArray.push(connection));
this.pendingConnections = new EventEmitter();
this.connections = new Subject();
this.connections.subscribe(connection => this.connectionsArray.push(connection));
this.pendingConnections = new Subject();
}
/**
@ -195,7 +185,7 @@ export class MockBackend implements ConnectionBackend {
*/
verifyNoPendingRequests() {
let pending = 0;
ObservableWrapper.subscribe(this.pendingConnections, c => pending++);
this.pendingConnections.subscribe(c => pending++);
if (pending > 0) throw new BaseException(`${pending} pending connections to be resolved`);
}
@ -205,9 +195,7 @@ export class MockBackend implements ConnectionBackend {
*
* This method only exists in the mock implementation, not in real Backends.
*/
resolveAllConnections() {
ObservableWrapper.subscribe<MockConnection>(this.connections, c => c.readyState = 4);
}
resolveAllConnections() { this.connections.subscribe(c => c.readyState = 4); }
/**
* Creates a new {@link MockConnection}. This is equivalent to calling `new
@ -220,7 +208,7 @@ export class MockBackend implements ConnectionBackend {
throw new BaseException(`createConnection requires an instance of Request, got ${req}`);
}
let connection = new MockConnection(req);
ObservableWrapper.callNext(this.connections, connection);
this.connections.next(connection);
return connection;
}
}

View File

@ -6,7 +6,9 @@ import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/src/core/di';
import {BrowserXhr} from './browser_xhr';
import {isPresent} from 'angular2/src/core/facade/lang';
var Observable = require('@reactivex/rxjs/dist/cjs/Observable');
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
/**
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the

View File

@ -5,9 +5,8 @@ import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces';
import {Request} from './static_request';
import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethods} from './enums';
import {EventEmitter} from 'angular2/src/core/facade/async';
function httpRequest(backend: ConnectionBackend, request: Request): EventEmitter {
function httpRequest(backend: ConnectionBackend, request: Request): any {
return backend.createConnection(request).response;
}
@ -94,8 +93,8 @@ export class Http {
* object can be provided as the 2nd argument. The options object will be merged with the values
* of {@link BaseRequestOptions} before performing the request.
*/
request(url: string | Request, options?: RequestOptionsArgs): EventEmitter {
var responseObservable: EventEmitter;
request(url: string | Request, options?: RequestOptionsArgs): any {
var responseObservable: any;
if (isString(url)) {
responseObservable = httpRequest(
this._backend,
@ -111,7 +110,7 @@ export class Http {
/**
* Performs a request with `get` http method.
*/
get(url: string, options?: RequestOptionsArgs): EventEmitter {
get(url: string, options?: RequestOptionsArgs): any {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Get, url)));
}
@ -119,7 +118,7 @@ export class Http {
/**
* Performs a request with `post` http method.
*/
post(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
post(url: string, body: string, options?: RequestOptionsArgs): any {
return httpRequest(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -129,7 +128,7 @@ export class Http {
/**
* Performs a request with `put` http method.
*/
put(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
put(url: string, body: string, options?: RequestOptionsArgs): any {
return httpRequest(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -139,7 +138,7 @@ export class Http {
/**
* Performs a request with `delete` http method.
*/
delete (url: string, options?: RequestOptionsArgs): EventEmitter {
delete (url: string, options?: RequestOptionsArgs): any {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Delete, url)));
}
@ -147,7 +146,7 @@ export class Http {
/**
* Performs a request with `patch` http method.
*/
patch(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
patch(url: string, body: string, options?: RequestOptionsArgs): any {
return httpRequest(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -157,7 +156,7 @@ export class Http {
/**
* Performs a request with `head` http method.
*/
head(url: string, options?: RequestOptionsArgs): EventEmitter {
head(url: string, options?: RequestOptionsArgs): any {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Head, url)));
}
@ -175,8 +174,8 @@ export class Jsonp extends Http {
* object can be provided as the 2nd argument. The options object will be merged with the values
* of {@link BaseRequestOptions} before performing the request.
*/
request(url: string | Request, options?: RequestOptionsArgs): EventEmitter {
var responseObservable: EventEmitter;
request(url: string | Request, options?: RequestOptionsArgs): any {
var responseObservable: any;
if (isString(url)) {
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url));
}

View File

@ -22,7 +22,7 @@ export abstract class ConnectionBackend {
export abstract class Connection {
readyState: ReadyStates;
request: Request;
response: EventEmitter; // TODO: generic of <Response>;
response: any; // TODO: generic of <Response>;
}
/**