Change proxy to honor null values, create helper for open box name

This commit is contained in:
Ben Gotow 2016-07-13 12:39:07 -07:00
parent ffc2593c4e
commit a11ee2987b
2 changed files with 22 additions and 10 deletions

View file

@ -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;
},
})
}

View file

@ -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`)