1import { Component, OnInit } from '@angular/core';
2import { Observable, of} from 'rxjs';
3import { map, filter, tap } from 'rxjs/operators'
4
5
6@Component({
7 selector: 'app-root',
8 templateUrl: './app.component.html',
9 styleUrls: ['./app.component.css']
10})
11export class AppComponent implements OnInit {
12
13 obs = new Observable((observer) => {
14 observer.next(1)
15 observer.next(2)
16 observer.next(3)
17 observer.next(4)
18 observer.next(5)
19 observer.complete()
20 }).pipe(
21 filter(data => data > 2), //filter Operator
22 map((val) => {return val as number * 2}), //map operator
23 )
24
25 data = [];
26
27 ngOnInit() {
28 this.obs1.subscribe(
29 val => {
30 console.log(this.data)
31 }
32 )
33 }
34
35}
1
2 content_copy
3
4 // Create an Observable that will start listening to geolocation updates
5// when a consumer subscribes.
6const locations = new Observable((observer) => {
7 let watchId: number;
8
9 // Simple geolocation API check provides values to publish
10 if ('geolocation' in navigator) {
11 watchId = navigator.geolocation.watchPosition((position: Position) => {
12 observer.next(position);
13 }, (error: PositionError) => {
14 observer.error(error);
15 });
16 } else {
17 observer.error('Geolocation not available');
18 }
19
20 // When the consumer unsubscribes, clean up data ready for next subscription.
21 return {
22 unsubscribe() {
23 navigator.geolocation.clearWatch(watchId);
24 }
25 };
26});
27
28// Call subscribe() to start listening for updates.
29const locationsSubscription = locations.subscribe({
30 next(position) {
31 console.log('Current Position: ', position);
32 },
33 error(msg) {
34 console.log('Error Getting Location: ', msg);
35 }
36});
37
38// Stop listening for location after 10 seconds
39setTimeout(() => {
40 locationsSubscription.unsubscribe();
41}, 10000);
42
1import {Observable} from 'rxjs';
2const foo = new Observable (subscriber => {
3 subscriber.next (42);
4 subscriber.next (100);
5
6
7});
8foo.subscriber(x => {
9 console.log(x);
10});
11