Example #1
0
    // fromArray pattern
    constructor2() {
        console.log(new Observable());
        
        var startDates = [];
        var startDate = new Date();
        
        for (var day = -2; day <=2; day++) {
            var date = new Date(
                startDate.getFullYear(),
                startDate.getMonth(),
                startDate.getDate() + day);
            startDates.push(date);
        }

        //console.log(startDates);  
        
        var d = Observable.fromArray(startDates)
            .map(date => {
                console.log("Getting deals for date" + date);
                return [1, 2, 3];
            });
            
        d.subscribe(x => console.log(x));        
        
        
    }    
 beforeEach(inject(function(phones, $rootScope, $controller) {
   spyOn(phones, 'query').and.returnValue(Observable.fromArray([
     [{name: 'Nexus S'}, {name: 'Motorola DROID'}]
   ]));
   scope = $rootScope.$new();
   ctrl = $controller('PhoneListCtrl', {$scope: scope});
 }));
  it('should fetch phone detail', function() {
    spyOn(phones, 'get').and.returnValue(Observable.fromArray([xyzPhoneData()]));

    let ctrl = $controller('PhoneDetailCtrl', {$scope: scope});

    expect(phones.get).toHaveBeenCalledWith('xyz');
    expect(ctrl.phone).toEqual(xyzPhoneData());
  });
Example #4
0
    constructor3() {
        console.log(new Observable());
        
        var d1 = Observable.empty();
        d1.subscribe(x => console.log(x));

        var d2 = Observable.range(1, 5);
        d2.subscribe(x => console.log(x));

        var d3 = Observable.fromArray([1,2,3]);
        d3.subscribe(x => console.log(x));

        var d4 = Observable.of(1);
        d4.subscribe(x => console.log(x));

        var d5 = Observable.of(1,2,3);
        d5.subscribe(x => console.log(x));

        var d6 = Observable.of([1,2,3]);
        d6.subscribe(x => console.log(x));
                
    }
 query():Observable<Phone[]> {
   return Observable.fromArray([
     [{name: 'Nexus S'}, {name: 'Motorola DROID'}]
   ])
 }
 get(id):Observable<Phone> {
   return Observable.fromArray([xyzPhoneData()]);
 }
    constructor() {
        // Test various forms of Observable.
        console.log("fromArray [1,2,3]");
        var observable = Observable.fromArray([1, 2, 3])
            .subscribe(x => console.log(x));

        console.log("of [1,2,3]");
        observable = Observable.of([1,2,3])
            .subscribe(x => console.log(x));

        console.log("of 1,2,3");
        observable = Observable.of(1,2,3)
            .subscribe(x => console.log(x));

        console.log("empty");
        observable = Observable.empty()
            .subscribe(x => console.log(x));

        console.log("range 1,5");
        observable = Observable.range(1,5)
            .subscribe(x => console.log(x));

        console.log("completion function");
        observable = Observable.of(1)
            .subscribe(
                x => console.log(x),
                err => console.error(err),
                () => console.log("completed")
            );


        // console.log("interval 1000");
        // var interval = Observable.interval(10000)
        //     .flatMap(x => {
        //         // call server
        //         return Observable.of([1,2,3]);
        //     })
        //     .subscribe(x => console.log(x));

        // Simulating error.
        console.log("error handling");
        var errObs = Observable
            .throw(new Error("ahh!"))
            .subscribe(
                x => console.log(x), // success
                err => console.error(err) // error
            );

        // simulate a call which fails twice then succeeds.
        console.log("error handling with retry");
        var counter = 0;
        var ajaxCall = Observable.of('url')
            .flatMap(() => {
                if(++counter < 2)
                    return Observable.throw(new Error("ahh!"));

                return Observable.of([1,2,3]);
            })
            .retry(3)
            .subscribe(x => console.log(x), err => console.error(err));

        console.log("error handling with catch");
        var catchObs = Observable
            .throw(new Error("ahh!"))
            .catch(err => {
                return Observable.of([1,2,3]);
            })
            .subscribe(x => console.log(x));

        console.log("timeout");
        var timeout = Observable
            .of([1,2,3])
            .delay(5000)
            .timeout(100)
            .subscribe(x => console.log(x), err => console.error(err));

        // join two observables. Returns an array of the results
        // [o1.results, o2.results] after both complete. Map to something nicer.
        console.log("forkJoin");
        var o1 = Observable.of({
            userId: 1,
            username: 'something'
        }).delay(2000);
        var o2 = Observable.of([1,2,3]).delay(1500);
        Observable
            .forkJoin(o1,o2)
            .map(data => {
                {
                    return {
                        user: data[0],
                        stream: data[1]
                    };
                }
            })
            .subscribe(x => console.log(x));
    }
import { Observable } from 'rxjs/Rx';

Observable.fromArray(['AdriĆ ', 'Jen', 'Sergi']).subscribe(function(data){
    console.log(data);
},
function(err){
    console.log(err)
}, function(){
    console.log('completed')
})
    constructor(fb:FormBuilder) {
        this.formModel = fb.group({
            search: []
        });

        var searchControl = this.formModel.find('search');
        searchControl.valueChanges
            .debounceTime(400)
            .map(str => (str.replace(' ', '-')))
            .subscribe(x => console.log(x));


        /** travel search */
        var startDates = [];
        var startDate = new Date();

        for (var day = -2; day < 2; day++) {
            var date = new Date(
                startDate.getFullYear(),
                startDate.getMonth(),
                startDate.getDate() + day
            );
            startDates.push(date);
        }

        Observable
            .fromArray(startDates)
            .map(date => {
                console.log("Getting deals for date: " + date);
                return [1, 2, 3];
            })
            .subscribe(x => console.log(x));

        /** interval */
        // Observable
        //     .interval(1000)
        //     .subscribe(x => console.log(x))

        // Observable
        //     .interval(1000)
        //     .flatMap(x => {
        //         console.log("Calling server to get latest news..");
        //         return Observable.of([1, 2, 3]);
        //     })
        //     .subscribe(news => console.log(news));

        /** delay */
        var userStream = Observable.of({
            userId: 1, userName: 'chanu'
        }).delay(2000);

        /** join */
        var tweetsStream = Observable.of([1, 2, 3]).delay(1500);

        Observable
            .forkJoin(userStream, tweetsStream)
            .map(joined => Object({user: joined[0], tweets: joined[1]}))
            .subscribe(result => console.log(result));

        /** error handling */
        var observable = Observable.throw(new Error("Something failed!"));
        observable
            .subscribe(
                x => console.log(x),
                error => console.log(error));

        /** retry */
        var counter = 0;
        var ajaxCall = Observable
            .of('url')
            .flatMap(() => {
                if (++counter < 2)
                    return Observable.throw(new Error("Request failed!"));
                return Observable.of([1, 2, 3]);
            })
            .retry(3)
            .subscribe(
                x => console.log("Response from retry: " + x),
                error => console.log(error)
            );

        /** catching and continuing */
        var remoteDataStream = Observable.throw(new Error("Something is wrong!"));
        remoteDataStream.catch(err => {
                var localDataStream = Observable.of([1, 2, 3]);
                return localDataStream;
            })
            .subscribe(x => console.log("Catching and continuing: " + x));

        /** Completed */
        Observable
            .throw(new Error("error"))
            .subscribe(
                x => console.log(x),
                error => console.log(error),
                () => console.log("Completed"));

        Observable
            .fromArray([1, 2, 3])
            .subscribe(
                x => console.log(x),
                error => console.log(error),
                () => console.log("Completed"));


    }