Files: 1b789e63aa59f5d3e5539a4432c8b3c466977a0e / lib / latest-update.js
1999 bytesRaw
1 | const https = require('https') |
2 | const packageInfo = require('../package.json') |
3 | const compareVersion = require('compare-version') |
4 | const Value = require('mutant/value') |
5 | const computed = require('mutant/computed') |
6 | |
7 | module.exports = function () { |
8 | const update = Value() |
9 | const hidden = Value(false) |
10 | update.sync = Value(false) |
11 | const version = packageInfo.version |
12 | const checkForUpdate = () => { |
13 | // Check whether notification has been hidden. |
14 | if (hidden() === true) { |
15 | // If so, stop the interval timer. |
16 | clearInterval(updateCheckInterval) |
17 | // And `return` to quit this function. |
18 | return |
19 | } |
20 | |
21 | https.get({ |
22 | host: 'api.github.com', |
23 | path: '/repos/ssbc/patchwork/releases/latest', |
24 | headers: { |
25 | 'user-agent': `Patchwork v${version}` |
26 | } |
27 | }, function (res) { |
28 | if (res.statusCode === 200) { |
29 | let result = '' |
30 | res.on('data', (x) => { |
31 | result += x |
32 | }).on('end', () => { |
33 | const info = JSON.parse(result) |
34 | if (compareVersion(info.tag_name.slice(1), version) > 0) { |
35 | update.set(info.tag_name.slice(1)) |
36 | } |
37 | update.sync.set(true) |
38 | }) |
39 | } |
40 | // You must handle the error here otherwise you get an unhandled error exception which stops the whole app. |
41 | }).on('error', function (error) { |
42 | console.log('error trying to reach github to check for latest patchwork version: ', error) |
43 | }) |
44 | } |
45 | |
46 | // Retry update check every 24 hours. |
47 | const millisecondsPerDay = 1000 * 60 * 60 * 24 |
48 | |
49 | // Previously we only checked for updates on startup, but some people keep |
50 | // Patchwork running in the background all the time and they should get |
51 | // update notifications too. |
52 | const updateCheckInterval = setInterval(checkForUpdate, millisecondsPerDay) |
53 | |
54 | // Check for update immediately. |
55 | checkForUpdate() |
56 | |
57 | const obs = computed([update, hidden], (update, hidden) => update && !hidden ? update : false) |
58 | obs.ignore = () => hidden.set(true) |
59 | return obs |
60 | } |
61 |
Built with git-ssb-web