Compare commits

...

3 Commits

Author SHA1 Message Date
6f267109a4 demo 2023-10-20 23:05:24 +08:00
c37086cfcd DEBUGL 2023-10-20 21:54:20 +08:00
e5c9623092 初始化存储 2023-10-20 20:34:13 +08:00
6 changed files with 74 additions and 10 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,6 +22,7 @@
"dependencies": {
"express": "^4.18.2",
"express-ws": "^5.0.2",
"idb": "^7.1.1",
"node-turn": "^0.0.6"
}
}

1
src/communication.js Normal file
View File

@ -0,0 +1 @@
// 通信

View File

@ -17,7 +17,7 @@ export default class IndexedDB {
}
request.onupgradeneeded = (event) => {
const db = event.target.result
if (!db.objectStoreNames.contains('todo')) {
if (!db.objectStoreNames.contains(name)) {
db.createObjectStore(name, { keyPath: 'id' })
console.log('store created:', name)
}

View File

@ -1,5 +1,4 @@
import 'virtual:windi.css'
//import 'virtual:windi-devtools'
import IndexedDB from './indexeddb.js'
import MusicList from './music.js'
@ -19,15 +18,13 @@ function appendBuffer(buffer1, buffer2) {
return tmp.buffer
}
//// 读取本地图像
//const imageStore = new IndexedDB('musicDatabase', 1, 'imageObjectStore')
//await imageStore.open()
// 读取本地音乐列表并标识为缓存状态(本地缓存)
const database = new IndexedDB('musicDatabase', 1)
const musicStore = await database.store('musicObjectStore')
await database.store('musicObjectStore') // 音乐(为什么会用这么丑的格式呢)
//await database.store('imageObjectStore') // 图像(为什么会用这么丑的格式呢)
//await database.store('chatsObjectStore') // 聊天室
//// 读取本地音乐列表并标识为缓存状态(本地缓存)
// 读取本地音乐列表并标识为缓存状态(本地缓存)
const list = (await database.getAll('musicObjectStore')).map(item => {
return { save: true, ...item }
})
@ -36,7 +33,6 @@ const list = (await database.getAll('musicObjectStore')).map(item => {
const name = localStorage.getItem('username') ?? '匿'
const avatar = localStorage.getItem('avatar') ?? '/favicon.ico'
// 初始化客户端列表
const clientList = new ClientList({
name,
@ -56,7 +52,6 @@ const clientList = new ClientList({
}
})
// 初始化音乐列表(加入本地缓存)
const musicList = new MusicList({
list,

53
src/store.js Normal file
View File

@ -0,0 +1,53 @@
// 使用封装为支持异步的 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 {
constructor(data) {
this.name = data.name
this.info = data.info
}
create(data) {
const id = window.crypto.randomUUID()
return { id, ...data }
}
delete(id) {}
query() {}
}
export const musicStore = new Store({
name: '音乐(资源类型)',
info: '音乐(类型说明)',
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除
list: [{
id: 'uuid',
name: 'Dear big sisterremix ver.mp3',
arrayBuffer: 'ArrayBuffer',
createdAt: 1580000000000, // 创建时间
updatedAt: 1580000000000, // 更新时间
lastUsedAt: 1580000000000, // 最后使用时间
}]
})
export const chatStore = new Store({
name: '聊天(资源类型)',
info: '聊天(类型说明)',
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除
list: [{
id: 'uuid',
name: 'Dear big sisterremix ver.mp3',
arrayBuffer: 'ArrayBuffer',
createdAt: 1580000000000, // 创建时间
updatedAt: 1580000000000, // 更新时间
lastUsedAt: 1580000000000, // 最后使用时间
}]
})