Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AsyncPipe): allow onError argument #7990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions modules/angular2/src/common/pipes/async_pipe.ts
@@ -1,4 +1,5 @@
import {isBlank, isPresent, isPromise, CONST} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this anymore

import {ObservableWrapper, Observable, EventEmitter} from 'angular2/src/facade/async';
import {
Pipe,
Expand All @@ -12,8 +13,9 @@ import {
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';

class ObservableStrategy {
createSubscription(async: any, updateLatestValue: any): any {
return ObservableWrapper.subscribe(async, updateLatestValue, e => { throw e; });
createSubscription(async: any, updateLatestValue: any,
onError: (v: any) => any = e => { throw e; }): any {
return ObservableWrapper.subscribe(async, updateLatestValue, onError);
}

dispose(subscription: any): void { ObservableWrapper.dispose(subscription); }
Expand All @@ -22,8 +24,9 @@ class ObservableStrategy {
}

class PromiseStrategy {
createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any): any {
return async.then(updateLatestValue);
createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any,
onError: (v: any) => any = e => { throw e; }): any {
return async.then(updateLatestValue, onError);
}

dispose(subscription: any): void {}
Expand Down Expand Up @@ -79,15 +82,15 @@ export class AsyncPipe implements PipeTransform, OnDestroy {
transform(obj: Observable<any>| Promise<any>| EventEmitter<any>, args?: any[]): any {
if (isBlank(this._obj)) {
if (isPresent(obj)) {
this._subscribe(obj);
this._subscribe(obj, ListWrapper.first(args));
}
this._latestReturnedValue = this._latestValue;
return this._latestValue;
}

if (obj !== this._obj) {
this._dispose();
return this.transform(obj);
return this.transform(obj, ListWrapper.first(args));
}

if (this._latestValue === this._latestReturnedValue) {
Expand All @@ -99,11 +102,11 @@ export class AsyncPipe implements PipeTransform, OnDestroy {
}

/** @internal */
_subscribe(obj: Observable<any>| Promise<any>| EventEmitter<any>): void {
_subscribe(obj: Observable<any>| Promise<any>| EventEmitter<any>, onError?: any): void {
this._obj = obj;
this._strategy = this._selectStrategy(obj);
this._subscription = this._strategy.createSubscription(
obj, (value: Object) => this._updateLatestValue(obj, value));
obj, (value: Object) => this._updateLatestValue(obj, value), onError);
}

/** @internal */
Expand Down
67 changes: 50 additions & 17 deletions modules/angular2/test/common/pipes/async_pipe_spec.ts
Expand Up @@ -53,10 +53,9 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
async.done();
}, 0)
}, 0);
}));


it("should return same value when nothing has changed since the last call",
inject([AsyncTestCompleter], (async) => {
pipe.transform(emitter);
Expand All @@ -66,7 +65,24 @@ export function main() {
pipe.transform(emitter);
expect(pipe.transform(emitter)).toBe(message);
async.done();
}, 0)
}, 0);
}));

it("should invoke onError of when a function is provided",
inject([AsyncTestCompleter], (async) => {
var error = false;
function onError() { error = true; }
var args = [onError];
expect(() => pipe.transform(emitter, args)).not.toThrow();

expect(error).toBe(false);
// this should not affect the pipe
ObservableWrapper.callError(emitter, message);

TimerWrapper.setTimeout(() => {
expect(error).toBe(true);
async.done();
}, 0);
}));

it("should dispose of the existing subscription when subscribing to a new observable",
Expand All @@ -82,7 +98,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(newEmitter)).toBe(null);
async.done();
}, 0)
}, 0);
}));

it("should request a change detection check upon receiving a new value",
Expand All @@ -93,7 +109,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(ref.spy('markForCheck')).toHaveBeenCalled();
async.done();
}, 10)
}, 10);
}));
});

Expand All @@ -110,7 +126,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(emitter)).toBe(null);
async.done();
}, 0)
}, 0);
}));
});
});
Expand Down Expand Up @@ -141,7 +157,24 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
async.done();
}, timer)
}, timer);
}));

it("should invoke onError of when a function is provided",
inject([AsyncTestCompleter], (async) => {
var error = false;
function onError() { error = true; }
var args = [onError];
expect(() => pipe.transform(completer.promise, args)).not.toThrow();

expect(error).toBe(false);
// this should not affect the pipe
completer.reject(completer.promise);

TimerWrapper.setTimeout(() => {
expect(error).toBe(true);
async.done();
}, 0);
}));

it("should return unwrapped value when nothing has changed since the last call",
Expand All @@ -153,7 +186,7 @@ export function main() {
pipe.transform(completer.promise);
expect(pipe.transform(completer.promise)).toBe(message);
async.done();
}, timer)
}, timer);
}));

it("should dispose of the existing subscription when subscribing to a new promise",
Expand All @@ -169,7 +202,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(pipe.transform(newCompleter.promise)).toBe(null);
async.done();
}, timer)
}, timer);
}));

it("should request a change detection check upon receiving a new value",
Expand All @@ -181,7 +214,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(markForCheck).toHaveBeenCalled();
async.done();
}, timer)
}, timer);
}));

describe("ngOnDestroy", () => {
Expand All @@ -191,15 +224,15 @@ export function main() {
it("should dispose of the existing source", inject([AsyncTestCompleter], (async) => {
pipe.transform(completer.promise);
expect(pipe.transform(completer.promise)).toBe(null);
completer.resolve(message)
completer.resolve(message);


TimerWrapper.setTimeout(() => {
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
pipe.ngOnDestroy();
expect(pipe.transform(completer.promise)).toBe(null);
async.done();
}, timer);
TimerWrapper.setTimeout(() => {
expect(pipe.transform(completer.promise)).toEqual(new WrappedValue(message));
pipe.ngOnDestroy();
expect(pipe.transform(completer.promise)).toBe(null);
async.done();
}, timer);
}));
});
});
Expand Down