@@ -15,7 +15,7 @@ export default {
1515 name : "New Email Received" ,
1616 description : "Emit new event when a new email is received." ,
1717 type : "source" ,
18- version : "0.1.7 " ,
18+ version : "0.1.8 " ,
1919 dedupe : "unique" ,
2020 props : {
2121 gmail,
@@ -419,6 +419,15 @@ export default {
419419 } ) ) ;
420420 return messages ;
421421 } ,
422+ getHistoryResponse ( startHistoryId ) {
423+ return this . gmail . listHistory ( {
424+ startHistoryId,
425+ historyTypes : [
426+ "messageAdded" ,
427+ ] ,
428+ labelId : this . label ,
429+ } ) ;
430+ } ,
422431 } ,
423432 async run ( event ) {
424433 if ( this . triggerType === "polling" ) {
@@ -479,20 +488,27 @@ export default {
479488 console . log ( "Last processed historyId:" , lastProcessedHistoryId ) ;
480489
481490 // Use the minimum of lastProcessedHistoryId and the received historyId
482- const startHistoryId = Math . min (
491+ let startHistoryId = Math . min (
483492 parseInt ( lastProcessedHistoryId ) ,
484493 parseInt ( receivedHistoryId ) ,
485494 ) ;
486495 console . log ( "Using startHistoryId:" , startHistoryId ) ;
487496
488497 // Fetch the history
489- const historyResponse = await this . gmail . listHistory ( {
490- startHistoryId,
491- historyTypes : [
492- "messageAdded" ,
493- ] ,
494- labelId : this . label ,
495- } ) ;
498+ let historyResponse ;
499+ try {
500+ historyResponse = await this . getHistoryResponse ( startHistoryId ) ;
501+ } catch {
502+ // catch error thrown if startHistoryId is invalid or expired
503+
504+ // emit recent messages to attempt to avoid missing any messages
505+ await this . emitRecentMessages ( ) ;
506+
507+ // set startHistoryId to the historyId received from the webhook
508+ startHistoryId = parseInt ( receivedHistoryId ) ;
509+ console . log ( "Using startHistoryId:" , startHistoryId ) ;
510+ historyResponse = await this . getHistoryResponse ( startHistoryId ) ;
511+ }
496512
497513 console . log (
498514 "History response:" ,
0 commit comments