Skip to content

Commit

Permalink
feat(rxjs): remove uses of rxjs patch operators
Browse files Browse the repository at this point in the history
  • Loading branch information
emoralesb05 committed Jul 9, 2017
1 parent ee140f7 commit cd2a386
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/app/services/selective-preloading-strategy.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { of } from 'rxjs/observable/of';

@Injectable()
export class SelectivePreloadingStrategyService implements PreloadingStrategy {
Expand All @@ -13,7 +13,7 @@ export class SelectivePreloadingStrategyService implements PreloadingStrategy {
return load();
} else {
/* tslint:disable-next-line */
return Observable.of(null);
return of(null);
}
}
}
22 changes: 10 additions & 12 deletions src/platform/core/chips/chips.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormControl } from '@angular/f

import { TemplatePortalDirective, UP_ARROW, DOWN_ARROW,
ESCAPE, LEFT_ARROW, RIGHT_ARROW, DELETE, BACKSPACE, ENTER, SPACE, TAB, HOME } from '@angular/cdk';
import { RxChain, debounceTime, filter } from '@angular/cdk';
import { MdChip, MdInputDirective, MdOption, MdAutocompleteTrigger } from '@angular/material';

import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/filter';
import { timer } from 'rxjs/observable/timer';
import { toPromise } from 'rxjs/operator/toPromise';
import { fromEvent } from 'rxjs/observable/fromEvent';

import { ICanDisable, mixinDisabled } from '../common/common.module';

