Skip to content

Commit

Permalink
SAK-40861 use the browser visibility state to avoid fetches when hidd…
Browse files Browse the repository at this point in the history
…en and to fetch as soon as we are visible again
  • Loading branch information
ottenhoff authored and Miguel Pellicer committed Oct 31, 2018
1 parent aab3c31 commit 78656f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected void init() throws Exception
.build();
heartbeatMap = CacheBuilder.newBuilder()
//.recordStats()
.expireAfterAccess(pollInterval*2, TimeUnit.MILLISECONDS)
.expireAfterWrite(1, TimeUnit.HOURS)
.build();

timezoneCache = CacheBuilder.newBuilder()
Expand Down Expand Up @@ -1362,7 +1362,7 @@ private TransferableChatMessage addHeartBeat(String channelId, String sessionKey
ret = TransferableChatMessage.HeartBeat(channelId, sessionKey);
heartbeatMap.get(channelId, () -> {
return CacheBuilder.newBuilder()
.expireAfterWrite(pollInterval*2, TimeUnit.MILLISECONDS)
.expireAfterWrite(1, TimeUnit.HOURS)
.build();
}).put(sessionId, ret);
} catch(Exception e){
Expand All @@ -1382,8 +1382,12 @@ private boolean isOnline(String channelId, String sessionId) {
return false;
}

//thanks to the cache auto-expiration system, not updated hearbeats will be automatically removed
return (heartbeatMap.getIfPresent(channelId).getIfPresent(sessionId) != null);
// Check to see how active the user has been
TransferableChatMessage userHeartbeat = heartbeatMap.getIfPresent(channelId).getIfPresent(sessionId);
long timeDiff = ((new Date()).getTime()) - userHeartbeat.getTimestamp();
log.debug("Heartbeat diff for {} is {}; interval={}", sessionId, timeDiff, pollInterval*2);
// Safari seems to back off on setTimeout calls when in background for 60 seconds
return timeDiff <= 60000 + (pollInterval*2);
}

private void sendToCluster(TransferableChatMessage message){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,13 @@ public Map<String,Object> handleChatData(EntityReference ref, Map<String,Object>
log.debug("No siteId specified");
throw new SecurityException("You must be specify the site ID");
}
log.debug("siteId: {}", siteId);

String channelId = (String) params.get("channelId");
if (StringUtils.isBlank(channelId)) {
log.debug("No channelId specified");
throw new SecurityException("You must be specify the channel ID");
}
log.debug("channelId: {}", channelId);
log.debug("channelId: {}, siteId: {}, sessionKey: {}", channelId, siteId, chatManager.getSessionKey());

ChatChannel channel = chatManager.getChatChannel(channelId);
if (!chatManager.getCanReadMessage(channel)) {
Expand Down
26 changes: 22 additions & 4 deletions chat/chat-tool/tool/src/webapp/js/chatscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
* limitations under the License.
*
**********************************************************************************/

// When user goes between tabs or back from home screen, we monitor and updateChatData
document.addEventListener('visibilitychange', function() {
if (document.visibilityState == 'hidden' || document.visibilityState == 'visible') {
chatscript.changePageVisibility(document.visibilityState);
}
});

var chatscript = {
url_submit : "/direct/chat-message/",
keycode_enter : 13,
pollInterval : 5000,
currentChatChannelId : null,
timeoutVar : null,

pageVisibility : 'visible',

init : function(){
var me = this;

Expand Down Expand Up @@ -101,6 +110,12 @@ var chatscript = {
});
});
},
changePageVisibility : function(newVisibility) {
this.pageVisibility = newVisibility;
if (newVisibility == 'visible') {
this.updateChatData();
}
},
sendMessage : function(params, textarea, submitButton) {
var me = this;
var errorSubmit = $("#errorSubmit");
Expand Down Expand Up @@ -143,9 +158,12 @@ var chatscript = {
if(this.timeoutVar != null) {
clearTimeout(this.timeoutVar);
}
this.timeoutVar = setTimeout(function() {
me.updateChatData();
}, this.pollInterval);
// If the tab is hidden, no use in firing AJAX requests for new chat data
if(this.pageVisibility != 'hidden') {
this.timeoutVar = setTimeout(function() {
me.updateChatData();
}, this.pollInterval);
}
},
doUpdateChatData : function() {
var me = this;
Expand Down

0 comments on commit 78656f6

Please sign in to comment.