Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for limit when searching #221

Closed
engram-design opened this Issue · 3 comments

2 participants

@engram-design

Related to #218 would it be possible to add a LIMIT option when searching for emails? I'm currently using [ 'ALL', ['SINCE', 'yesterday'] ], and processing those emails. The issue is - what if the script has missed processing emails before yesterday?

To alleviate the errors produced in #218 I need to not read so many emails per batch, which would be really easy to do with LIMIT - and wouldn't be constrained to time.

Thoughts?

@mscdex
Owner

There is no built-in mechanism in the imap protocol (or even any extra server capabilities that I know of) to limit search results.

I think the best way is to just fetch a portion of the results array at a time (you might have to use some trial and error to find out what the optimal size is). One solution might be something like (untested):

var FETCH_CONCURRENCY = 10;
function fetchMsgs(results, idx, cb) {
  if (typeof idx === 'function') {
    cb = idx;
    idx = 0;
  }
  var end = Math.min(results.length, idx + FETCH_CONCURRENCY);
  conn.fetch(results.slice(idx, end), {
    // ....
  }, function(err) {
    if (err) return cb(err);
    if (end < results.length)
      fechMsgs(results, end, cb);
    else
      cb();
  });
}

// somewhere else in your code after performing your search
// and storing results in `results`...
fetchMsgs(results, function(err) {
  if (err) throw err; // something more sensible than this obviously
  // all fetches done!
});
@engram-design

@mscdex Yep, I think that's a fine solution! Only reason I suggested limit was mainly from the feature here

@mscdex
Owner

@engram-design The limit being used there is just a simple range based on messages' sequence numbers, which you can do in this module as well with search(). The problem is you may not know what the sequence numbers are before searching. If you did know them before searching, then you probably wouldn't have to search in the first place... :-)

@mscdex mscdex closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.