Skip to content

Instantly share code, notes, and snippets.

@gundisalwa
Last active August 29, 2015 14:17
Show Gist options
  • Save gundisalwa/84f294b2b34f6bea2db5 to your computer and use it in GitHub Desktop.
Save gundisalwa/84f294b2b34f6bea2db5 to your computer and use it in GitHub Desktop.
Mock for a CDE dashboard watcher
function isEditorActive(){
return document.hasFocus();
}
function getActiveDashboardStateId (){
// Returns an identifier for the current saved state of the dashboard.
}
function getSavedDashboardStateId( ){
return $.ajax('dashboard_state_endpoint_url');
}
function promptReload(){
// Prompts the user to reload the dashboard
}
function throttle(){
// Some throttle implementation. Underscore.js has one.
}
var throttledTest = throttle( function ( callback ){
return getSavedDashboardStateId();
}, 1000 );
function handleSavedStateResponse ( newSavedStateId ){
if ( newSavedStateId !== getActiveDashboardStateId() ){
promptReload();
} else if ( !isEditorActive() ){
throttledTest( ).then( handleSavedStateResponse );
}
}
function startSavedStateTest(){
throttledTest( ).then( handleSavedStateResponse );
}
//---------------------------------------//
$(window).blur( startSavedStateTest );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment