Skip to content

Commit 34eaf65

Browse files
committed
docs(Http): add docs about breaking changes with EventEmitter/Observable
BREAKING CHANGE: The Http module previously would return RxJS Observables from method calls of the Http class. In order to support Dart, the module was refactored to return the EventEmitter abstraction instead, which does not contain the same combinators or subscription semantics as an RxJS Observable. However, the EventEmitter provides a toRx() method which will return an RxJS Subject, providing the same subscription and combinator conveniences as were available prior to this refactor. This is temporary, until issue angular#2794 is resolved, when Observables will again be returned directly from Http class methods.
1 parent 27e7100 commit 34eaf65

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

modules/angular2/src/http/http.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
3636
*
3737
* `Http` is available as an injectable class, with methods to perform http requests. Calling
3838
* `request` returns an {@link EventEmitter} which will emit a single {@link Response} when a
39-
*response is
40-
* received.
39+
* response is received.
40+
*
41+
*
42+
* ## Breaking Change
43+
*
44+
* Previously, methods of `Http` would return an RxJS Observable directly. For now,
45+
* the `toRx()` method of {@link EventEmitter} needs to be called in order to get the RxJS
46+
* Subject. `EventEmitter` does not provide combinators like `map`, and has different semantics for
47+
* subscribing/observing. This is temporary; the result of all `Http` method calls will be either an
48+
* Observable
49+
* or Dart Stream when [issue #2794](https://github.com/angular/angular/issues/2794) is resolved.
4150
*
4251
* #Example
4352
*
@@ -47,7 +56,9 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
4756
* @View({templateUrl: 'people.html'})
4857
* class PeopleComponent {
4958
* constructor(http: Http) {
50-
* http('people.json')
59+
* http.get('people.json')
60+
* //Get the RxJS Subject
61+
* .toRx()
5162
* // Call map on the response observable to get the parsed people object
5263
* .map(res => res.json())
5364
* // Subscribe to the observable to get the parsed people object and attach it to the
@@ -57,6 +68,16 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
5768
* }
5869
* ```
5970
*
71+
* To use the {@link EventEmitter} returned by `Http`, simply pass a generator (See "interface
72+
*Generator" in the Async Generator spec: https://github.com/jhusain/asyncgenerator) to the
73+
*`observer` method of the returned emitter, with optional methods of `next`, `throw`, and `return`.
74+
*
75+
* #Example
76+
*
77+
* ```
78+
* http.get('people.json').observer({next: (value) => this.people = people});
79+
* ```
80+
*
6081
* The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
6182
* {@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
6283
* the {@link XHRBackend} binding, as in the following example:
@@ -75,7 +96,7 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
7596
* [MockBackend, BaseRequestOptions])
7697
* ]);
7798
* var http = injector.get(Http);
78-
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
99+
* http.get('request-from-mock-backend.json').toRx().subscribe((res:Response) => doSomething(res));
79100
* ```
80101
*
81102
**/

0 commit comments

Comments
 (0)