mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-22 00:06:06 +08:00
Change proxy to honor null values, create helper for open box name
This commit is contained in:
parent
ffc2593c4e
commit
a11ee2987b
|
@ -4,23 +4,31 @@ const {
|
|||
IMAPConnectionNotReadyError,
|
||||
} = require('./imap-errors');
|
||||
|
||||
/*
|
||||
IMAPBox uses Proxy to wrap the "box" exposed by node-imap. It provides higher-level
|
||||
primitives, but you can still call through to properties / methods of the node-imap
|
||||
box, ala `imapbox.uidvalidity`
|
||||
*/
|
||||
class IMAPBox {
|
||||
constructor(imapConn, box) {
|
||||
this._conn = imapConn
|
||||
this._box = box
|
||||
|
||||
return new Proxy(this, {
|
||||
get(target, name) {
|
||||
const prop = Reflect.get(target, name)
|
||||
if (!prop) {
|
||||
return Reflect.get(target._box, name)
|
||||
get(obj, prop) {
|
||||
const val = (prop in obj) ? obj[prop] : obj._box[prop];
|
||||
|
||||
if (_.isFunction(val)) {
|
||||
const myBox = obj._box.name;
|
||||
const openBox = obj._conn.getOpenBoxName()
|
||||
if (myBox !== openBox) {
|
||||
return () => {
|
||||
throw new Error(`IMAPBox::${prop} - Mailbox is no longer selected on the IMAPConnection (${myBox} != ${openBox}).`);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_.isFunction(prop) && target._conn._imap._box.name !== target._box.name) {
|
||||
return () => Promise.reject(
|
||||
new Error(`IMAPBox::${name} - Can't operate on a mailbox that is no longer open on the current IMAPConnection.`)
|
||||
)
|
||||
}
|
||||
return prop
|
||||
|
||||
return val;
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -184,6 +184,10 @@ class IMAPConnection extends EventEmitter {
|
|||
return this._imap.delBoxAsync(folderName)
|
||||
}
|
||||
|
||||
getOpenBoxName() {
|
||||
return (this._imap && this._imap._box) ? this._imap._box.name : null;
|
||||
}
|
||||
|
||||
runOperation(operation) {
|
||||
if (!this._imap) {
|
||||
throw new IMAPConnectionNotReadyError(`IMAPConnection::runOperation`)
|
||||
|
|
Loading…
Reference in a new issue