-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (35 loc) · 1.22 KB
/
index.js
File metadata and controls
43 lines (35 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const storage = require('@google-cloud/storage');
function getLastModified(file) {
return file.getMetadata().then(([{timeCreated}]) => timeCreated);
}
function schedulePoll(pollTime, currentLastModified, ...args) {
setTimeout(() => {
poll(currentLastModified, ...args).then((newLastModified) => {
schedulePoll(pollTime, newLastModified, ...args);
});
}, pollTime);
}
function poll(currentLastModified, ui, file, notify) {
return getLastModified(file).then((newLastModified) => {
if (currentLastModified !== newLastModified) {
ui.writeLine(`config modified; old=${currentLastModified}; new=${newLastModified}`);
notify();
}
return newLastModified;
});
}
class GCSNotifier {
constructor({bucket, key, authentication, poll} = {}) {
this.bucket = bucket;
this.key = key || 'fastboot-deploy-info.json';
this.authentication = authentication;
this.pollTime = poll || 3 * 1000;
}
subscribe(notify) {
const file = storage(this.authentication).bucket(this.bucket).file(this.key);
return getLastModified(file).then((lastModified) => {
schedulePoll(this.pollTime, lastModified, this.ui, file, notify);
});
}
}
module.exports = GCSNotifier;