Skip to content

Commit

Permalink
Re-translate multi item URL if cached session is not found (zotero#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode authored and dstillman committed Oct 22, 2018
1 parent bc7a18e commit 1b5d429
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/webEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
const WebSession = require('./webSession');

// Timeout for select requests, in seconds
//const SELECT_TIMEOUT = 120;
const SELECT_TIMEOUT = 15;
const SELECT_TIMEOUT = 60;
const sessionsWaitingForSelection = {};

var requestsSinceGC = 0;
Expand Down Expand Up @@ -56,28 +55,36 @@ var WebEndpoint = module.exports = {
ctx.throw(400, "'session' not provided");
}
session = sessionsWaitingForSelection[sessionID];
if (!session) {
ctx.throw(400, "Session not found");
if (session) {
delete sessionsWaitingForSelection[sessionID];
session.ctx = ctx;
session.next = next;
session.data = data;
} else {
let single = !!ctx.request.query.single;
session = new WebSession(ctx, next, data.url, { single });
}
delete sessionsWaitingForSelection[sessionID];
session.ctx = ctx;
session.next = next;
session.data = data;
}
else {
if (!data.match(/^https?:/)) {
ctx.throw(400, "URL not provided");
}

let single = !!ctx.request.query.single;
session = new WebSession(ctx, next, data, { single });
}

if (typeof data != 'object' && !data.match(/^https?:/)) {
ctx.throw(400, "URL not provided");
}

await session.handleURL();

// Store session if returning multiple choices
if (ctx.response.status == 300) {
sessionsWaitingForSelection[session.id] = session;
if(typeof data == 'object') {
// Select item if this was an item selection query
session.data = data;
await session.handleURL();
} else {
// Store session if returning multiple choices
sessionsWaitingForSelection[session.id] = session;
}
}
}
};
Expand All @@ -86,7 +93,7 @@ var WebEndpoint = module.exports = {
* Perform garbage collection every 10 requests
*/
function gc() {
if ((++requestsSinceGC) == 3) {
if ((++requestsSinceGC) == 10) {
for (let i in sessionsWaitingForSelection) {
let session = sessionsWaitingForSelection[i];
if (session.started && Date.now() >= session.started + SELECT_TIMEOUT * 1000) {
Expand Down

0 comments on commit 1b5d429

Please sign in to comment.