Skip to content

Commit c687451

Browse files
committedMar 6, 2023
Create custom operator for live search
1 parent 447892a commit c687451

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed
 

‎src/app/app.component.ts

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
import { Component } from '@angular/core';
2+
import { Observable, Subject, switchMap } from 'rxjs';
3+
import { liveSearchOperator } from './live-search-operator';
24

35
@Component({
46
selector: 'app-root',
5-
template: `
6-
<!--The content below is only a placeholder and can be replaced.-->
7-
<div style="text-align:center" class="content">
8-
<h1>
9-
Welcome to {{title}}!
10-
</h1>
11-
<span style="display: block">{{ title }} app is running!</span>
12-
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
13-
</div>
14-
<h2>Here are some links to help you start: </h2>
15-
<ul>
16-
<li>
17-
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
18-
</li>
19-
<li>
20-
<h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2>
21-
</li>
22-
<li>
23-
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
24-
</li>
25-
</ul>
26-
<router-outlet></router-outlet>
27-
`,
7+
template: ``,
288
styles: []
299
})
3010
export class AppComponent {
31-
title = 'rxjs-custom-operator';
11+
searchTerm$ = new Subject<string>();
12+
user$!: Observable<any>;
13+
14+
constructor() {
15+
this.user$ = this.searchTerm$.pipe(
16+
liveSearchOperator(
17+
(term: string) => term.length >= 4,
18+
500,
19+
(prev, curr) => prev === curr
20+
),
21+
//switchMap((term: string) => this.yourService.filterUser(term))
22+
);
23+
}
24+
25+
search(event: Event): void {
26+
const element = event.currentTarget as HTMLInputElement;
27+
this.searchTerm$.next(element.value);
28+
}
3229
}

‎src/app/live-search-operator.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { OperatorFunction, debounceTime, distinctUntilChanged, Observable, filter } from 'rxjs';
2+
const DEFAULT_FILTER = 3;
3+
const DEFAULT_DEBOUNCE_TIME = 300;
4+
5+
export function liveSearchOperator<T>(
6+
filterFn: (value: T) => boolean,
7+
debounceTimeFn: number,
8+
distinctFn: (value: T, otherValue: T) => boolean): OperatorFunction<T, T> {
9+
return (source: Observable<T>) =>
10+
source.pipe(
11+
filter(filterFn || DEFAULT_FILTER),
12+
debounceTime(debounceTimeFn || DEFAULT_DEBOUNCE_TIME),
13+
distinctUntilChanged(distinctFn)
14+
);
15+
}

0 commit comments

Comments
 (0)