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

refactor(common): remove WrappedValue from AsyncPipe #36633

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
12 changes: 2 additions & 10 deletions packages/common/src/pipes/async_pipe.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, WrappedValue, ɵisObservable, ɵisPromise, ɵlooseIdentical} from '@angular/core';
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, ɵisObservable, ɵisPromise} from '@angular/core';
import {Observable, SubscriptionLike} from 'rxjs';
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';

Expand Down Expand Up @@ -81,7 +81,6 @@ const _observableStrategy = new ObservableStrategy();
@Pipe({name: 'async', pure: false})
export class AsyncPipe implements OnDestroy, PipeTransform {
private _latestValue: any = null;
private _latestReturnedValue: any = null;

private _subscription: SubscriptionLike|Promise<any>|null = null;
private _obj: Observable<any>|Promise<any>|EventEmitter<any>|null = null;
Expand All @@ -104,7 +103,6 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
if (obj) {
this._subscribe(obj);
}
this._latestReturnedValue = this._latestValue;
return this._latestValue;
}

Expand All @@ -113,12 +111,7 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
return this.transform(obj as any);
}

if (ɵlooseIdentical(this._latestValue, this._latestReturnedValue)) {
return this._latestReturnedValue;
}

this._latestReturnedValue = this._latestValue;
return WrappedValue.wrap(this._latestValue);
return this._latestValue;
}

private _subscribe(obj: Observable<any>|Promise<any>|EventEmitter<any>): void {
Expand All @@ -143,7 +136,6 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
private _dispose(): void {
this._strategy.dispose(this._subscription!);
this._latestValue = null;
this._latestReturnedValue = null;
this._subscription = null;
this._obj = null;
}
Expand Down
18 changes: 8 additions & 10 deletions packages/common/test/pipes/async_pipe_spec.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {AsyncPipe, ɵgetDOM as getDOM} from '@angular/common';
import {EventEmitter, WrappedValue} from '@angular/core';
import {EventEmitter} from '@angular/core';
import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';

Expand All @@ -32,13 +32,13 @@ import {SpyChangeDetectorRef} from '../spies';
expect(pipe.transform(emitter)).toBe(null);
});

it('should return the latest available value wrapped',
it('should return the latest available value',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
pipe.transform(emitter);
emitter.emit(message);

setTimeout(() => {
expect(pipe.transform(emitter)).toEqual(new WrappedValue(message));
expect(pipe.transform(emitter)).toEqual(message);
async.done();
}, 0);
}));
Expand Down Expand Up @@ -82,15 +82,14 @@ import {SpyChangeDetectorRef} from '../spies';
}, 10);
}));

it('should return unwrapped value for unchanged NaN', () => {
it('should return value for unchanged NaN', () => {
const emitter = new EventEmitter<any>();
emitter.emit(null);
pipe.transform(emitter);
emitter.next(NaN);
const firstResult = pipe.transform(emitter);
const secondResult = pipe.transform(emitter);
expect(firstResult instanceof WrappedValue).toBe(true);
expect((firstResult as WrappedValue).wrapped).toBeNaN();
expect(firstResult).toBeNaN();
expect(secondResult).toBeNaN();
});
});
Expand Down Expand Up @@ -145,12 +144,12 @@ import {SpyChangeDetectorRef} from '../spies';
resolve(message);

setTimeout(() => {
expect(pipe.transform(promise)).toEqual(new WrappedValue(message));
expect(pipe.transform(promise)).toEqual(message);
async.done();
}, timer);
}));

it('should return unwrapped value when nothing has changed since the last call',
it('should return value when nothing has changed since the last call',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
pipe.transform(promise);
resolve(message);
Expand All @@ -169,7 +168,6 @@ import {SpyChangeDetectorRef} from '../spies';
promise = new Promise<any>(() => {});
expect(pipe.transform(promise)).toBe(null);

// this should not affect the pipe, so it should return WrappedValue
resolve(message);

setTimeout(() => {
Expand Down Expand Up @@ -203,7 +201,7 @@ import {SpyChangeDetectorRef} from '../spies';


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