Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: improve performance #18

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 27 additions & 81 deletions demo/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
inputs = {
nixpkgs.follows = "nixpkgs";
search.follows = "search";
flake-utils.follows = "flake-utils";
};
};
nixvim = {
Expand Down
2 changes: 1 addition & 1 deletion nix/frontend.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ stdenv.mkDerivation (finalAttrs: {

pnpmDeps = pnpm_8.fetchDeps {
inherit (finalAttrs) pname version src;
hash = "sha256-0t9qJ2Cgq2mbiVnFbd61knlzdqQ68RIIW+toa21h8NU=";
hash = "sha256-ETUP7CJeKX30nDZk+hWxO84V4P25/cn3fZiXeTT0bAI=";
};

nativeBuildInputs = [ nodejs pnpm_8.configHook ];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@angular-devkit/build-angular": "^18.0.5",
"@angular/cli": "^18.0.5",
"@angular/compiler-cli": "^18.0.0",
"@angular/language-service": "^18.0.5",
"@types/jasmine": "~5.1.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ <h1>NüschtOS Search</h1>
<div class="card">
<feel-text-field [formControl]="search" label="Search"></feel-text-field>

<ul>
<li *ngFor="let option of results | async">
<a [routerLink]="[]" [queryParams]="{option: option.name}" queryParamsHandling="merge" routerLinkActive="active"><code>{{option.name}}</code></a>
</li>
</ul>
<ng-container *ngIf="(results | async) as options">
<ul>
<li *ngFor="let option of options; trackBy trackBy">
<a [routerLink]="[]" [queryParams]="{option: option.name}" queryParamsHandling="merge"
routerLinkActive="active">
<code>{{option.name}}</code>
</a>
</li>
</ul>
<p *ngIf="options.length == 500">Only showing the first 500 results. Make your search term more concise.</p>
</ng-container>
</div>
</aside>

Expand Down
5 changes: 0 additions & 5 deletions src/app/app.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@

li {
list-style-type:none;
/* display: block;

&:not(:first-child) {
border-top: 0.1rem solid $c-secondary-l;
}*/

a {
display: block;
Expand Down
19 changes: 12 additions & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { AfterViewInit, ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { Router, RouterLink, RouterOutlet } from '@angular/router';
import { SearchService } from './core/search.service';
import { of, switchMap } from 'rxjs';
import { AsyncPipe, NgFor } from '@angular/common';
import { Option, SearchService } from './core/search.service';
import { switchMap } from 'rxjs';
import { AsyncPipe, NgFor, NgIf } from '@angular/common';
import { TextFieldComponent } from "@feel/form";
import { OptionComponent } from './pages/option/option.component';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink, OptionComponent],
imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink, OptionComponent, NgIf],
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
Expand All @@ -25,7 +25,7 @@ export class AppComponent implements AfterViewInit {
if (q && q.length > 0) {
return this.searchService.search(q);
} else {
return of([]);
return this.searchService.all();
}
}),
);
Expand All @@ -36,10 +36,15 @@ export class AppComponent implements AfterViewInit {
) {
}

ngAfterViewInit(): void {
public ngAfterViewInit(): void {
this.search.setValue(AppComponent.getQuery());
}

private static getQuery(): string | null {
return new URL(location.href).searchParams.get("query");
}

protected trackBy(_idx: number, { name }: Option): string {
return name;
}
}
35 changes: 32 additions & 3 deletions src/app/core/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,41 @@ export class SearchService {

public search(query: string): Observable<Option[]> {
this.update();
return this.data.pipe(map(options => options.filter(option => {
return option.name.includes(query)
})));
return this.data.pipe(map(options => {
const result = [];
let i = 0;
for (const option of options) {
if (option.name.includes(query)) {
result.push(option);
i++;
// TODO: pagination
if (i === 500) {
return result;
}
}
}
return result;
}));
}

public getByName(name: string): Observable<Option | undefined> {
this.update();
return this.data.pipe(map(options => options.find(option => option.name === name)));
}

public all(): Observable<Option[]> {
return this.data.pipe(map(options => {
const result = [];
let i = 0;
for (const option of options) {
result.push(option);
i++;
// TODO: pagination
if (i === 500) {
return result;
}
}
return result;
}));
}
}
Loading