diff --git a/demo.html b/demo.html deleted file mode 100644 index af950ba..0000000 --- a/demo.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - DEMO - - - - - diff --git a/src/client.js b/src/client.js index 982073d..da9a8e5 100644 --- a/src/client.js +++ b/src/client.js @@ -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) diff --git a/src/communication.js b/src/communication.js deleted file mode 100644 index 03a1c91..0000000 --- a/src/communication.js +++ /dev/null @@ -1 +0,0 @@ -// 通信 diff --git a/src/store.js b/src/store.js deleted file mode 100644 index 48656e4..0000000 --- a/src/store.js +++ /dev/null @@ -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') -})