From f15685f02f94184df4deb3a6d44187f809b9d996 Mon Sep 17 00:00:00 2001 From: Alex Kozack Date: Thu, 3 Jun 2021 17:51:40 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=B4=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=BA=20=D0=BF=D0=BE=20=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B8=20=D0=BF=D1=80=D0=BE=D1=81=D0=BC=D0=BE?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B2=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Работает не лучшим образом. Вынужденно загружает вообще всю историю просмотров, чтобы получить несколько последних элементов, как как нативные возможности idb не позволяют получить N записей по индексу с конца. Смотри: https://github.com/jakearchibald/idb/issues/176 https://github.com/w3c/IndexedDB/issues/130 --- .../renderer/src/components/HomeSearch.vue | 61 +++++++++++++++---- .../renderer/src/utils/history-views/index.ts | 30 +++++---- .../renderer/src/utils/videoProvider/index.ts | 2 +- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/packages/renderer/src/components/HomeSearch.vue b/packages/renderer/src/components/HomeSearch.vue index 8bdec84e..f21d6677 100644 --- a/packages/renderer/src/components/HomeSearch.vue +++ b/packages/renderer/src/components/HomeSearch.vue @@ -1,6 +1,6 @@ @@ -43,9 +61,11 @@ import {computed, defineComponent, ref, watch} from 'vue'; import {useRouter} from 'vue-router'; import {getSeriesId} from '/@shared/utils/getSeriesId'; +import type {Series} from '/@/utils/videoProvider'; import {getEpisodes, getSeries} from '/@/utils/videoProvider'; import {asyncComputed} from '@vueuse/core'; import WinIcon from '/@/components/WinIcon.vue'; +import {getHistoryItems} from '/@/utils/history-views'; export default defineComponent({ @@ -54,10 +74,31 @@ export default defineComponent({ setup() { const router = useRouter(); - const defaultSearchText = import.meta.env.MODE === 'development' ? 'https://shikimori.org/animes/14719-jojo-no-kimyou-na-bouken-tv' : ''; - const searchText = ref(defaultSearchText); + const searchText = ref(''); const animeID = computed(() => getSeriesId(searchText.value)); + const history = ref([]); + + getHistoryItems() + .then(items => Promise.all(items.map((i) => getSeries(i.seriesId)))) + .then(series => history.value = series.filter((s?: T): s is T => s !== undefined)); + + + const onDatalistOptionSelect = () => { + if (history.value.length === 0) { + return; + } + + const searchTextTrimmed = searchText.value.trim(); + const target = history.value.find(i => i && i.title.toLowerCase() === searchTextTrimmed.toLowerCase()); + if (!target) { + return; + } + + return open(target.id); + }; + + /** * Если удалось определить ID аниме -- выполнить загрузку серий, чтобы они кэшировались */ @@ -82,15 +123,13 @@ export default defineComponent({ */ const onSearch = () => { if (animeID.value) { - router.push({name: 'Watch', params: {seriesId: animeID.value}}); + open(animeID.value); } }; - return {onSearch, searchText, title}; + const open = (seriesId: number) => router.push({name: 'Watch', params: {seriesId}}); + + return {onSearch, onDatalistOptionSelect, searchText, title, history}; }, }); - - diff --git a/packages/renderer/src/utils/history-views/index.ts b/packages/renderer/src/utils/history-views/index.ts index cd2f9bcd..c4a145ea 100644 --- a/packages/renderer/src/utils/history-views/index.ts +++ b/packages/renderer/src/utils/history-views/index.ts @@ -6,7 +6,7 @@ import {getUserRate, isLoggedIn, saveUserRate} from '/@/utils/shikimori-api'; // type HistoryViewsItemStates = 'planned' | 'watching' | 'rewatching' | 'completed' | 'on_hold' | 'dropped' -interface HistoryViewsItem { +export interface HistoryViewsItem { seriesId: number, episode: { number: number, @@ -32,9 +32,10 @@ interface HistoryViews extends DBSchema { history: { value: HistoryViewsItem; key: HistoryViewsItem['seriesId']; - // indexes: { - // 'by-state': HistoryViewsItem['state'] - // } + indexes: { + 'by-updated-at': number + // 'by-state': HistoryViewsItem['state'] + } }; meta: Meta } @@ -52,16 +53,16 @@ function getDB() { return dbPromise; } - dbPromise = openDB('history-views', 1, { - upgrade(db, oldVersion: number) { + dbPromise = openDB('history-views', 2, { + upgrade(db, oldVersion, _newVersion, transaction) { if (oldVersion < 1) { - db - .createObjectStore('history', {keyPath: 'seriesId'}); - // .createIndex('by-state', 'state'); - - + db.createObjectStore('history', {keyPath: 'seriesId'}); db.createObjectStore('meta'); } + + if (oldVersion < 2) { + transaction.objectStore('history').createIndex('by-updated-at', 'updated_at'); + } }, }); @@ -137,3 +138,10 @@ export async function getViewHistoryItem(seriesId: number): Promise { + return getDB() + .then(db => db.getAllFromIndex('history', 'by-updated-at')) + .then(items => items.splice(-limit).reverse()); +} diff --git a/packages/renderer/src/utils/videoProvider/index.ts b/packages/renderer/src/utils/videoProvider/index.ts index f001adbb..248f10c4 100644 --- a/packages/renderer/src/utils/videoProvider/index.ts +++ b/packages/renderer/src/utils/videoProvider/index.ts @@ -70,7 +70,7 @@ export interface TranslationAuthor { /** * Возвращает {@link Series} по его MyAnimeListID */ -export function getSeries(malId: number): Promise | undefined> { +export function getSeries(malId: number): Promise { return deDuplicatedRequest( `series-${malId}`, () => provider.getSeries(malId).then(s => s === undefined ? s : readonly(s)),