Skip to content

Commit 917f295

Browse files
committed
Adding debounce to function that POSTs bookmark to avoid dozens of requests per second in some situations. Closes #1067
1 parent 626894b commit 917f295

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

web-server/js/slycat-bookmark-manager.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,49 @@ module.create = function(pid, mid)
8484
// Updates the bookmark state (privileged)
8585
manager.updateState = function(params)
8686
{
87+
// State needs to be updated each time updateState is called
8788
$.extend(state, params); // Consider using deep merge by adding true as the first parameter. However, deep merging does not work with bookmarking of expanded and collapsed dendrogram nodes since they are passed as arrays
88-
89-
// Store bookmark and update the bid
90-
if(req)
91-
req.abort();
92-
93-
req = $.ajax({
94-
type : "POST",
95-
url : api_root + "projects/" + pid + "/bookmarks",
96-
contentType : "application/json",
97-
data: JSON.stringify(state),
98-
processData: false,
99-
success: function(result)
100-
{
101-
bid = result.id;
102-
updateURL(bid);
103-
// Store latest bid in local storage
104-
if(mid != null)
105-
localStorage[mid] = bid;
106-
req = null;
107-
},
108-
});
89+
90+
// But we don't have to post the bookmark each time, so calling a seaparte postBookmark
91+
// function that is debounced (or throttled in the future?) to limit number of POSTs to the backend
92+
manager.postBookmark();
10993
}
11094

95+
// Using debounce to only POST bookmark every 1/3 second (333 ms) so we don't
96+
// inundate the server with dozens of requests per second when user does something
97+
// that updates state often, like drag a pin around the screen.
98+
manager.postBookmark = _.debounce(
99+
// This is the function that POSTs a bookmark to the server
100+
function(params)
101+
{
102+
// Store bookmark and update the bid
103+
if(req)
104+
req.abort();
105+
106+
req = $.ajax({
107+
type : "POST",
108+
url : api_root + "projects/" + pid + "/bookmarks",
109+
contentType : "application/json",
110+
data: JSON.stringify(state),
111+
processData: false,
112+
success: function(result)
113+
{
114+
bid = result.id;
115+
updateURL(bid);
116+
// Store latest bid in local storage
117+
if(mid != null)
118+
localStorage[mid] = bid;
119+
req = null;
120+
},
121+
});
122+
},
123+
// How long to wait before actually running that function.
124+
// Debounce just waits this long and then executes the last call to postBookmark.
125+
// But in the future we might want to use throttle function instead, which
126+
// would call postBookmark every 333 seconds.
127+
333
128+
);
129+
111130
// Retrieves the state for the current bookmark id (asynchronous)
112131
manager.getState = function(callback)
113132
{

0 commit comments

Comments
 (0)