唯一ID?
This commit is contained in:
@@ -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)
|
||||
|
@@ -1 +0,0 @@
|
||||
// 通信
|
70
src/store.js
70
src/store.js
@@ -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')
|
||||
})
|
Reference in New Issue
Block a user