唯一ID?

This commit is contained in:
2023-10-21 04:21:27 +08:00
parent ea397c735b
commit e6af4f0fc7
4 changed files with 30 additions and 105 deletions

View File

@ -1,30 +0,0 @@
<!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 { musicStore, chatStore } from './src/store.js'
console.log(musicStore.name)
console.log(chatStore.name)
// 检查过期的消息自动销毁
// 查询消息列表
const element = document.createElement('div')
element.innerHTML = `
<h1>DEMO</h1>
<h2>音乐</h2>
<div id="music">${musicStore.name}</div>
<h2>聊天</h2>
<div id="chat">${chatStore.name}</div>
`
document.body.appendChild(element)
</script>
</body>
</html>

View File

@ -1,8 +1,10 @@
import { get, set, del, update, createStore, values } from 'idb-keyval'
import { List, ListItem, Avatar, Span, Dialog, Button, Input } from './weigets.js'
export default class ClientList {
constructor({ channels = {}, EventListeners = {}, name: username, onexit }) {
this.event = { onexit }
this.store = createStore(`db-user`, `store-user`)
this.channels = channels
this.EventListeners = EventListeners
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
@ -194,10 +196,10 @@ export default class ClientList {
}
this.websocket = linkStart()
// 也插入自己的信息
const avatar = localStorage.getItem('avatar')
this.push({ id: 'self', name: username, avatar }, true)
this.我的帐户()
this.DEBUG()
}
async DEBUG() {
// 监听键盘Esc按下, 如果全局没有焦点则显示调试信息, 如果在调试信息显示期间弹起Esc则隐藏调试信息
let debug = false
let debugElement = Dialog({
@ -225,6 +227,30 @@ export default class ClientList {
}
})
}
async 我的帐户() {
if (!localStorage.getItem('id')) {
localStorage.setItem('id', window.crypto.randomUUID())
}
if (!localStorage.getItem('username')) {
localStorage.setItem('username', '匿')
}
if (!localStorage.getItem('avatar')) {
localStorage.setItem('avatar', '/favicon.ico')
}
const id = localStorage.getItem('id')
const username = localStorage.getItem('username')
const avatar = localStorage.getItem('avatar')
console.log('我的帐户:', { id, username, avatar })
this.push({ id, name: username, avatar }, true)
}
async 用户列表() {}
async 用户加入({ name, id }) {
await set(id, { name, id }, this.store)
}
async 用户离开({ id }) {
await del(id, this.store)
}
getAvatar(id) { }
setAvatar(user) {
//console.info('更新avatar', user)

View File

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

View File

@ -1,70 +0,0 @@
import { get, set, del, update, createStore } from 'idb-keyval'
export class Store {
constructor(data) {
this.name = data.name
this.store = data.store
// 检查过期的数据
this.checkExpired()
}
async checkExpired() {
const keys = await this.store.keys()
const now = Date.now()
for (const key of keys) {
const item = await get(key, this.store)
if (item && item.expiresAt && item.expiresAt < now) {
await this.store.delete(key)
}
}
}
async create(data) {
const id = window.crypto.randomUUID()
const createdAt = Date.now()
const updatedAt = createdAt
const lastUsedAt = createdAt
const item = { id, createdAt, updatedAt, lastUsedAt, ...data }
await set(id, item, this.store)
return item
}
async delete(id) {
return await this.store.delete(id)
}
async query(query) {
}
async get(id) {
return await get(id, this.store)
}
async update(id, data) {
const item = await this.get(id)
if (!item) {
return null
}
const updatedAt = Date.now()
const lastUsedAt = updatedAt
const item2 = { ...item, updatedAt, lastUsedAt, ...data }
await set(id, item2, this.store)
return item2
}
}
export const musicStore = new Store({
name: '音乐(资源类型)',
store: createStore('db-music', 'store-music')
})
export const chatStore = new Store({
name: '聊天(资源类型)',
store: createStore('db-chat', 'store-chat')
})
export const userStore = new Store({
name: '用户(资源类型)',
store: createStore('db-user', 'store-user')
})