Move 'new version' support into Platform

This commit is contained in:
David Baker 2016-11-02 16:02:55 +00:00
parent a714edbf2b
commit 64fdb290eb
4 changed files with 86 additions and 59 deletions

View File

@ -19,6 +19,7 @@ limitations under the License.
var React = require('react'); var React = require('react');
var sdk = require('matrix-react-sdk'); var sdk = require('matrix-react-sdk');
import Modal from 'matrix-react-sdk/lib/Modal'; import Modal from 'matrix-react-sdk/lib/Modal';
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
/** /**
* Check a version string is compatible with the Changelog * Check a version string is compatible with the Changelog
@ -31,30 +32,50 @@ function checkVersion(ver) {
export default function NewVersionBar(props) { export default function NewVersionBar(props) {
const onChangelogClicked = () => { const onChangelogClicked = () => {
const ChangelogDialog = sdk.getComponent('dialogs.ChangelogDialog'); if (props.releaseNotes) {
const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog');
Modal.createDialog(ChangelogDialog, { Modal.createDialog(QuestionDialog, {
version: props.version, title: "What's New",
newVersion: props.newVersion, description: <pre className="changelog_text">{props.releaseNotes}</pre>,
button: "Update",
onFinished: (update) => { onFinished: (update) => {
if(update) { if(update && PlatformPeg.get()) {
window.location.reload(); PlatformPeg.get().installUpdate();
} }
} }
}); });
} else {
const ChangelogDialog = sdk.getComponent('dialogs.ChangelogDialog');
Modal.createDialog(ChangelogDialog, {
version: props.version,
newVersion: props.newVersion,
releaseNotes: releaseNotes,
onFinished: (update) => {
if(update && PlatformPeg.get()) {
PlatformPeg.get().installUpdate();
}
}
});
}
}; };
let changelog_button; const onUpdateClicked = () => {
if (checkVersion(props.version) && checkVersion(props.newVersion)) { PlatformPeg.get().installUpdate();
changelog_button = <button className="mx_MatrixToolbar_action" onClick={onChangelogClicked}>Changelog</button>; };
let action_button;
if (props.releaseNotes || (checkVersion(props.version) && checkVersion(props.newVersion))) {
action_button = <button className="mx_MatrixToolbar_action" onClick={onChangelogClicked}>What's new?</button>;
} else if (PlatformPeg.get()) {
action_button = <button className="mx_MatrixToolbar_action" onClick={onUpdateClicked}>Update</button>;
} }
return ( return (
<div className="mx_MatrixToolbar"> <div className="mx_MatrixToolbar">
<img className="mx_MatrixToolbar_warning" src="img/warning.svg" width="24" height="23" alt="/!\"/> <img className="mx_MatrixToolbar_warning" src="img/warning.svg" width="24" height="23" alt="/!\"/>
<div className="mx_MatrixToolbar_content"> <div className="mx_MatrixToolbar_content">
A new version of Riot is available. Refresh your browser. A new version of Riot is available.
</div> </div>
{changelog_button} {action_button}
</div> </div>
); );
} }
@ -62,4 +83,5 @@ export default function NewVersionBar(props) {
NewVersionBar.propTypes = { NewVersionBar.propTypes = {
version: React.PropTypes.string.isRequired, version: React.PropTypes.string.isRequired,
newVersion: React.PropTypes.string.isRequired, newVersion: React.PropTypes.string.isRequired,
releaseNotes: React.PropTypes.string,
}; };

View File

@ -113,10 +113,6 @@ function onHashChange(ev) {
routeUrl(window.location); routeUrl(window.location);
} }
function onVersion(current, latest) {
window.matrixChat.onVersion(current, latest);
}
var loaded = false; var loaded = false;
var lastLoadedScreen = null; var lastLoadedScreen = null;
@ -165,8 +161,7 @@ window.onload = function() {
if (!validBrowser) { if (!validBrowser) {
return; return;
} }
UpdateChecker.setVersionListener(onVersion); UpdateChecker.start();
UpdateChecker.run();
routeUrl(window.location); routeUrl(window.location);
loaded = true; loaded = true;
if (lastLoadedScreen) { if (lastLoadedScreen) {

View File

@ -18,12 +18,14 @@ limitations under the License.
import BasePlatform from 'matrix-react-sdk/lib/BasePlatform'; import BasePlatform from 'matrix-react-sdk/lib/BasePlatform';
import Favico from 'favico.js'; import Favico from 'favico.js';
import request from 'browser-request';
import dis from 'matrix-react-sdk/lib/dispatcher.js'; import dis from 'matrix-react-sdk/lib/dispatcher.js';
import q from 'q'; import q from 'q';
export default class WebPlatform extends BasePlatform { export default class WebPlatform extends BasePlatform {
constructor() { constructor() {
super(); super();
this.runningVersion = null;
// The 'animations' are really low framerate and look terrible. // The 'animations' are really low framerate and look terrible.
// Also it re-starts the animationb every time you set the badge, // Also it re-starts the animationb every time you set the badge,
// and we set the state each time, even if the value hasn't changed, // and we set the state each time, even if the value hasn't changed,
@ -88,4 +90,42 @@ export default class WebPlatform extends BasePlatform {
notification.close(); notification.close();
}, 5 * 1000); }, 5 * 1000);
} }
_getVersion() {
const deferred = q.defer();
request(
{ method: "GET", url: "version" },
(err, response, body) => {
if (err || response.status < 200 || response.status >= 300) {
if (err == null) err = { status: response.status };
deferred.reject(err);
return;
}
const ver = body.trim();
deferred.resolve(ver);
}
);
return deferred.promise;
}
pollForUpdate() {
this._getVersion().done((ver) => {
if (this.runningVersion == null) {
this.runningVersion = ver;
} else if (this.runningVersion != ver) {
dis.dispatch({
action: 'new_version',
currentVersion: this.runningVersion,
newVersion: ver,
});
}
}, (err) => {
console.error("Failed to poll for update", err);
});
}
installUpdate() {
window.location.reload();
}
} }

View File

@ -13,48 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
var POKE_RATE_MS = 10 * 60 * 1000; // 10 min var POKE_RATE_MS = 10 * 60 * 1000; // 10 min
var currentVersion = null;
var latestVersion = null;
var listener = function(){}; // NOP
module.exports = { module.exports = {
setVersionListener: function(fn) { // invoked with fn(currentVer, newVer) start: function() {
listener = fn; module.exports.poll();
setInterval(module.exports.poll, POKE_RATE_MS);
}, },
run: function() { poll: function() {
var req = new XMLHttpRequest(); PlatformPeg.get().pollForUpdate();
req.addEventListener("load", function() {
if (!req.responseText) {
return;
}
var ver = req.responseText.trim();
if (!currentVersion) {
currentVersion = ver;
listener(currentVersion, currentVersion);
}
if (ver !== latestVersion) {
latestVersion = ver;
if (module.exports.hasNewVersion()) {
console.log("Current=%s Latest=%s", currentVersion, latestVersion);
listener(currentVersion, latestVersion);
}
}
});
var cacheBuster = "?ts=" + new Date().getTime();
req.open("GET", "version" + cacheBuster);
req.send(); // can't suppress 404s from being logged.
setTimeout(module.exports.run, POKE_RATE_MS);
},
getCurrentVersion: function() {
return currentVersion;
},
hasNewVersion: function() {
return currentVersion && latestVersion && (currentVersion !== latestVersion);
} }
}; };