@@ -9,7 +9,7 @@ export default {
99 key : "notion-updated-page" ,
1010 name : "Updated Page in Database" , /* eslint-disable-line pipedream/source-name */
1111 description : "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead" ,
12- version : "0.1.2 " ,
12+ version : "0.1.3 " ,
1313 type : "source" ,
1414 dedupe : "unique" ,
1515 props : {
@@ -40,35 +40,37 @@ export default {
4040 } ,
4141 } ,
4242 hooks : {
43- async deploy ( ) {
44- const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
43+ async activate ( ) {
44+ console . log ( "Activating: fetching pages and properties" ) ;
45+ this . _setLastUpdatedTimestamp ( Date . now ( ) ) ;
4546 const propertyValues = { } ;
47+ const propertiesToCheck = await this . _getPropertiesToCheck ( ) ;
4648 const params = this . lastUpdatedSortParam ( ) ;
4749 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
48- let count = 0 ;
49- let lastUpdatedTimestamp = 0 ;
5050 for await ( const page of pagesStream ) {
5151 for ( const propertyName of propertiesToCheck ) {
52- const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
52+ const currentValue = this . _maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
5353 propertyValues [ page . id ] = {
5454 ...propertyValues [ page . id ] ,
5555 [ propertyName ] : currentValue ,
5656 } ;
5757 }
58- lastUpdatedTimestamp = Math . max (
59- lastUpdatedTimestamp ,
60- Date . parse ( page . last_edited_time ) ,
61- ) ;
62- if ( count ++ < 25 ) {
63- this . emitEvent ( page ) ;
64- }
6558 }
6659 this . _setPropertyValues ( propertyValues ) ;
67- this . setLastUpdatedTimestamp ( lastUpdatedTimestamp ) ;
60+ } ,
61+ async deactivate ( ) {
62+ console . log ( "Deactivating: clearing states" ) ;
63+ this . _setLastUpdatedTimestamp ( null ) ;
6864 } ,
6965 } ,
7066 methods : {
7167 ...base . methods ,
68+ _getLastUpdatedTimestamp ( ) {
69+ return this . db . get ( constants . timestamps . LAST_EDITED_TIME ) ;
70+ } ,
71+ _setLastUpdatedTimestamp ( ts ) {
72+ this . db . set ( constants . timestamps . LAST_EDITED_TIME , ts ) ;
73+ } ,
7274 _getPropertyValues ( ) {
7375 const compressed = this . db . get ( "propertyValues" ) ;
7476 const buffer = Buffer . from ( compressed , "base64" ) ;
@@ -80,14 +82,14 @@ export default {
8082 const compressed = zlib . deflateSync ( string ) . toString ( "base64" ) ;
8183 this . db . set ( "propertyValues" , compressed ) ;
8284 } ,
83- async getPropertiesToCheck ( ) {
85+ async _getPropertiesToCheck ( ) {
8486 if ( this . properties ?. length ) {
8587 return this . properties ;
8688 }
8789 const { properties } = await this . notion . retrieveDatabase ( this . databaseId ) ;
8890 return Object . keys ( properties ) ;
8991 } ,
90- maybeRemoveFileSubItems ( property ) {
92+ _maybeRemoveFileSubItems ( property ) {
9193 // Files & Media type:
9294 // `url` and `expiry_time` are constantly updated by Notion, so ignore these fields
9395 if ( property . type === "files" ) {
@@ -101,7 +103,7 @@ export default {
101103 }
102104 return property ;
103105 } ,
104- generateMeta ( obj , summary ) {
106+ _generateMeta ( obj , summary ) {
105107 const { id } = obj ;
106108 const title = this . notion . extractPageTitle ( obj ) ;
107109 const ts = Date . now ( ) ;
@@ -111,10 +113,10 @@ export default {
111113 ts,
112114 } ;
113115 } ,
114- emitEvent ( page , changes = [ ] , isNewPage = true ) {
116+ _emitEvent ( page , changes = [ ] , isNewPage = true ) {
115117 const meta = isNewPage
116- ? this . generateMeta ( page , constants . summaries . PAGE_ADDED )
117- : this . generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
118+ ? this . _generateMeta ( page , constants . summaries . PAGE_ADDED )
119+ : this . _generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
118120 const event = {
119121 page,
120122 changes,
@@ -123,9 +125,15 @@ export default {
123125 } ,
124126 } ,
125127 async run ( ) {
126- const lastCheckedTimestamp = this . getLastUpdatedTimestamp ( ) ;
128+ const lastCheckedTimestamp = this . _getLastUpdatedTimestamp ( ) ;
127129 const propertyValues = this . _getPropertyValues ( ) ;
128130
131+ if ( ! lastCheckedTimestamp ) {
132+ // recently updated (deactivated / activated), skip execution
133+ console . log ( "Awaiting restart completion: skipping execution" ) ;
134+ return ;
135+ }
136+
129137 const params = {
130138 ...this . lastUpdatedSortParam ( ) ,
131139 filter : {
@@ -136,7 +144,7 @@ export default {
136144 } ,
137145 } ;
138146 let newLastUpdatedTimestamp = lastCheckedTimestamp ;
139- const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
147+ const propertiesToCheck = await this . _getPropertiesToCheck ( ) ;
140148 const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
141149
142150 for await ( const page of pagesStream ) {
@@ -156,7 +164,7 @@ export default {
156164 for ( const propertyName of propertiesToCheck ) {
157165 const previousValue = structuredClone ( propertyValues [ page . id ] ?. [ propertyName ] ) ;
158166 // value used to compare and to save to this.db
159- const currentValueToSave = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
167+ const currentValueToSave = this . _maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
160168 // (unmodified) value that should be emitted
161169 const currentValueToEmit = page . properties [ propertyName ] ;
162170
@@ -197,11 +205,11 @@ export default {
197205 }
198206
199207 if ( propertyHasChanged ) {
200- this . emitEvent ( page , changes , isNewPage ) ;
208+ this . _emitEvent ( page , changes , isNewPage ) ;
201209 }
202210 }
203211
204- this . setLastUpdatedTimestamp ( newLastUpdatedTimestamp ) ;
212+ this . _setLastUpdatedTimestamp ( newLastUpdatedTimestamp ) ;
205213 this . _setPropertyValues ( propertyValues ) ;
206214 } ,
207215 sampleEmit,
0 commit comments