Expand Down Expand Up @@ -292,7 +291,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
mousedownListener(event: FocusEvent): void {
// sets a flag to know if there was a mousedown and then it returns it back to false
this._isMousedown = true;
Observable.timer().toPromise().then(() => {
toPromise.call(timer()).then(() => {
this._isMousedown = false;
});
}
Expand Down Expand Up @@ -320,7 +319,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
switch (event.keyCode) {
case TAB:
// if tabing out, then unfocus the component
Observable.timer().toPromise().then(() => {
toPromise.call(timer()).then(() => {
this.removeFocusedState();
});
break;
Expand All @@ -339,8 +338,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
}

ngOnInit(): void {
this.inputControl.valueChanges
.debounceTime(this.debounce)
RxChain.from(this.inputControl.valueChanges).call(debounceTime, this.debounce)
.subscribe((value: string) => {
this.onInputChange.emit(value ? value : '');
});
Expand Down Expand Up @@ -417,7 +415,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
* to rerender the next list and at the correct spot
*/
this._closeAutocomplete();
Observable.timer(this.debounce).toPromise().then(() => {
toPromise.call(timer(this.debounce)).then(() => {
this.setFocusedState();
this._setFirstOptionActive();
this._openAutocomplete();
Expand Down Expand Up @@ -695,7 +693,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
private _setFirstOptionActive(): void {
if (this.requireMatch) {
// need to use a timer here to wait until the autocomplete has been opened (end of queue)
Observable.timer().toPromise().then(() => {
toPromise.call(timer()).then(() => {
if (this.focused && this._options && this._options.length > 0) {
// clean up of previously active options
this._options.toArray().forEach((option: MdOption) => {
Expand All @@ -716,7 +714,7 @@ export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueA
*/
private _watchOutsideClick(): void {
if (this._document) {
this._outsideClickSubs = Observable.fromEvent(this._document, 'click').filter((event: MouseEvent) => {
this._outsideClickSubs = RxChain.from(fromEvent(this._document, 'click')).call(filter, (event: MouseEvent) => {
const clickTarget: HTMLElement = <HTMLElement>event.target;
setTimeout(() => {
this._internalClick = false;
Expand Down
7 changes: 6 additions & 1 deletion src/platform/core/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { TdTimeDifferencePipe } from './pipes/time-difference/time-difference.pi
import { TdBytesPipe } from './pipes/bytes/bytes.pipe';
import { TdDigitsPipe } from './pipes/digits/digits.pipe';
import { TdTruncatePipe } from './pipes/truncate/truncate.pipe';
import { RouterPathService } from './services/router.path.service';

const TD_PIPES: Type<any>[] = [
TdTimeAgoPipe,
Expand All @@ -66,6 +65,12 @@ const TD_PIPES: Type<any>[] = [
export { TdTimeAgoPipe, TdTimeDifferencePipe,
TdBytesPipe, TdDigitsPipe, TdTruncatePipe };

/**
* Services
*/

import { RouterPathService } from './services/router-path.service';

@NgModule({
imports: [
FormsModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Component } from '@angular/core';
import { Router, Routes, RoutesRecognized } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { RouterPathService } from './router.path.service';
import { RouterPathService } from './router-path.service';

@Component({
selector: 'fake',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Injectable } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';

import { filter } from 'rxjs/operator/filter';
import { pairwise } from 'rxjs/operator/pairwise';

@Injectable()
export class RouterPathService {
private static _previousRoute: string = '/';
constructor(private _router: Router) {
this._router.events
.filter((e: any) => e instanceof RoutesRecognized)
.pairwise()
.subscribe((e: any[]) => {
pairwise.call(
filter.call(this._router.events, (e: any) => e instanceof RoutesRecognized),
).subscribe((e: any[]) => {
RouterPathService._previousRoute = e[0].urlAfterRedirects;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Subject } from 'rxjs/Subject';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { By } from '@angular/platform-browser';
import { CovalentLoadingModule, LoadingMode, LoadingType, LoadingStrategy, TdLoadingService } from '../loading.module';
import { of } from 'rxjs/observable/of';
import { _catch } from 'rxjs/operator/catch';

describe('Directive: Loading', () => {

Expand Down Expand Up @@ -380,9 +382,9 @@ class TdLoadingNamedErrorStarUntilAsyncTestComponent {
constructor(private _loadingService: TdLoadingService) {}

createObservable(): void {
this.observable = this._subject.asObservable().catch(() => {
this.observable = _catch.call(this._subject.asObservable(), () => {
this._loadingService.resolveAll('name1');
return Observable.of(undefined);
return of(undefined);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/core/media/services/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NgZone, SkipSelf, Optional, Provider } from '@angular/core'
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/observable/fromEvent';
import { fromEvent } from 'rxjs/observable/fromEvent';

@Injectable()
export class TdMediaService {
Expand Down Expand Up @@ -30,7 +30,7 @@ export class TdMediaService {
this._resizing = false;
// we make sure that the resize checking happend outside of angular since it happens often
this._globalSubscription = this._ngZone.runOutsideAngular(() => {
return Observable.fromEvent(window, 'resize').subscribe(() => {
return fromEvent(window, 'resize').subscribe(() => {
// way to prevent the resize event from triggering the match media if there is already one event running already.
if (!this._resizing) {
this._resizing = true;
Expand Down
10 changes: 5 additions & 5 deletions src/platform/core/search/search-input/search-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { trigger, state, style, transition, animate } from '@angular/animations'
import { FormControl } from '@angular/forms';
import { Dir } from '@angular/cdk';
import { MdInputDirective } from '@angular/material';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/skip';
import { debounceTime } from 'rxjs/operator/debounceTime';
import { skip } from 'rxjs/operator/skip';

@Component({
selector: 'td-search-input',
Expand Down Expand Up @@ -88,9 +88,9 @@ export class TdSearchInputComponent implements OnInit {
}

ngOnInit(): void {
this._input._ngControl.valueChanges
.skip(1) // skip first change when value is set to undefined
.debounceTime(this.debounce)
debounceTime.call(
skip.call(this._input._ngControl.valueChanges, 1), // skip first change when value is set to undefined
this.debounce)
.subscribe((value: string) => {
this._searchTermChanged(value);
});
Expand Down
1 change: 0 additions & 1 deletion src/platform/http/http-rest.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { XHRBackend, Response, ResponseOptions, Headers, RequestMethod } from '@
import { MockBackend, MockConnection } from '@angular/http/testing';
import { HttpModule, Http } from '@angular/http';
import { RESTService } from './http-rest.service';
import 'rxjs/Rx';

@Injectable()
export class BasicTestRESTService extends RESTService<any> {
Expand Down
22 changes: 12 additions & 10 deletions src/platform/http/http-rest.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Headers, RequestOptionsArgs, Response, Request } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Subscriber } from 'rxjs/Subscriber';
import { map } from 'rxjs/operator/map';
import { _catch } from 'rxjs/operator/catch';

export interface IRestTransform {
(response: Response): any;
Expand Down Expand Up @@ -49,12 +51,12 @@ export abstract class RESTService<T> {

public query(query?: IRestQuery, transform?: IRestTransform): Observable<any> {
let request: Observable<Response> = this.http.get(this.buildUrl(undefined, query), this.buildRequestOptions());
return request.map((res: Response) => {
return _catch.call(map.call(request, (res: Response) => {
if (transform) {
return transform(res);
}
return this.transform(res);
}).catch((error: Response) => {
}), (error: Response) => {
return new Observable<any>((subscriber: Subscriber<any>) => {
try {
subscriber.error(this.transform(error));
Expand All @@ -67,12 +69,12 @@ export abstract class RESTService<T> {

public get(id: string | number, transform?: IRestTransform): Observable<any> {
let request: Observable<Response> = this.http.get(this.buildUrl(id), this.buildRequestOptions());
return request.map((res: Response) => {
return _catch.call(map.call(request, (res: Response) => {
if (transform) {
return transform(res);
}
return this.transform(res);
}).catch((error: Response) => {
}), (error: Response) => {
return new Observable<any>((subscriber: Subscriber<any>) => {
try {
subscriber.error(this.transform(error));
Expand All @@ -86,7 +88,7 @@ export abstract class RESTService<T> {
public create(obj: T, transform?: IRestTransform): Observable<any> {
let requestOptions: RequestOptionsArgs = this.buildRequestOptions();
let request: Observable<Response> = this.http.post(this.buildUrl(), obj, requestOptions);
return request.map((res: Response) => {
return _catch.call(map.call(request, (res: Response) => {
if (res.status === 201) {
if (transform) {
return transform(res);
Expand All @@ -95,7 +97,7 @@ export abstract class RESTService<T> {
} else {
return res;
}
}).catch((error: Response) => {
}), (error: Response) => {
return new Observable<any>((subscriber: Subscriber<any>) => {
try {
subscriber.error(this.transform(error));
Expand All @@ -109,7 +111,7 @@ export abstract class RESTService<T> {
public update(id: string | number, obj: T, transform?: IRestTransform): Observable<any> {
let requestOptions: RequestOptionsArgs = this.buildRequestOptions();
let request: Observable<Response> = this.http.patch(this.buildUrl(id), obj, requestOptions);
return request.map((res: Response) => {
return _catch.call(map.call(request, (res: Response) => {
if (res.status === 200) {
if (transform) {
return transform(res);
Expand All @@ -118,7 +120,7 @@ export abstract class RESTService<T> {
} else {
return res;
}
}).catch((error: Response) => {
}), (error: Response) => {
return new Observable<any>((subscriber: Subscriber<any>) => {
try {
subscriber.error(this.transform(error));
Expand All @@ -131,7 +133,7 @@ export abstract class RESTService<T> {

public delete(id: string | number, transform?: IRestTransform): Observable<any> {
let request: Observable<Response> = this.http.delete(this.buildUrl(id), this.buildRequestOptions());
return request.map((res: Response) => {
return _catch.call(map.call(request, (res: Response) => {
if (res.status === 200) {
if (transform) {
return transform(res);
Expand All @@ -140,7 +142,7 @@ export abstract class RESTService<T> {
} else {
return res;
}
}).catch((error: Response) => {
}), (error: Response) => {
return new Observable<any>((subscriber: Subscriber<any>) => {
try {
subscriber.error(this.transform(error));
Expand Down
Loading

0 comments on commit cd2a386

Please sign in to comment.