mirror of
https://github.com/discourse/discourse.git
synced 2024-12-01 20:33:43 +08:00
499472b6a0
Per new lifecycle https://developers.google.com/web/updates/2018/07/page-lifecycle-api On Android and latest Chrome when an app transitions from "frozen" to active the new "resume" event fires with no accompanying "visibilitychange" event. This means that often background tabs may be stuck thinking that discourse has no focus when, indeed, it has. This leads to cases where no posts are marked read anymore.
41 lines
911 B
JavaScript
41 lines
911 B
JavaScript
function gotFocus() {
|
|
if (!Discourse.get("hasFocus")) {
|
|
Discourse.setProperties({ hasFocus: true, notify: false });
|
|
}
|
|
}
|
|
|
|
function lostFocus() {
|
|
if (Discourse.get("hasFocus")) {
|
|
Discourse.set("hasFocus", false);
|
|
}
|
|
}
|
|
|
|
let onchange;
|
|
|
|
export default Ember.Mixin.create({
|
|
ready() {
|
|
this._super(...arguments);
|
|
|
|
onchange = () => {
|
|
document.visibilityState === "hidden" ? lostFocus() : gotFocus();
|
|
};
|
|
|
|
// Default to true
|
|
Discourse.set("hasFocus", true);
|
|
|
|
document.addEventListener("visibilitychange", onchange);
|
|
document.addEventListener("resume", onchange);
|
|
document.addEventListener("freeze", onchange);
|
|
},
|
|
|
|
reset() {
|
|
this._super(...arguments);
|
|
|
|
document.removeEventListener("visibilitychange", onchange);
|
|
document.removeEventListener("resume", onchange);
|
|
document.removeEventListener("freeze", onchange);
|
|
|
|
onchange = undefined;
|
|
}
|
|
});
|