|
| 1 | +/* |
| 2 | +Copyright 2016 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +var POKE_RATE_MS = 10 * 60 * 1000; // 10 min |
| 17 | +var currentVersion = null; |
| 18 | +var latestVersion = null; |
| 19 | +var listener = function(){}; // NOP |
| 20 | + |
| 21 | +module.exports = { |
| 22 | + setNewVersionListener: function(fn) { // invoked with fn(currentVer, newVer) |
| 23 | + listener = fn; |
| 24 | + }, |
| 25 | + |
| 26 | + run: function() { |
| 27 | + var req = new XMLHttpRequest(); |
| 28 | + req.addEventListener("load", function() { |
| 29 | + if (!req.responseText) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if (!currentVersion) { |
| 33 | + currentVersion = req.responseText.trim(); |
| 34 | + } |
| 35 | + |
| 36 | + if (req.responseText !== latestVersion) { |
| 37 | + latestVersion = req.responseText.trim(); |
| 38 | + if (module.exports.hasNewVersion()) { |
| 39 | + console.log("Current=%s Latest=%s", currentVersion, latestVersion); |
| 40 | + listener(currentVersion, latestVersion); |
| 41 | + } |
| 42 | + } |
| 43 | + }); |
| 44 | + req.open("GET", "version"); |
| 45 | + req.send(); // can't suppress 404s from being logged. |
| 46 | + |
| 47 | + setTimeout(module.exports.run, POKE_RATE_MS); |
| 48 | + }, |
| 49 | + |
| 50 | + getCurrentVersion: function() { |
| 51 | + return currentVersion; |
| 52 | + }, |
| 53 | + |
| 54 | + hasNewVersion: function() { |
| 55 | + return currentVersion && latestVersion && (currentVersion !== latestVersion); |
| 56 | + } |
| 57 | +}; |
0 commit comments