Performance fixes

Kiro fixed Performance
This commit is contained in:
2026-02-07 17:23:27 +01:00
parent 68c665d527
commit 61008b63bb
6 changed files with 210 additions and 64 deletions

View File

@@ -69,9 +69,10 @@ export class ImapService {
* @param {string|null} _pageToken - reserved for future pagination
*/
async listEmails(maxResults = 50, _pageToken = null) {
const lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
let lock = null
try {
lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
const uids = await this.client.search({ all: true }, { uid: true })
const slice = uids.slice(0, maxResults)
const nextPageToken = uids.length > maxResults ? String(slice[slice.length - 1]) : null
@@ -80,8 +81,10 @@ export class ImapService {
nextPageToken,
}
} finally {
lock.release()
this.lock = null
if (lock) {
lock.release()
this.lock = null
}
}
}
@@ -101,14 +104,17 @@ export class ImapService {
* Get one message by id (UID string)
*/
async getEmail(messageId) {
const lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
let lock = null
try {
lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
const list = await this.client.fetchAll(String(messageId), { envelope: true }, { uid: true })
return this._normalize(list && list[0])
} finally {
lock.release()
this.lock = null
if (lock) {
lock.release()
this.lock = null
}
}
}
@@ -117,9 +123,10 @@ export class ImapService {
*/
async batchGetEmails(messageIds) {
if (!messageIds.length) return []
const lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
let lock = null
try {
lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
const uids = messageIds.map((id) => (typeof id === 'string' ? Number(id) : id)).filter((n) => !Number.isNaN(n))
if (!uids.length) return []
const list = await this.client.fetchAll(uids, { envelope: true }, { uid: true })
@@ -128,8 +135,10 @@ export class ImapService {
log.warn('IMAP batchGetEmails failed', { error: e.message })
return []
} finally {
lock.release()
this.lock = null
if (lock) {
lock.release()
this.lock = null
}
}
}
@@ -155,13 +164,16 @@ export class ImapService {
async moveToFolder(messageId, folderName) {
const path = `${FOLDER_PREFIX}/${folderName}`
await this.ensureFolder(folderName)
const lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
let lock = null
try {
lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
await this.client.messageMove(String(messageId), path, { uid: true })
} finally {
lock.release()
this.lock = null
if (lock) {
lock.release()
this.lock = null
}
}
}
@@ -169,13 +181,16 @@ export class ImapService {
* Mark message as read (\\Seen)
*/
async markAsRead(messageId) {
const lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
let lock = null
try {
lock = await this.client.getMailboxLock(INBOX)
this.lock = lock
await this.client.messageFlagsAdd(String(messageId), ['\\Seen'], { uid: true })
} finally {
lock.release()
this.lock = null
if (lock) {
lock.release()
this.lock = null
}
}
}
}