@@ -12,14 +12,36 @@ import drive from "./drive.mjs";
12
12
*/
13
13
export default {
14
14
...base ,
15
+ props : {
16
+ ...base . props ,
17
+ watchDrive : {
18
+ type : "boolean" ,
19
+ label : "Watch Drive" ,
20
+ description : "Set to `true` to watch the drive for changes. May reduce rate limiting." ,
21
+ optional : true ,
22
+ } ,
23
+ } ,
15
24
methods : {
16
25
...drive . methods ,
17
26
...base . methods ,
27
+ _getChangeToken ( ) {
28
+ return this . db . get ( "changeToken" ) ;
29
+ } ,
30
+ _setChangeToken ( changeToken ) {
31
+ this . db . set ( "changeToken" , changeToken ) ;
32
+ } ,
18
33
async activateHook ( channelID ) {
19
- return this . googleSheets . activateHook (
34
+ if ( this . watchDrive ) {
35
+ return this . googleSheets . activateHook (
36
+ channelID ,
37
+ this . http . endpoint ,
38
+ this . googleSheets . getDriveId ( this . watchedDrive ) ,
39
+ ) ;
40
+ }
41
+ return this . googleSheets . activateFileHook (
20
42
channelID ,
21
43
this . http . endpoint ,
22
- this . googleSheets . getDriveId ( this . watchedDrive ) ,
44
+ this . getSheetId ( ) ,
23
45
) ;
24
46
} ,
25
47
async getAllWorksheetIds ( sheetID ) {
@@ -29,6 +51,19 @@ export default {
29
51
. filter ( ( { sheetType } ) => sheetType === "GRID" )
30
52
. map ( ( { sheetId } ) => ( sheetId . toString ( ) ) ) ;
31
53
} ,
54
+ async isSheetRelevant ( ) {
55
+ const pageToken = this . _getChangeToken ( ) || this . _getPageToken ( ) ;
56
+ const drive = this . googleSheets . drive ( ) ;
57
+ const { data } = await drive . changes . list ( {
58
+ pageToken,
59
+ driveId : this . googleSheets . getDriveId ( this . watchedDrive ) ,
60
+ } ) ;
61
+ const {
62
+ changes, newStartPageToken,
63
+ } = data ;
64
+ this . _setChangeToken ( newStartPageToken ) ;
65
+ return changes . some ( ( change ) => change . file . id === this . getSheetId ( ) ) ;
66
+ } ,
32
67
async renewSubscription ( ) {
33
68
// Assume subscription & channelID may all be undefined at
34
69
// this point Handle their absence appropriately.
@@ -53,10 +88,37 @@ export default {
53
88
} ) ;
54
89
this . _setChannelID ( newChannelID ) ;
55
90
} ,
91
+ async renewDriveSubscription ( ) {
92
+ const subscription = this . _getSubscription ( ) ;
93
+ const channelID = this . _getChannelID ( ) || uuid ( ) ;
94
+
95
+ const {
96
+ expiration,
97
+ resourceId,
98
+ newChannelID,
99
+ newPageToken,
100
+ } = await this . googleSheets . renewSubscription (
101
+ this . watchedDrive ,
102
+ subscription ,
103
+ this . http . endpoint ,
104
+ channelID ,
105
+ this . _getPageToken ( ) ,
106
+ ) ;
107
+
108
+ this . _setSubscription ( {
109
+ expiration,
110
+ resourceId,
111
+ } ) ;
112
+ this . _setChannelID ( newChannelID ) ;
113
+ this . _setPageToken ( newPageToken ) ;
114
+ } ,
56
115
} ,
57
116
async run ( event ) {
58
117
if ( event . timestamp ) {
59
118
// Component was invoked by timer
119
+ if ( this . watchDrive ) {
120
+ return this . renewDriveSubscription ( ) ;
121
+ }
60
122
return this . renewSubscription ( ) ;
61
123
}
62
124
@@ -65,6 +127,15 @@ export default {
65
127
return ;
66
128
}
67
129
130
+ this . http . respond ( {
131
+ status : 200 ,
132
+ } ) ;
133
+
134
+ if ( this . watchDrive && ! ( await this . isSheetRelevant ( event ) ) ) {
135
+ console . log ( "Change to unwatched file, exiting early" ) ;
136
+ return ;
137
+ }
138
+
68
139
const spreadsheet = await this . googleSheets . getSpreadsheet ( this . sheetID ) ;
69
140
70
141
return this . processSpreadsheet ( spreadsheet ) ;
0 commit comments