rxjs for angular notes 0

Software Design Patterns:

general, reusable solutions to commonly occurring problems within given context of software design


Observer Pattern:

mainly used in event handing systems and in event driven scenarios

(key part of model-view-controller architectural pattern)


Iterator Pattern:

(in) object-oriented-programming - pattern to go through a container object


Lazy Evaluations:


Pull systems vs. Push systems:

pull systems: The data Consumer determines when it receives data from Producer. Producer is unaware of when the data is delivered. eg: A JavaScript function

push systems: The data Producer determines when to send data to the Consumer. Consumer is unaware of when it will receive data. eg: A JavaScript Promise


popular JavaScript data delivery systems:


Multicasting:

data from single Producer to multiple Consumers


RxJS:

in the context of angular, this is a TypeScript library for asynchronous and event-based code

combines observer pattern and iterator pattern, and functional programming with collections to fill the need for an ideal way of managing sequences of events

RxJS provided angular:

Observable


initializing an Observable:

    import { Observable } from 'rxjs';

    const observable = new Observable(function subscribe(subscriber) {
        const id = setInterval(() => {
            subscriber.next('hi')
        }, 1000);
    });

also, can be initialized using of, from, interval, etc; example:

    const source = from([1, 2, 3]);

source has type Observable


subscribing to an Observable:

    observable.subscribe(function (x) {
        console.log(x)
    });

this is essentially the Observer


executing Observable:

the code within the subscribe function of the Observable – ‘Observable Execution’


disposing an Observable:

Observable executions are allowed to be infinite, so an abort option is made available by RxJS:

    import { from } from 'rxjs';

    const observable = from([10, 20, 30]);
    const subscription = observable.subscribe(function(x) {
        console.log(x)
    });

    // Later:
    subscription.unsubscribe();

    // explicitly define the method of stopping the Observable:
    const observable = new Observable(function subscribe(subscriber) {

        // Keep track of the interval resource
        const intervalId = setInterval(() => {
            subscriber.next('hi');
        }, 1000);

        // Provide a way of canceling and 
        // disposing the interval resource
        return function unsubscribe() {
            clearInterval(intervalId);
        };
        
    });


Subscription

single subscription:

it is the object that represents a disposable resource, such as the execution of the Observable. a subscription has an unsubscribe() method to stop the resource execution

    import { interval } from 'rxjs';

    const observable = interval(1000);
    const subscription = observable.subscribe(function(x) {
        console.log(x);
    });

    // Later:
    // This cancels the ongoing Observable execution which
    // was started by calling subscribe with an Observer.

    subscription.unsubscribe();

multiple subscriptions:

multiple child subscriptions can be added using parentSubscription.add(childSubscription) to enable unsubscribing from many of them at the same time

    import { interval } from 'rxjs';

    const observable1 = interval(400);
    const observable2 = interval(300);

    const subscription = observable1.subscribe(function(x) {
        console.log('first: ' + x);
    });

    const childSubscription = observable2.subscribe(function(x) {
        console.log('second: ' + x);
    });

    subscription.add(childSubscription);

    setTimeout(() => {

        // Unsubscribes BOTH subscription and childSubscription
        subscription.unsubscribe();

    }, 1000);

    // second: 0
    // first: 0
    // second: 1
    // first: 1
    // second: 2

here, interval is a built-in Observable that emits sequential numbers per specified period

child subscriptions are removed by parentSubscription.remove(childSubscription)


Subject

additional Subject variants:


Scheduler

sets conditions for Observable to Observer delivery; it has three components:

example of a Scheduler:

    import { Observable, asyncScheduler } from 'rxjs';
    import { observeOn } from 'rxjs/operators';

    const observable = new Observable( function(observer) {
        observer.next(1);
        observer.next(2);
        observer.next(3);
        observer.complete();
    }).pipe(
        observeOn(asyncScheduler);
    );
    console.log('just before subscribe');

    observable.subscribe({
        next(x) {
            console.log('got value ' + x)
        },
        error(err) {
            console.error('something wrong occurred: ' + err);
        },
        complete() {
            console.log('done');
        }
    });
    console.log('just after subscribe');

    // just before subscribe
    // just after subscribe
    // got value 1
    // got value 2
    // got value 3
    // done

observeOn(asyncScheduler) introduces a proxy Observer between new Observable and the final Observer


Reading:


created: 21 Feb 2019
today's track: Helpless (Ian Flux & Thomas Blofeld Mix) by Myon & Shane 54 ft. Aruna