Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #1 - update abort method on MQHandler requests #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ require("./utils/interceptors").enable(module.exports);
const PollRequestManager = require("./utils/PollRequestManager");
const rm = new PollRequestManager(module.exports.fetch);

module.exports.poll = function (url, options, connectionTimeout, delayStart) {
module.exports.poll = function (url, options, connectionTimeout, delayStart, abortController) {
connectionTimeout = connectionTimeout || 10000;
rm.setConnectionTimeout(connectionTimeout);
const request = rm.createRequest(url, options, delayStart);
const request = rm.createRequest(url, options, delayStart, abortController);
return request;
};

Expand Down
233 changes: 92 additions & 141 deletions http/utils/PollRequestManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@ function PollRequestManager(fetchFunction, connectionTimeout = 10000, pollingTi

const requests = new Map();

function Request(url, options, delay = 0) {
let promiseHandlers = {};
let currentState = undefined;
function Request(url, options, delay = 0, abortController) {

let timeout;
this.url = url;
let abortController;
let previousAbortController;

this.execute = function() {
if (typeof AbortController !== "undefined") {
if (typeof abortController === "undefined") {
previousAbortController = new AbortController()
} else {
previousAbortController = abortController;
}
abortController = new AbortController();
options.signal = previousAbortController.signal;
}
if (!currentState && delay) {
let currentState;
options.signal = abortController.signal; // Always overwrite request options to prevent external overwriting
if (!timeout && delay) {
currentState = new Promise((resolve, reject) => {
timeout = setTimeout(() => {
fetchFunction(url, options).then((response) => {
Expand All @@ -36,73 +26,25 @@ function PollRequestManager(fetchFunction, connectionTimeout = 10000, pollingTi
return currentState;
}

this.cancelExecution = function() {
this.abort = () => {
clearTimeout(timeout);
timeout = undefined;
if(typeof currentState !== "undefined"){
currentState = undefined;
}
promiseHandlers.resolve = (...args) => {console.log("(not important) Resolve called after cancel execution with the following args", ...args)};
promiseHandlers.reject = (...args) => {console.log("(not important) Reject called after cancel execution with the following args", ...args)};
}

this.setExecutor = function(resolve, reject) {
if(promiseHandlers.resolve){
return reject(new Error("Request already in progress"));
}
promiseHandlers.resolve = resolve;
promiseHandlers.reject = reject;
}

this.resolve = function(...args) {
promiseHandlers.resolve(...args);
this.destroy();
promiseHandlers = {};
}

this.reject = function(...args) {
if(promiseHandlers.reject){
promiseHandlers.reject(...args);
}
this.destroy();
promiseHandlers = {};
}

this.destroy = function(removeFromPool = true) {
this.cancelExecution();

if (!removeFromPool) {
return;
}

// Find our identifier
const requestsEntries = requests.entries()
let identifier;
for (const [key, value] of requestsEntries) {
if (value === this) {
identifier = key;
break;
}
}

if (identifier) {
requests.delete(identifier);
}
}

this.abort = () => {
if (typeof previousAbortController !== "undefined") {
previousAbortController.abort();
}
abortController.abort();
}
}

this.createRequest = function (url, options, delayedStart = 0) {
const request = new Request(url, options, delayedStart);
this.createRequest = function (url, options, delayedStart = 0, abortController) {
if (!abortController)
abortController = new AbortController();

const request = new Request(url, options, delayedStart, abortController);

const promise = new Promise((resolve, reject) => {
request.setExecutor(resolve, reject);
createPollingTask(request);
createPollingTask(request).then((response) => {
resolve(response);
}).catch((err) => {
reject(err);
})
});
promise.abort = () => {
this.cancelRequest(promise);
Expand All @@ -120,8 +62,10 @@ function PollRequestManager(fetchFunction, connectionTimeout = 10000, pollingTi

const request = requests.get(promiseOfRequest);
if (request) {
request.destroy(false);
request.abort();
requests.delete(promiseOfRequest);
} else {
console.warn("No active request found.");
}
}

Expand All @@ -131,84 +75,91 @@ function PollRequestManager(fetchFunction, connectionTimeout = 10000, pollingTi

/* *************************** polling zone ****************************/
function createPollingTask(request) {
let safePeriodTimeoutHandler;
let serverResponded = false;
/**
* default connection timeout in api-hub is @connectionTimeout
* we wait double the time before aborting the request
*/
function beginSafePeriod() {
safePeriodTimeoutHandler = setTimeout(() => {
if (!serverResponded) {
request.abort();
}
serverResponded = false;
beginSafePeriod()
}, connectionTimeout * 2);
reArm();
}
return new Promise((resolve, reject) => {
let safePeriodTimeoutHandler;
let serverResponded = false;
/**
* default connection timeout in api-hub is @connectionTimeout
* we wait double the time before aborting the request
*/
function beginSafePeriod() {
safePeriodTimeoutHandler = setTimeout(() => {
if (!serverResponded) {
request.abort();
}
serverResponded = false;
beginSafePeriod()
}, connectionTimeout * 2);
reArm();
}

function endSafePeriod(serverHasResponded) {
serverResponded = serverHasResponded;
function endSafePeriod(serverHasResponded) {
serverResponded = serverHasResponded;

clearTimeout(safePeriodTimeoutHandler);
}
clearTimeout(safePeriodTimeoutHandler);
}

function reArm() {
request.execute().then( (response) => {
if (!response.ok) {
endSafePeriod(true);
function reArm() {
request.execute().then( (response) => {
if (!response.ok) {
endSafePeriod(true);

//todo check for http errors like 404
if (response.status === 403) {
reject(Error("Token expired"));
return
}

if (response.status === 503){
let err = Error(response.statusText || "Service unavailable");
err.code = 503;
throw err;
return;
}

//todo check for http errors like 404
if (response.status === 403) {
request.reject(Error("Token expired"));
return
return beginSafePeriod();
}

if (response.status === 503){
let err = Error(response.statusText || "Service unavailable");
err.code = 503;
throw err;
if (response.status === 204) {
endSafePeriod(true);
beginSafePeriod();
return;
}

return beginSafePeriod();
}

if (response.status === 204) {
endSafePeriod(true);
beginSafePeriod();
return;
}
if (safePeriodTimeoutHandler) {
clearTimeout(safePeriodTimeoutHandler);
}

if (safePeriodTimeoutHandler) {
clearTimeout(safePeriodTimeoutHandler);
}
resolve(response);
}).catch( (err) => {
switch (err.code) {
case "ETIMEDOUT":
case "ECONNREFUSED":
endSafePeriod(true);
beginSafePeriod();
break;
case 20:
case "ERR_NETWORK_IO_SUSPENDED":
//reproduced when user is idle on ios (chrome).
case "ERR_INTERNET_DISCONNECTED":
//indicates a general network failure.
break;
case "ABORT_ERR":
endSafePeriod(true);
reject(err);
break;
default:
console.log("abnormal error: ", err);
endSafePeriod(true);
reject(err);
}
});

request.resolve(response);
}).catch( (err) => {
switch (err.code) {
case "ETIMEDOUT":
case "ECONNREFUSED":
endSafePeriod(true);
beginSafePeriod();
break;
case 20:
case "ERR_NETWORK_IO_SUSPENDED":
//reproduced when user is idle on ios (chrome).
case "ERR_INTERNET_DISCONNECTED":
//indicates a general network failure.
break;
default:
console.log("abnormal error: ", err);
endSafePeriod(true);
request.reject(err);
}
});
}

}
beginSafePeriod();

beginSafePeriod();
})
}

}
Expand Down
Loading