Last active
October 25, 2021 19:42
-
-
Save jhades/8b5560092dfc90c23aeb6fcd1f2dae90 to your computer and use it in GitHub Desktop.
RxJs mapping operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http$ : Observable<Course[]> = this.http.get('/api/courses'); | |
http$ | |
.pipe( | |
tap(() => console.log('HTTP request executed')), | |
map(res => Object.values(res['payload'])) | |
) | |
.subscribe( | |
courses => console.log("courses", courses) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.form.valueChanges | |
.subscribe( | |
formValue => { | |
const httpPost$ = | |
this.http.put(`/api/course/${courseId}`, formValue); | |
httpPost$.subscribe( | |
res => ... handle successful save ... | |
err => ... handle save error ... | |
); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const series1$ = of('a', 'b'); | |
const series2$ = of('x', 'y'); | |
const result$ = concat(series1$, series2$); | |
result$.subscribe(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.form.valueChanges | |
.pipe( | |
concatMap(formValue => this.http.put(`/api/course/${courseId}`, | |
formValue)) | |
) | |
.subscribe( | |
saveResult => ... handle successful save ..., | |
err => ... handle save error ... | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const series1$ = interval(1000).pipe(map(val => val*10)); | |
const series2$ = interval(1000).pipe(map(val => val*100)); | |
const result$ = merge(series1$, series2$); | |
result$.subscribe(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.form.valueChanges | |
.pipe( | |
mergeMap(formValue => | |
this.http.put(`/api/course/${courseId}`, | |
formValue)) | |
) | |
.subscribe( | |
saveResult => ... handle successful save ..., | |
err => ... handle save error ... | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchText$: Observable<string> = | |
fromEvent<any>(this.input.nativeElement, 'keyup') | |
.pipe( | |
map(event => event.target.value), | |
startWith('') | |
) | |
.subscribe(console.log); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchText$: Observable<string> = | |
fromEvent<any>(this.input.nativeElement, 'keyup') | |
.pipe( | |
map(event => event.target.value), | |
startWith(''), | |
debounceTime(400) | |
) | |
.subscribe(console.log); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchText$: Observable<string> = | |
fromEvent<any>(this.input.nativeElement, 'keyup') | |
.pipe( | |
map(event => event.target.value), | |
startWith(''), | |
debounceTime(400), | |
distinctUntilChanged() | |
); | |
const lessons$: Observable<Lesson[]> = searchText$ | |
.pipe( | |
switchMap(search => this.loadLessons(search)) | |
) | |
.subscribe(); | |
function loadLessons(search:string): Observable<Lesson[]> { | |
const params = new HttpParams().set('search', search); | |
return this.http.get(`/api/lessons/${coursesId}`, {params}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fromEvent(this.saveButton.nativeElement, 'click') | |
.pipe( | |
concatMap(() => this.saveCourse(this.form.value)) | |
) | |
.subscribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fromEvent(this.saveButton.nativeElement, 'click') | |
.pipe( | |
exhaustMap(() => this.saveCourse(this.form.value)) | |
) | |
.subscribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'course-dialog', | |
templateUrl: './course-dialog.component.html' | |
}) | |
export class CourseDialogComponent implements AfterViewInit { | |
form: FormGroup; | |
course:Course; | |
@ViewChild('saveButton') saveButton: ElementRef; | |
constructor( | |
private fb: FormBuilder, | |
private dialogRef: MatDialogRef<CourseDialogComponent>, | |
@Inject(MAT_DIALOG_DATA) course:Course) { | |
this.course = course; | |
this.form = fb.group({ | |
description: [course.description, | |
Validators.required], | |
category: [course.category, Validators.required], | |
releasedAt: [moment(), Validators.required], | |
longDescription: [course.longDescription, | |
Validators.required] | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't 07.ts, 08.ts and 09.ts be doing the subscribe as a separate statement? Now it seems like they're trying to assign a Subscription to a const typed as an Observable.
For example