mirror of
https://github.com/zoriya/flood.git
synced 2026-06-01 10:35:59 +00:00
Close event stream after 30 seconds of inactivity
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
via an event-stream. This significantly reduces data usage on the Flood client
|
||||
* Stream covers torrent list, transfer rate summary & history,
|
||||
torrent taxonomy, and notification count.
|
||||
* Close event stream after the window/tab has been inactive for 30 seconds
|
||||
|
||||
## [1.0.0] (April 21, 2017)
|
||||
* First "official" release
|
||||
|
||||
@@ -10,7 +10,33 @@ import serverEventTypes from '../../../shared/constants/serverEventTypes';
|
||||
const baseURI = ConfigStore.getBaseURI();
|
||||
|
||||
let activityStreamEventSource = null;
|
||||
let lastHistorySnapshot = null;
|
||||
let lastActivityStreamOptions = undefined;
|
||||
let visibilityChangeTimeout = null;
|
||||
|
||||
const handleProlongedInactivity = () => {
|
||||
FloodActions.closeActivityStream();
|
||||
};
|
||||
|
||||
const handleWindowVisibilityChange = () => {
|
||||
if (global.document.hidden) {
|
||||
// After 30 seconds of inactivity, we stop the event stream.
|
||||
visibilityChangeTimeout = global.setTimeout(
|
||||
handleProlongedInactivity,
|
||||
1000 * 30
|
||||
);
|
||||
} else {
|
||||
global.clearTimeout(visibilityChangeTimeout);
|
||||
|
||||
if (activityStreamEventSource == null) {
|
||||
FloodActions.startActivityStream(lastActivityStreamOptions);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
global.document.addEventListener(
|
||||
'visibilitychange',
|
||||
handleWindowVisibilityChange
|
||||
);
|
||||
|
||||
const FloodActions = {
|
||||
clearNotifications: (options) => {
|
||||
@@ -216,16 +242,19 @@ const FloodActions = {
|
||||
});
|
||||
},
|
||||
|
||||
restartActivityStream(options) {
|
||||
restartActivityStream() {
|
||||
this.closeActivityStream();
|
||||
this.startActivityStream(options);
|
||||
this.startActivityStream(lastActivityStreamOptions);
|
||||
},
|
||||
|
||||
startActivityStream(options = {}) {
|
||||
const {historySnapshot = historySnapshotTypes.FIVE_MINUTE} = options;
|
||||
const didHistorySnapshotChange = lastHistorySnapshot !== historySnapshot;
|
||||
const didHistorySnapshotChange = (
|
||||
lastActivityStreamOptions
|
||||
&& lastActivityStreamOptions.historySnapshot !== historySnapshot
|
||||
);
|
||||
|
||||
lastHistorySnapshot = historySnapshot;
|
||||
lastActivityStreamOptions = options;
|
||||
|
||||
// When the user requests a new history snapshot during an open session,
|
||||
// we need to close and re-open the event stream.
|
||||
@@ -235,7 +264,7 @@ const FloodActions = {
|
||||
|
||||
// If the user requested a new history snapshot, or the event source has not
|
||||
// alraedy been created, we open the event stream.
|
||||
if (didHistorySnapshotChange || activityStreamEventSource == null) {
|
||||
if (didHistorySnapshotChange || activityStreamEventSource === null) {
|
||||
activityStreamEventSource = new EventSource(
|
||||
`${baseURI}api/activity-stream?historySnapshot=${historySnapshot}`
|
||||
);
|
||||
|
||||
+46
-46
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user