This commit is contained in:
2023-10-20 23:05:24 +08:00
parent c37086cfcd
commit 6f267109a4
4 changed files with 30 additions and 8 deletions

14
demo.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DEMO</title>
</head>
<body>
<script type="module">
import './src/store.js'
</script>
</body>
</html>

View File

@ -22,7 +22,7 @@
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"express-ws": "^5.0.2", "express-ws": "^5.0.2",
"node-turn": "^0.0.6", "idb": "^7.1.1",
"uuid": "^9.0.1" "node-turn": "^0.0.6"
} }
} }

View File

@ -21,8 +21,8 @@ function appendBuffer(buffer1, buffer2) {
// 读取本地音乐列表并标识为缓存状态(本地缓存) // 读取本地音乐列表并标识为缓存状态(本地缓存)
const database = new IndexedDB('musicDatabase', 1) const database = new IndexedDB('musicDatabase', 1)
await database.store('musicObjectStore') // 音乐(为什么会用这么丑的格式呢) await database.store('musicObjectStore') // 音乐(为什么会用这么丑的格式呢)
await database.store('imageObjectStore') // 图像(为什么会用这么丑的格式呢) //await database.store('imageObjectStore') // 图像(为什么会用这么丑的格式呢)
await database.store('chatsObjectStore') // 聊天室 //await database.store('chatsObjectStore') // 聊天室
// 读取本地音乐列表并标识为缓存状态(本地缓存) // 读取本地音乐列表并标识为缓存状态(本地缓存)
const list = (await database.getAll('musicObjectStore')).map(item => { const list = (await database.getAll('musicObjectStore')).map(item => {

View File

@ -1,4 +1,12 @@
import { v4 } from 'uuid' // 使用封装为支持异步的 IndexedDB
import { openDB, deleteDB, wrap, unwrap } from 'idb'
const storenames = ['music', 'chat', 'user']
const database = await openDB('database', 1, {
upgrade(db) {
storenames.forEach(name => db.createObjectStore(name, { keyPath: 'id' }))
console.log('store created:', storenames)
}
})
export class Store { export class Store {
constructor(data) { constructor(data) {
@ -7,7 +15,7 @@ export class Store {
} }
create(data) { create(data) {
const id = v4() const id = window.crypto.randomUUID()
return { id, ...data } return { id, ...data }
} }
@ -16,7 +24,7 @@ export class Store {
query() {} query() {}
} }
export const music = new Store({ export const musicStore = new Store({
name: '音乐(资源类型)', name: '音乐(资源类型)',
info: '音乐(类型说明)', info: '音乐(类型说明)',
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除 存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除
@ -30,7 +38,7 @@ export const music = new Store({
}] }]
}) })
export const chat = new Store({ export const chatStore = new Store({
name: '聊天(资源类型)', name: '聊天(资源类型)',
info: '聊天(类型说明)', info: '聊天(类型说明)',
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除 存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除