@@ -2,13 +2,14 @@ import notion from "../../notion.app.mjs";
22import  sampleEmit  from  "./test-event.mjs" ; 
33import  base  from  "../common/base.mjs" ; 
44import  constants  from  "../common/constants.mjs" ; 
5+ import  md5  from  "md5" ; 
56
67export  default  { 
78  ...base , 
89  key : "notion-updated-page" , 
910  name : "Updated Page in Database" ,  /* eslint-disable-line pipedream/source-name */ 
1011  description : "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead" , 
11-   version : "0.0.16 " , 
12+   version : "0.0.17 " , 
1213  type : "source" , 
1314  dedupe : "unique" , 
1415  props : { 
@@ -42,14 +43,26 @@ export default {
4243    async  deploy ( )  { 
4344      const  properties  =  await  this . getProperties ( ) ; 
4445      const  propertyValues  =  { } ; 
45-       const  pagesStream  =  this . notion . getPages ( this . databaseId ) ; 
46+       const  params  =  this . lastUpdatedSortParam ( ) ; 
47+       const  pagesStream  =  this . notion . getPages ( this . databaseId ,  params ) ; 
48+       let  count  =  0 ; 
49+       let  lastUpdatedTimestamp  =  0 ; 
4650      for  await  ( const  page  of  pagesStream )  { 
4751        propertyValues [ page . id ]  =  { } ; 
4852        for  ( const  property  of  properties )  { 
49-           propertyValues [ page . id ] [ property ]  =  JSON . stringify ( page . properties [ property ] ) ; 
53+           propertyValues [ page . id ] [ property ]  =  md5 ( JSON . stringify ( page . properties [ property ] ) ) ; 
5054        } 
55+         lastUpdatedTimestamp  =  Math . max ( 
56+           lastUpdatedTimestamp , 
57+           Date . parse ( page ?. last_edited_time ) , 
58+         ) ; 
59+         if  ( count  <  25 )  { 
60+           this . emitEvent ( page ) ; 
61+         } 
62+         count ++ ; 
5163      } 
5264      this . _setPropertyValues ( propertyValues ) ; 
65+       this . setLastUpdatedTimestamp ( lastUpdatedTimestamp ) ; 
5366    } , 
5467  } , 
5568  methods : { 
@@ -77,14 +90,16 @@ export default {
7790        ts, 
7891      } ; 
7992    } , 
93+     emitEvent ( page )  { 
94+       const  meta  =  this . generateMeta ( page ,  constants . summaries . PAGE_UPDATED ) ; 
95+       this . $emit ( page ,  meta ) ; 
96+     } , 
8097  } , 
8198  async  run ( )  { 
8299    const  lastCheckedTimestamp  =  this . getLastUpdatedTimestamp ( ) ; 
83100    const  propertyValues  =  this . _getPropertyValues ( ) ; 
84101
85-     const  params  =  { 
86-       ...this . lastUpdatedSortParam ( ) , 
87-     } ; 
102+     const  params  =  this . lastUpdatedSortParam ( ) ; 
88103    let  newLastUpdatedTimestamp  =  lastCheckedTimestamp ; 
89104    const  properties  =  await  this . getProperties ( ) ; 
90105    const  pagesStream  =  this . notion . getPages ( this . databaseId ,  params ) ; 
@@ -97,7 +112,7 @@ export default {
97112
98113      let  propertyChangeFound  =  false ; 
99114      for  ( const  property  of  properties )  { 
100-         const  currentProperty  =  JSON . stringify ( page . properties [ property ] ) ; 
115+         const  currentProperty  =  md5 ( JSON . stringify ( page . properties [ property ] ) ) ; 
101116        if  ( ! propertyValues [ page . id ]  ||  currentProperty  !==  propertyValues [ page . id ] [ property ] )  { 
102117          propertyChangeFound  =  true ; 
103118          propertyValues [ page . id ]  =  { 
@@ -114,8 +129,11 @@ export default {
114129        continue ; 
115130      } 
116131
117-       const  meta  =  this . generateMeta ( page ,  constants . summaries . PAGE_UPDATED ) ; 
118-       this . $emit ( page ,  meta ) ; 
132+       this . emitEvent ( page ) ; 
133+ 
134+       if  ( Date . parse ( page ?. last_edited_time  <  lastCheckedTimestamp ) )  { 
135+         break ; 
136+       } 
119137    } 
120138
121139    this . setLastUpdatedTimestamp ( newLastUpdatedTimestamp ) ; 
0 commit comments