Skip to content

Commit

Permalink
✨ feat: use indexed db to cache songs
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Aug 10, 2024
1 parent dd3bc4b commit 1d54ce8
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@tauri-apps/plugin-dialog": "^2.0.0-rc.0",
"@tauri-apps/plugin-fs": "^2.0.0-rc.0",
"@tauri-apps/plugin-shell": ">=2.0.0-rc.0",
"@tauri-apps/plugin-store": "^2.0.0-rc.0",
"greenlet": "^1.1.0",
"idb-keyval": "^6.2.1",
"jotai": "^2.9.1",
"jotai-optics": "^0.4.0",
"md5": "^2.3.0",
Expand Down
16 changes: 0 additions & 16 deletions src-tauri/Cargo.lock

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

1 change: 0 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ tokio = { version = "1", features = ["full"] }
thiserror = "1.0"
serde_json = "1"
tauri-plugin-fs = "2.0.0-rc.0"
tauri-plugin-store = "2.0.0-rc.0"
tauri-plugin-dialog = "2.0.0-rc.0"
rodio = { git = "https://github.com/SimonShiki/rodio.git", branch = "master", features = ["symphonia-all"] }
souvlaki = "0.6"
Expand Down
7 changes: 1 addition & 6 deletions src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
"core:menu:allow-new",
"core:menu:default",
"fs:default",
"store:allow-get",
"store:allow-set",
"store:allow-save",
"store:allow-load",
"fs:read-all",
{
"identifier": "fs:scope",
Expand All @@ -33,7 +29,6 @@
"fs:write-all",
"core:path:default",
"core:path:allow-resolve",
"dialog:default",
"store:default"
"dialog:default"
]
}
1 change: 0 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub async fn run() {
};

tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_shell::init())
Expand Down
18 changes: 14 additions & 4 deletions src/pages/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Button from '../components/base/button';
import Select from '../components/base/select';
import Input from '../components/base/input';
import { Virtuoso } from 'react-virtuoso';
import type { Song as AbstractSong } from '../jotais/storage';
import type { Song as AbstractSong, Song } from '../jotais/storage';
import { focusAtom } from 'jotai-optics';
import Spinner from '../components/base/spinner';
import * as player from '../utils/player';
Expand Down Expand Up @@ -37,19 +37,29 @@ export default function Local () {
player.addToPlaylist(...list);
player.setCurrentSong(song);
}, [list]);
const handleRandomPlay = useCallback(() => {
const newList = [...list];
player.clearPlaylist();
player.shuffleNewSongs(newList, newList.length);
player.addToPlaylist(...newList);
player.setCurrentSong(newList[0]);
}, [list]);
useEffect(() => {
let ir = sortSongList(_list, sortBy);
let ir: Song<'local'>[] = _list;
if (keyword.trim() !== '') {
ir = filterSongList(list, keyword);
ir = filterSongList(ir, keyword);
}
ir = sortSongList(ir, sortBy);
setList(ir);
}, [_list, keyword, sortBy]);
return (
<main className='flex flex-col gap-6'>
<div className='flex flex-col gap-4 pl-2'>
<span className='color-text-pri font-size-3xl font-500'>Local</span>
<div className='flex flex-row items-center gap-4'>
<Button variant='primary' className='flex flex-row gap-2 items-center'><span className='i-fluent:arrow-shuffle-20-regular w-5 h-5' />Random</Button>
<Button onClick={() => {
handleRandomPlay();
}} variant='primary' className='flex flex-row gap-2 items-center'><span className='i-fluent:arrow-shuffle-20-regular w-5 h-5' />Random</Button>
{!scanned && (
<div className='flex items-center gap-2'>
<Spinner size='size-4' />
Expand Down
10 changes: 4 additions & 6 deletions src/storages/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ export class Local implements AbstractStorage {
}

private async initSongList () {
if (!(await backendStorage.has('cachedLocalSong'))) {
const cachedLocalSong = await backendStorage.get('cachedLocalSong');
if (!cachedLocalSong) {
await this.scan();
return;
}

const cache: Song<'local'>[] = (await backendStorage.get('cachedLocalSong'))!;
if (cache.length < 1) {
if (cachedLocalSong.length < 1) {
await this.scan();
return;
}
this.scanned = true;
this.songList = cache;
this.songList = cachedLocalSong;

// Auto-scan
const { autoScanBehavior } = this.getConfig();
Expand Down Expand Up @@ -94,7 +93,6 @@ export class Local implements AbstractStorage {

this.songList = buffer;
await backendStorage.set('cachedLocalSong', this.songList);
backendStorage.save();

this.scanned = true;
}
Expand Down
11 changes: 3 additions & 8 deletions src/storages/ncm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ export class NCM implements AbstractStorage {
private ncmStorageConfigJotai = focusAtom(storagesConfigJotai, (optic) => optic.prop('ncm')) as unknown as WritableAtom<NCMConfig, [SetStateAction<NCMConfig>], void>;
private songlistJotai?: WritableAtom<Song<'ncm'>[], [Song<'ncm'>[]], void>;
private scannedJotai?: WritableAtom<boolean, boolean[], void>;
private cachedBuffer?: [number, ArrayBuffer];

constructor () {
this.initCookie();
Expand All @@ -269,14 +268,14 @@ export class NCM implements AbstractStorage {
}

private async initSongList () {
if (!(await backendStorage.has('cachedNCMSong'))) {
const cachedNCMSong = await backendStorage.get('cachedNCMSong');
if (!cachedNCMSong) {
await backendStorage.set('cachedNCMSong', []);
return;
}

const cache: Song<'ncm'>[] = (await backendStorage.get('cachedNCMSong'))!;
this.scanned = true;
this.songList = cache;
this.songList = cachedNCMSong;
}

async scan () {
Expand Down Expand Up @@ -351,12 +350,8 @@ export class NCM implements AbstractStorage {
}

async getMusicBuffer (id: number, quality = this.config.defaultQuality) {
if (this.cachedBuffer && this.cachedBuffer[0] === id) {
return this.cachedBuffer[1];
}
const url = await this.getMusicURL(id, quality);
const buffer = await fetchArraybuffer(url);
this.cachedBuffer = [id, buffer];
return buffer;
}

Expand Down
4 changes: 0 additions & 4 deletions src/utils/chunk-transformer.worker.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/local-utitity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Store } from '@tauri-apps/plugin-store';
import * as idb from 'idb-keyval';

export function extractExtName (name: string) {
const result = /(?:\.([^.]+))?$/.exec(name);
return result ? result[1] : '';
}

export const backendStorage = new Store('cache.bin');
export const backendStorage = idb;
2 changes: 1 addition & 1 deletion src/utils/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export function addToPlaylist (...songs: Song<string>[]) {
});
}

function shuffleNewSongs (playlist: Song<string>[], newSongsCount: number) {
export function shuffleNewSongs (playlist: Song<string>[], newSongsCount: number) {
for (let i = playlist.length - newSongsCount; i < playlist.length; i++) {
const j = i + Math.floor(Math.random() * (playlist.length - i));
[playlist[i], playlist[j]] = [playlist[j], playlist[i]];
Expand Down
3 changes: 1 addition & 2 deletions src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function filterSongList<T extends string> (list: Song<T>[], keyword: stri
return list.filter(song => (
song.name.includes(keyword) ||
song.artist?.includes(keyword) ||
song.path?.includes(keyword)
song.album?.includes(keyword)
));
}

12 changes: 5 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1192,13 +1192,6 @@
dependencies:
"@tauri-apps/api" "^2.0.0-rc.0"

"@tauri-apps/plugin-store@^2.0.0-rc.0":
version "2.0.0-rc.0"
resolved "https://registry.yarnpkg.com/@tauri-apps/plugin-store/-/plugin-store-2.0.0-rc.0.tgz#74ec5ea63200a5184bc9455c799cff9fa8a58589"
integrity sha512-KqiEzq6EdRwxrl0/FwyNLwumDBM91xTchdu2a8vfkNub30GuP9z7RskP9ifVRI1gbxfa5TUDi0hKFk/SP7TANQ==
dependencies:
"@tauri-apps/api" "^2.0.0-rc.0"

"@types/babel__core@^7.20.5":
version "7.20.5"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.20.5.tgz#3df15f27ba85319caa07ba08d0721889bb39c017"
Expand Down Expand Up @@ -3014,6 +3007,11 @@ husky@^9.1.4:
resolved "https://registry.yarnpkg.com/husky/-/husky-9.1.4.tgz#926fd19c18d345add5eab0a42b2b6d9a80259b34"
integrity sha512-bho94YyReb4JV7LYWRWxZ/xr6TtOTt8cMfmQ39MQYJ7f/YE268s3GdghGwi+y4zAeqewE5zYLvuhV0M0ijsDEA==

idb-keyval@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/idb-keyval/-/idb-keyval-6.2.1.tgz#94516d625346d16f56f3b33855da11bfded2db33"
integrity sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==

ignore@^5.2.0, ignore@^5.3.1:
version "5.3.1"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
Expand Down

0 comments on commit 1d54ce8

Please sign in to comment.