Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
chore: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Oct 29, 2024
1 parent 80cc83b commit 58fee20
Showing 1 changed file with 17 additions and 65 deletions.
82 changes: 17 additions & 65 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,37 +167,6 @@ const adjustContactWidth = async () => {
})
}

// key为num str的Cache
class LimitedMap {
maxSize = 0
m = new Map()
constructor(maxSize) {
this.maxSize = maxSize
}
get(k) {
return this.m.get(k)
}
set(k, v) {
this.m.set(k, v)
if (this.m.size > this.maxSize) {
this.free()
}
}
has(k) {
return this.m.has(k)
}
async free() {
try {
const keys = Array.from(this.m.keys())
.sort((a, b) => parseInt(a) - parseInt(b))
.slice(0, Math.floor(this.maxSize / 4))
keys.forEach((k) => this.m.delete(k))
} catch {
// err
}
}
}

// 仿Telegram,拼接消息,头像浮动
const concatMsg = async () => {
const msgList = document.querySelector('#ml-root .ml-list')
Expand Down Expand Up @@ -367,43 +336,26 @@ const onMessageCreate = async () => {
IPC.updateAllSetting()

// 更新CSS
try {
waitForEle('body', () => {
updateAllCSS().catch((err) => {
throw err
})
waitForEle('body', () => {
updateAllCSS().catch((err) => {
error('updateAllCSS err', err.toString())
})
} catch (err) {
error('updateAllCSS failed', err)
}
})
// 调节宽度
try {
waitForEle(
`.two-col-layout__aside[style*='width-aside']:has(.resize-handler) + .two-col-layout__main[style*='width-main']`,
() => {
adjustContactWidth().catch((err) => {
throw err
})
},
500,
)
} catch (err) {
error('adjustContactWidth failed', err)
}
waitForEle(
`.two-col-layout__aside[style*='width-aside']:has(.resize-handler) + .two-col-layout__main[style*='width-main']`,
() => {
adjustContactWidth().catch((err) => {
error('adjustContactWidth err', err.toString())
})
},
)
// 拼接消息,头像浮动
try {
waitForEle(
'#ml-root .ml-list',
() => {
concatMsg().catch((err) => {
throw err
})
},
500,
)
} catch (err) {
error('concatMsg failed', err)
}
waitForEle('#ml-root .ml-list', () => {
concatMsg().catch((err) => {
error('concatMsg err', err.toString())
})
})

channel.onmessage = (event) => {
if (['#/main/message', '#/main/contact/profile', '#/chat'].includes(location.hash)) {
Expand Down

0 comments on commit 58fee20

Please sign in to comment.