diff --git a/src/email.ts b/src/email.ts index cf588f4..337572c 100644 --- a/src/email.ts +++ b/src/email.ts @@ -26,6 +26,7 @@ interface ImapClientI { opts: Record, ) => Promise; logout: () => Promise; + mailboxClose: () => Promise; } type Email = { @@ -287,7 +288,10 @@ export const perform = ( if (O.isSome(e.imap)) { const imap = e.imap.value; return pipe( - TE.tryCatch(() => imap.logout(), toError), + TE.tryCatch( + () => imap.mailboxClose().then(() => imap.logout()), + toError, + ), TE.flatMap(() => TE.left(e)), ); } @@ -295,8 +299,13 @@ export const perform = ( }, ({ mailboxLock, deleted, imap }) => { mailboxLock.release(); - imap.logout(); - return TE.right(deleted); + return pipe( + TE.tryCatch( + () => imap.mailboxClose().then(() => imap.logout()), + toError, + ), + TE.flatMap(() => TE.right(deleted)), + ); }, ), ); diff --git a/tst/email.spec.ts b/tst/email.spec.ts index e966030..79c21cc 100644 --- a/tst/email.spec.ts +++ b/tst/email.spec.ts @@ -32,6 +32,7 @@ const getMocks = () => { getMailboxLock: mock(() => Promise.resolve(lock)), messageDelete: mock(() => Promise.resolve(true)), logout: mock(() => Promise.resolve()), + mailboxClose: mock(() => Promise.resolve()), }; const mockDependencies: Partial = { @@ -116,6 +117,7 @@ test("releases lock on left", async () => { TE.mapLeft(() => { expect(imap.getMailboxLock).toHaveBeenCalledTimes(1); expect(lock.release).toHaveBeenCalledTimes(1); + expect(imap.mailboxClose).toHaveBeenCalledTimes(1); expect(imap.logout).toHaveBeenCalledTimes(1); }), )(); @@ -133,6 +135,7 @@ test("releases lock on right", async () => { expect(imap.getMailboxLock).toHaveBeenCalledTimes(1); expect(lock.release).toHaveBeenCalledTimes(1); expect(imap.logout).toHaveBeenCalledTimes(1); + expect(imap.mailboxClose).toHaveBeenCalledTimes(1); }), TE.mapLeft(() => expect(false).toBeTruthy()), )();