diff options
Diffstat (limited to 'src/background/completion/impl/BookmarkRepositoryImpl.ts')
-rw-r--r-- | src/background/completion/impl/BookmarkRepositoryImpl.ts | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/background/completion/impl/BookmarkRepositoryImpl.ts b/src/background/completion/impl/BookmarkRepositoryImpl.ts index 58df129..f34c7d1 100644 --- a/src/background/completion/impl/BookmarkRepositoryImpl.ts +++ b/src/background/completion/impl/BookmarkRepositoryImpl.ts @@ -1,11 +1,21 @@ -import { injectable } from "tsyringe"; import BookmarkRepository, {BookmarkItem} from "../BookmarkRepository"; +import {HistoryItem} from "../HistoryRepository"; +import PrefetchAndCache from "./PrefetchAndCache"; const COMPLETION_ITEM_LIMIT = 10; -@injectable() -export default class BookmarkRepositoryImpl implements BookmarkRepository { - async queryBookmarks(query: string): Promise<BookmarkItem[]> { +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[]> { const items = await browser.bookmarks.search({query}); return items .filter(item => item.title && item.title.length > 0) @@ -25,4 +35,12 @@ export default class BookmarkRepositoryImpl implements BookmarkRepository { url: item.url!!, })); } + + private filter(items: HistoryItem[], query: string) { + return items.filter(item => { + return query.split(' ').some(keyword => { + return item.title.toLowerCase().includes(keyword.toLowerCase()) || item.url.includes(keyword) + }); + }) + }; } |