Address: stackblitz.com/

Create a new Angular app of type RxJS:

StackBlitz automatically generates application templates:

The source code:

import { of } from 'rxjs'; 
import { map } from 'rxjs/operators';


const source = of('World').pipe(
  map(x= > `Hello ${x}! `)); source.subscribe(console.log);
Copy the code
import { Observable } from "rxjs";

const stream$ = new Observable(subscriber= > {
  setTimeout(() = > {
    subscriber.next([1.2.3]);
  }, 500);
  setTimeout(() = > {
    subscriber.next({ a: 1000 });
  }, 1000);
  setTimeout(() = > {
    subscriber.next("end");
  }, 3000);
  setTimeout(() = > {
    subscriber.complete();
  }, 4000);
});

/ / start the flow

class Subscriber {
  constructor(private name: string) {}
  complete = () = > console.log("name: " + this.name + " done");

  next = v= >
    console.log("time: " + Date.now() + " name: " + this.name + " value: " + v);
  error = () = > console.log("error");
}

const s1 = new Subscriber("Jerry");
const s2 = new Subscriber("Tom");

stream$.subscribe(s1);
stream$.subscribe(s2);
Copy the code

Run output:

Observables can repeatedly subscribe without interfering with each other.

More of Jerry’s original articles can be found in “Wang Zixi” :