Detect when the machine wakes from sleep, re-sync mail immediately #468

This commit is contained in:
Ben Gotow 2017-12-23 10:02:57 -07:00
parent 1cd8ce2682
commit a498796147
3 changed files with 51 additions and 72 deletions

44
app/package-lock.json generated
View file

@ -1,6 +1,6 @@
{
"name": "mailspring",
"version": "1.0.7",
"version": "1.0.11",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -3057,11 +3057,6 @@
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz",
"integrity": "sha1-M2w+/BIgrc7dosn6tntaeVWjNlg="
},
"regexp-quote": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/regexp-quote/-/regexp-quote-0.0.0.tgz",
"integrity": "sha1-Hg9GUMhi3L/tVP1CsUjpuxch/PI="
},
"repeat-string": {
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
@ -3175,43 +3170,6 @@
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
"integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
},
"sanitize-html": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/sanitize-html/-/sanitize-html-1.9.0.tgz",
"integrity": "sha1-mRBQcI+52k64IntFMHampTet8qA=",
"requires": {
"htmlparser2": "3.8.3",
"regexp-quote": "0.0.0",
"xtend": "4.0.1"
},
"dependencies": {
"domhandler": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.3.0.tgz",
"integrity": "sha1-LeWaCCLVAn+r/28DLCsloqir5zg=",
"requires": {
"domelementtype": "1.3.0"
}
},
"entities": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-1.0.0.tgz",
"integrity": "sha1-sph6o4ITR/zeZCsk/fyeT7cSvyY="
},
"htmlparser2": {
"version": "3.8.3",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.8.3.tgz",
"integrity": "sha1-mWwosZFRaovoZQGn15dX5ccMEGg=",
"requires": {
"domelementtype": "1.3.0",
"domhandler": "2.3.0",
"domutils": "1.5.1",
"entities": "1.0.0",
"readable-stream": "1.1.14"
}
}
}
},
"semver": {
"version": "4.3.6",
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz",

View file

@ -445,8 +445,8 @@ export default class MailsyncBridge {
return true;
};
_onOnlineStatusChanged = ({ onlineDidChange }) => {
if (onlineDidChange && OnlineStatusStore.isOnline()) {
_onOnlineStatusChanged = ({ onlineDidChange, wakingFromSleep }) => {
if (wakingFromSleep || (onlineDidChange && OnlineStatusStore.isOnline())) {
this.sendSyncMailNow();
}
};

View file

@ -14,6 +14,7 @@ class OnlineStatusStore extends MailspringStore {
this._interval = null;
this._timeout = null;
this._timeoutTargetTime = null;
this._backoffScheduler = new ExponentialBackoffScheduler({ jitter: false });
if (AppEnv.isMainWindow()) {
@ -30,49 +31,69 @@ class OnlineStatusStore extends MailspringStore {
return this._countdownSeconds;
}
async _setNextOnlineState() {
_checkOnlineStatus = async () => {
isOnlineModule = isOnlineModule || require('is-online'); //eslint-disable-line
const nextIsOnline = await isOnlineModule();
if (this._online !== nextIsOnline) {
this._online = nextIsOnline;
this.trigger({ onlineDidChange: true, countdownDidChange: false });
}
}
_checkOnlineStatus = async () => {
clearInterval(this._interval);
clearTimeout(this._timeout);
// If we are currently offline, this trigger will show `Retrying now...`
this._countdownSeconds = 0;
this.trigger({ onlineDidChange: false, countdownDidChange: true });
// If we're more than a minute "late", we probably went to sleep
// and are now waking.
const wakingFromSleep =
this._timeoutTargetTime && Date.now() > this._timeoutTargetTime + 1000 * 60;
this._timeoutTargetTime = null;
await this._setNextOnlineState();
// If we are currently offline, this trigger will show `Retrying now...`
if (this._countdownSeconds > 0) {
this._countdownSeconds = 0;
this.trigger({
onlineDidChange: false,
wakingFromSleep: false,
countdownDidChange: true,
});
}
const nextIsOnline = await isOnlineModule({ timeout: 10000 });
const onlineDidChange = this._online !== nextIsOnline;
this._online = nextIsOnline;
if (wakingFromSleep || onlineDidChange) {
this.trigger({
onlineDidChange,
wakingFromSleep,
countdownDidChange: false,
});
}
if (this._online) {
// just check again later
this._backoffScheduler.reset();
this._timeoutTargetTime = Date.now() + CHECK_ONLINE_INTERVAL;
this._timeout = setTimeout(this._checkOnlineStatus, CHECK_ONLINE_INTERVAL);
} else {
// count down an inreasing delay and check again
this._countdownSeconds = Math.ceil(this._backoffScheduler.nextDelay() / 1000);
this._interval = setInterval(() => {
this._countdownSeconds = Math.max(0, this._countdownSeconds - 1);
if (this._countdownSeconds === 0) {
this._checkOnlineStatus();
} else {
// if the countdown is greater than 10 seconds we only update every 5
// seconds just for a tiny, tiny offline performance improvement
// 45, 30, 15, 10, 9, 8, 7...
if (this._countdownSeconds > 30 && this._countdownSeconds % 15 !== 0) {
return;
}
if (this._countdownSeconds > 10 && this._countdownSeconds % 5 !== 0) {
return;
}
this.trigger({ onlineDidChange: false, countdownDidChange: true });
const next = Math.max(0, this._countdownSeconds - 1);
if (next === 0) {
return this._checkOnlineStatus();
}
this._countdownSeconds = next;
// if the countdown is greater than 10 seconds we only update every 5
// seconds just for a tiny, tiny offline performance improvement
// 45, 30, 15, 10, 9, 8, 7...
if (this._countdownSeconds > 30 && this._countdownSeconds % 15 !== 0) {
return;
}
if (this._countdownSeconds > 10 && this._countdownSeconds % 5 !== 0) {
return;
}
this.trigger({
onlineDidChange: false,
wakingFromSleep: false,
countdownDidChange: true,
});
}, 1000);
}
};