Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/jt/interval-sync' into autostaging
Browse files Browse the repository at this point in the history
  • Loading branch information
stephherbers committed Feb 23, 2024
2 parents 235bc96 + bff5d24 commit 144e45c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
35 changes: 35 additions & 0 deletions corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ hqDefine("cloudcare/js/formplayer/app", function () {
});

FormplayerFrontend.on('startForm', function (data) {
FormplayerFrontend.permitIntervalSync = false;
FormplayerFrontend.getChannel().request("clearMenu");
hqRequire(["cloudcare/js/formplayer/menus/utils"], function (MenusUtils) {
MenusUtils.showBreadcrumbs(data.breadcrumbs);
Expand Down Expand Up @@ -542,6 +543,40 @@ hqDefine("cloudcare/js/formplayer/app", function () {
$.ajax(options);
});

FormplayerFrontend.getChannel().reply("interval_sync-db", function (unusedOptions) {
var user = FormplayerFrontend.getChannel().request('currentUser'),
username = user.username,
domain = user.domain,
formplayerUrl = user.formplayer_url,
complete,
data = {
"username": username,
"domain": domain,
"restoreAs": user.restoreAs,
"app_id": unusedOptions.appId,
},
options;

complete = function (response) {
if (response.status === 'retry') {
FormplayerFrontend.trigger('retry', response, function () {
options.data = JSON.stringify($.extend(true, {mustRestore: true}, data));
$.ajax(options);
}, gettext('Waiting for server progress'));
} else {
FormplayerFrontend.trigger('clearProgress');
CloudcareUtils.formplayerSyncComplete(response.status === 'error');
}
};
options = {
url: formplayerUrl + "/interval_sync-db",
data: JSON.stringify(data),
complete: complete,
};
FormplayerUtils.setCrossDomainAjaxOptions(options);
$.ajax(options);
});

/**
* retry
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ hqDefine("cloudcare/js/formplayer/constants", function () {
FIELD_CHANGE: "field_change",
},

//Custom Properties
POST_FORM_SYNC: "cc-sync-after-form",

SMALL_SCREEN_WIDTH_PX: 992,

BREADCRUMB_HEIGHT_PX: 46.125,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ hqDefine("cloudcare/js/formplayer/menus/api", function () {
return;
}

formplayerUtils.startIntervalSync(params.appId, parsedMenus?.metaData?.attemptRestore);
sessionStorage.setItem("lastUserActivityTime", Date.now());
FormplayerFrontend.trigger('clearProgress');
defer.resolve(parsedMenus);
// Only configure menu debugger if we didn't get a form entry response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hqDefine("cloudcare/js/formplayer/menus/collections", function () {
'type',
'noItemsText',
'dynamicSearch',
'metaData',
],

entityProperties: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ hqDefine("cloudcare/js/formplayer/middleware", function () {
'max-height': maxHeight + 'px',
});
};
var setLastUserActiviyTime = function () {
sessionStorage.setItem("lastUserActivityTime", Date.now());
};

var self = {};

Expand All @@ -41,6 +44,7 @@ hqDefine("cloudcare/js/formplayer/middleware", function () {
clearVersionInfo,
setScrollableMaxHeight,
clearBreadcrumbMiddleware,
setLastUserActiviyTime,
];

self.apply = function (api) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*global Backbone, DOMPurify */
hqDefine("cloudcare/js/formplayer/utils/utils", function () {
var initialPageData = hqImport("hqwebapp/js/initial_page_data"),
toggles = hqImport("hqwebapp/js/toggles");
toggles = hqImport("hqwebapp/js/toggles"),
constants = hqImport("cloudcare/js/formplayer/constants");

var Utils = {};

Expand Down Expand Up @@ -460,5 +461,49 @@ hqDefine("cloudcare/js/formplayer/utils/utils", function () {
});
};

Utils.startIntervalSync = function (appId, attemptedRestore) {
hqRequire(["cloudcare/js/formplayer/app"], function (FormplayerFrontend) {
let currentApp = FormplayerFrontend.getChannel().request("appselect:getApp", appId),
customProperties = currentApp?.attributes?.profile.custom_properties || {};
const FIVE_MINUTES_IN_MILLISECONDS = 1000 * 60 * 5;
FormplayerFrontend.permitIntervalSync = true;
if (attemptedRestore) {
Utils.setEnableIntervalSync(false);
}

let useAggressiveSyncTiming = (customProperties[constants.POST_FORM_SYNC] === "yes");
if (useAggressiveSyncTiming) {
// Sync frequency is synchronized with Formplayer's restore expiration
Utils.setEnableIntervalSync(true, FIVE_MINUTES_IN_MILLISECONDS);
}
});
};

Utils.setEnableIntervalSync = function (toggleOn, delayInMilliseconds) {
function shouldSync() {
let currentTime = Date.now(),
lastUserActivityTime = sessionStorage.getItem("lastUserActivityTime") || 0,
elapsedTimeSinceLastActivity = currentTime - (lastUserActivityTime),
isInApp = Utils.currentUrlToObject().appId !== undefined;
if (elapsedTimeSinceLastActivity <= delayInMilliseconds && isInApp) {
return true;
}
}

hqRequire(["cloudcare/js/formplayer/app"], function (FormplayerFrontend) {
if (!FormplayerFrontend.syncInterval && toggleOn) {
FormplayerFrontend.syncInterval = setInterval(function () {
if (shouldSync() && FormplayerFrontend.permitIntervalSync) {
let urlObject = Utils.currentUrlToObject();
FormplayerFrontend.getChannel().request("interval_sync-db", urlObject);
}
}, delayInMilliseconds);
} else if (FormplayerFrontend.syncInterval && !toggleOn) {
clearInterval(FormplayerFrontend.syncInterval);
FormplayerFrontend.syncInterval = null;
}
});
};

return Utils;
});

0 comments on commit 144e45c

Please sign in to comment.