B2G/gaia/apps/email/js/ext/mailapi/composite/configurator.js
// - got the target folder conn, now do the copies
got the target folder conn, now do the copies
got the target folder conn now do the copies
function gotTargetConn(targetConn, targetStorage) {
          var uidnext = targetConn.box._uidnext;
          folderConn._conn.copy(serverIds, targetStorage.folderMeta.path,
                                copiedMessages_reselect);
          function copiedMessages_reselect() {
            targetConn.reselectBox(copiedMessages_findNewUIDs);
          }
          function copiedMessages_findNewUIDs() {
            var fetcher = targetConn._conn.fetch(
              uidnext + ':*',
              {
                request: {
                  headers: ['MESSAGE-ID'],
                  struct: false,
                  body: false
                }
              });
            fetcher.on('message', function(msg) {
              msg.on('end', fetchedMessageData);
            });
            fetcher.on('error', function(err) {
              aggrErr = err;
              perFolderDone();
            });
            fetcher.on('end', function() {
              if (reportedHeaders < namers.length) {
                if (--retriesLeft === 0) {
                  aggrErr = 'aborted-retry';
                  perFolderDone();
                  return;
                }
                window.setTimeout(copiedMessages_findNewUIDs, 500);
              }
            });
          }
          function fetchedMessageData(msg) {
            var guid = msg.msg.meta.messageId;
            if (!guidToNamer.hasOwnProperty(guid))
              return;
            reportedHeaders++;
            var namer = guidToNamer[guid];
            stateDelta.serverIdMap[namer.suid] = msg.id;
            uidnext = msg.id + 1;
            var newSuid = state.moveMap[namer.suid];
            var newId =
                  parseInt(newSuid.substring(newSuid.lastIndexOf('/') + 1));
            targetStorage.updateMessageHeader(
              namer.date, newId, false,
              function(header) {
                if (header)
                  header.srvid = msg.id;
                else
                  console.warn('did not find header for', namer.suid,
                               newSuid, namer.date, newId);
                if (--waitingOnHeaders === 0)
                  foundUIDs_deleteOriginals();
                return true;
              });
          }
        }
function gotTargetConn ( targetConn , targetStorage ) { var uidnext = targetConn . box . _uidnext ; folderConn . _conn . copy ( serverIds , targetStorage . folderMeta . path , copiedMessages_reselect ) ; function copiedMessages_reselect ( ) { targetConn . reselectBox ( copiedMessages_findNewUIDs ) ; } function copiedMessages_findNewUIDs ( ) { var fetcher = targetConn . _conn . fetch ( uidnext + ':*' , { request : { headers : [ 'MESSAGE-ID' ] , struct : false , body : false } } ) ; fetcher . on ( 'message' , function ( msg ) { msg . on ( 'end' , fetchedMessageData ) ; } ) ; fetcher . on ( 'error' , function ( err ) { aggrErr = err ; perFolderDone ( ) ; } ) ; fetcher . on ( 'end' , function ( ) { if ( reportedHeaders < namers . length ) { if ( -- retriesLeft === 0 ) { aggrErr = 'aborted-retry' ; perFolderDone ( ) ; return ; } window . setTimeout ( copiedMessages_findNewUIDs , 500 ) ; } } ) ; } function fetchedMessageData ( msg ) { var guid = msg . msg . meta . messageId ; if ( ! guidToNamer . hasOwnProperty ( guid ) ) return ; reportedHeaders ++ ; var namer = guidToNamer [ guid ] ; stateDelta . serverIdMap [ namer . suid ] = msg . id ; uidnext = msg . id + 1 ; var newSuid = state . moveMap [ namer . suid ] ; var newId = parseInt ( newSuid . substring ( newSuid . lastIndexOf ( '/' ) + 1 ) ) ; targetStorage . updateMessageHeader ( namer . date , newId , false , function ( header ) { if ( header ) header . srvid = msg . id ; else console . warn ( 'did not find header for' , namer . suid , newSuid , namer . date , newId ) ; if ( -- waitingOnHeaders === 0 ) foundUIDs_deleteOriginals ( ) ; return true ; } ) ; } }
got the target folder conn, now do the copies
got the target folder conn now do the copies
// Force a re-select of the folder to try and force the server to // perceive the move.  This was necessary for me at least on my // dovceot test setup.  Although we had heard that the COPY // completed, our FETCH was too fast, although an IDLE did report // the new messages after that. // We need to use a callback here because imap.js's state // checking is immediate, so it's very possible to race the folder // selection and lose. // - copies are done, find the UIDs // because we aren't waiting for body data, we can just process the // 'message' event directly without registering for an 'end' event // on it. // We don't need to wait for 'end' since we know how many of these // we care about. // If we didn't hear about all the headers, let's retry in // a little bit. // If the header isn't there because it got moved, then null // will be returned and it's up to the next move operation to // fix this up.