src/gas-gmail-channel-lib.js
/**
    *
    * Run all middlewares for each thread, then call finallCallback(if specified)
    *
    * @param <Function> finalCallback 
    *   finalCallback will be called at the end of all middleware had ran.
    *   because middleware could be terminated at the middle, the lastest one could not know what happend.
    *
    */
Run all middlewares for each thread, then call finallCallback(if specified)
@param  finalCallback
finalCallback will be called at the end of all middleware had ran.
because middleware could be terminated at the middle, the lastest one could not know what happend.
Run all middlewares for each thread then call finallCallback ( if specified ) @param finalCallback finalCallback will be called at the end of all middleware had ran . because middleware could be terminated at the middle the lastest one could not know what happend .
function done(finalCallback) {
      if (finalCallback && (!finalCallback instanceof Function)) throw Error('done() need param finalCallback to be a function')
      var mailThreads = getNewThreads(LIMIT)
      var res, req
      var counter = 0
      for (var i=0; i<mailThreads.length; i++) {
        var mailMessages = mailThreads[i].getMessages()
        for (var j=0; j<mailMessages.length; j++) {
          ERRORS = []
          res = {}          
          copyKeys(res, RES)
          req = {
            getChannelName: getName
            , getThread:  (function (t) { return function () { return t } })(mailThreads[i]) 
            , getMessage: (function (m) { return function () { return m } })(mailMessages[j]) 
            , getErrors: function ()  { return ERRORS }
            , pushError: function (e) { ERRORS.push(e) }
          }        
          for (var k=0; k<MIDDLEWARES.length; k++) {
            var middleware = MIDDLEWARES[k]
            var isNextCalled = false
            var error = undefined
            try {
              middleware(req, res, function (err) {
                isNextCalled = true
                error = err
              })
            } catch (e) {
              error = e
            }
            if (error) ERRORS.push(error)
            if (!isNextCalled) {
              break
            }
          } 
          if (finalCallback) finalCallback(req, res, function (err) { } )
          if (DONE_LABEL) mailThreads[i].addLabel(DONE_LABEL);
          counter++; 
          if (CONVERSATION) break;
        } 
      } 
      return counter
    }
function done ( finalCallback ) { if ( finalCallback && ( ! finalCallback instanceof Function ) ) throw Error ( 'done() need param finalCallback to be a function' ) var mailThreads = getNewThreads ( LIMIT ) var res , req var counter = 0 for ( var i = 0 ; i < mailThreads . length ; i ++ ) { var mailMessages = mailThreads [ i ] . getMessages ( ) for ( var j = 0 ; j < mailMessages . length ; j ++ ) { ERRORS = [ ] res = { } copyKeys ( res , RES ) req = { getChannelName : getName , getThread : ( function ( t ) { return function ( ) { return t } } ) ( mailThreads [ i ] ) , getMessage : ( function ( m ) { return function ( ) { return m } } ) ( mailMessages [ j ] ) , getErrors : function ( ) { return ERRORS } , pushError : function ( e ) { ERRORS . push ( e ) } } for ( var k = 0 ; k < MIDDLEWARES . length ; k ++ ) { var middleware = MIDDLEWARES [ k ] var isNextCalled = false var error = undefined try { middleware ( req , res , function ( err ) { isNextCalled = true error = err } ) } catch ( e ) { error = e } if ( error ) ERRORS . push ( error ) if ( ! isNextCalled ) { break } } if ( finalCallback ) finalCallback ( req , res , function ( err ) { } ) if ( DONE_LABEL ) mailThreads [ i ] . addLabel ( DONE_LABEL ) ; counter ++ ; if ( CONVERSATION ) break ; } } return counter }
Run all middlewares for each thread, then call finallCallback(if specified)
 @param <Function> finalCallback
   finalCallback will be called at the end of all middleware had ran.
Run all middlewares for each thread then call finallCallback ( if specified ) @param <Function > finalCallback finalCallback will be called at the end of all middleware had ran .
// Count for how many times we called middlewares(threads/messages we had dealed with) /**
          *
          * re-init ERRORS & res & req
          *
          */ // closure for the furture possible run in nodejs, because of async call back // loop end, because middleware did not call next // END for loop of MIDDLEWARES // record we had run a set of middleware (procceed a mail message) /**
          *
          * if we are in conversation mode, we call middleware function chains for each thread.
          *
          */ // END for loop of mailMessages // END for loop of mailThreads 