Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1099 from ueokande/delay-completion
Browse files Browse the repository at this point in the history
Delay before filling completions
  • Loading branch information
ueokande authored Apr 13, 2021
2 parents d80d0f8 + 53450dd commit 14df305
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 345 deletions.
25 changes: 1 addition & 24 deletions src/background/completion/impl/BookmarkRepositoryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import BookmarkRepository, { BookmarkItem } from "../BookmarkRepository";
import { HistoryItem } from "../HistoryRepository";
import PrefetchAndCache from "./PrefetchAndCache";

const COMPLETION_ITEM_LIMIT = 10;

export default class CachedBookmarkRepository implements BookmarkRepository {
private bookmarkCache: PrefetchAndCache<BookmarkItem>;

constructor() {
this.bookmarkCache = new PrefetchAndCache(this.getter, this.filter, 10);
}

queryBookmarks(query: string): Promise<BookmarkItem[]> {
return this.bookmarkCache.get(query);
}

private async getter(query: string): Promise<BookmarkItem[]> {
async queryBookmarks(query: string): Promise<BookmarkItem[]> {
const items = await browser.bookmarks.search({ query });
return items
.filter((item) => item.title && item.title.length > 0)
Expand All @@ -35,15 +23,4 @@ export default class CachedBookmarkRepository implements BookmarkRepository {
url: item.url!,
}));
}

private filter(items: HistoryItem[], query: string) {
return items.filter((item) => {
return query.split(" ").every((keyword) => {
return (
item.title.toLowerCase().includes(keyword.toLowerCase()) ||
item.url!.includes(keyword)
);
});
});
}
}
62 changes: 10 additions & 52 deletions src/background/completion/impl/HistoryRepositoryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
import * as filters from "./filters";
import HistoryRepository, { HistoryItem } from "../HistoryRepository";
import PrefetchAndCache from "./PrefetchAndCache";

const COMPLETION_ITEM_LIMIT = 10;

export default class CachedHistoryRepository implements HistoryRepository {
private historyCache: PrefetchAndCache<browser.history.HistoryItem>;

constructor() {
this.historyCache = new PrefetchAndCache(this.getter, this.filter, 10);
}

export default class HistoryRepositoryImpl implements HistoryRepository {
async queryHistories(keywords: string): Promise<HistoryItem[]> {
const items = await this.historyCache.get(keywords);

const filterOrKeep = <T>(
source: T[],
filter: (items: T[]) => T[],
min: number
): T[] => {
const filtered = filter(source);
if (filtered.length < min) {
return source;
}
return filtered;
};

return [items]
.map((items) =>
filterOrKeep(items, filters.filterByPathname, COMPLETION_ITEM_LIMIT)
)
.map((items) =>
filterOrKeep(items, filters.filterByOrigin, COMPLETION_ITEM_LIMIT)
)[0]
.sort((x, y) => Number(y.visitCount) - Number(x.visitCount))
.slice(0, COMPLETION_ITEM_LIMIT)
.map((item) => ({
title: item.title!,
url: item.url!,
}));
}

private async getter(
keywords: string
): Promise<browser.history.HistoryItem[]> {
const items = await browser.history.search({
text: keywords,
startTime: 0,
Expand All @@ -52,17 +13,14 @@ export default class CachedHistoryRepository implements HistoryRepository {
return [items]
.map(filters.filterBlankTitle)
.map(filters.filterHttp)
.map(filters.filterByTailingSlash)[0];
}

private filter(items: browser.history.HistoryItem[], query: string) {
return items.filter((item) => {
return query.split(" ").every((keyword) => {
return (
item.title!.toLowerCase().includes(keyword.toLowerCase()) ||
item.url!.includes(keyword)
);
});
});
.map(filters.filterByTailingSlash)
.map((pages) => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT))
.map((pages) => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0]
.sort((x, y) => Number(y.visitCount) - Number(x.visitCount))
.slice(0, COMPLETION_ITEM_LIMIT)
.map((item) => ({
title: item.title!,
url: item.url!,
}));
}
}
108 changes: 0 additions & 108 deletions src/background/completion/impl/PrefetchAndCache.ts

This file was deleted.

16 changes: 12 additions & 4 deletions src/background/completion/impl/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const filterByTailingSlash = (items: Item[]): Item[] => {
});
};

const filterByPathname = (items: Item[]): Item[] => {
const filterByPathname = (items: Item[], min: number): Item[] => {
const hash: { [key: string]: Item } = {};
for (const item of items) {
const url = new URL(item.url as string);
Expand All @@ -50,10 +50,14 @@ const filterByPathname = (items: Item[]): Item[] => {
hash[pathname] = item;
}
}
return Object.values(hash);
const filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};

const filterByOrigin = (items: Item[]): Item[] => {
const filterByOrigin = (items: Item[], min: number): Item[] => {
const hash: { [key: string]: Item } = {};
for (const item of items) {
const origin = new URL(item.url as string).origin;
Expand All @@ -65,7 +69,11 @@ const filterByOrigin = (items: Item[]): Item[] => {
hash[origin] = item;
}
}
return Object.values(hash);
const filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};

export {
Expand Down
Loading

0 comments on commit 14df305

Please sign in to comment.