-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.component.ts
143 lines (120 loc) · 4.08 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { Comments } from './Model/comments';
import { UserService } from './user.service';
import { Component, OnDestroy, ChangeDetectionStrategy, OnInit } from '@angular/core';
import { timer, of, forkJoin, Observable, Subject, interval, concat, from, fromEvent } from 'rxjs';
import { switchMap, takeUntil, catchError, tap, debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { filter, map, flatMap } from 'rxjs/operators';
import { Post } from './Model/post';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
const states = ['Alabama', 'Alaska', 'American Samoa', 'Arizona', 'Arkansas', 'California', 'Colorado',
'Connecticut', 'Delaware', 'District Of Columbia', 'Federated States Of Micronesia', 'Florida', 'Georgia',
'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
'Marshall Islands', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Northern Mariana Islands', 'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Islands', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'angularrxjs';
items: {};
post: Post;
public searchTerm: string;
constructor(private UserService: UserService, private formBuilder: FormBuilder) {
}
ngOnInit() {
this.searchBook()
}
postId = new FormControl();
bookForm: FormGroup = this.formBuilder.group({
postId: this.postId
});
public searchBook() {
this.postId.valueChanges.pipe(
debounceTime(500),
switchMap(id => {
console.log(id);
return this.UserService.getpost(id);
})
).subscribe(res => console.log(res) );
}
public test() {
interval(1000)
.pipe(
flatMap(() => this.UserService.getPosts())
)
.subscribe(
data => {
this.items = data
console.log(this.items);
},
error => {
console.log('error fetching data')
}
)
}
rxjs() {
const squareOf2 = of(1, 2, 3, 4, 5, 6)
.pipe(
filter(num => num % 2 === 0),
map(num => num * num)
);
squareOf2.subscribe((num) => console.log(num));
}
public SwitchMap() {
this.UserService.getPosts().pipe(
switchMap(posts => {
return this.UserService.comments().pipe(
tap(comments => {
console.log('posts are', posts)
console.log('Comments: are', comments)
})
)
})
)
.subscribe(
data => {
this.items = data
console.log(this.items);
},
error => {
console.log('error fetching data')
}
)
}
public Concat() {
const commentsobs: Observable<Comments[]> = this.UserService.comments();
const postsobs: Observable<Post[]> = this.UserService.getPosts();
const joined = concat(commentsobs, postsobs);
joined.subscribe(res => console.log('joined dta', res));
}
public Forkjoin() {
const commentsobs: Observable<Comments[]> = this.UserService.comments();
const postsobs: Observable<Post[]> = this.UserService.getPosts();
const joined = forkJoin(commentsobs, postsobs);
joined.subscribe(res => console.log('joined dta', res));
}
/**
* obs
*/
public obs() {
const secondsCounter = interval(1000);
// Subscribe to begin publishing values
secondsCounter.subscribe(n =>
console.log(`It's been ${n} seconds since subscribing!`));
}
// search = (text$: Observable<string>) : Observable<Comments[]> {
// return text$.pipe(
// switchMap(searchTerm =>{
// if(!searchTerm){
// return [];
// }
// return this.UserService.comments(searchTerm);
// })
// )
// }
}