Skip to content

Commit

Permalink
Make message response timeout logic less frequent
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Aug 1, 2017
1 parent 64cd9c6 commit 450ce51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/conf/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let CONFIG : Object = {
BRIDGE_TIMEOUT: 5000,

ACK_TIMEOUT: 1000,
RES_TIMEOUT: __TEST__ ? 2000 : 10000,
RES_TIMEOUT: __TEST__ ? 2000 : Infinity,

LOG_TO_PAGE: false,

Expand Down
37 changes: 24 additions & 13 deletions src/public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getAncestor, isAncestor, isWindowClosed } from 'cross-domain-utils/src'
import { CONFIG, CONSTANTS } from '../conf';
import { sendMessage, addResponseListener, deleteResponseListener } from '../drivers';
import { type ResponseListenerType } from '../drivers';
import { uniqueID, safeInterval, onWindowReady } from '../lib';
import { uniqueID, onWindowReady } from '../lib';
import { global } from '../global';

global.requestPromises = global.requestPromises || new WeakMap();
Expand Down Expand Up @@ -137,14 +137,15 @@ export function request(options : RequestOptionsType) : ZalgoPromise<ResponseMes
let ackTimeout = CONFIG.ACK_TIMEOUT;
let resTimeout = options.timeout || CONFIG.RES_TIMEOUT;

let interval = safeInterval(() => {
let cycleTime = 100;

if (responseListener.ack && hasResult) {
return interval.cancel();
let cycle = () => {

if (hasResult) {
return;
}

if (isWindowClosed(win)) {
interval.cancel();

if (!responseListener.ack) {
return reject(new Error(`Window closed for ${name} before ack`));
Expand All @@ -153,20 +154,30 @@ export function request(options : RequestOptionsType) : ZalgoPromise<ResponseMes
return reject(new Error(`Window closed for ${name} before response`));
}

ackTimeout -= 100;
resTimeout -= 100;
ackTimeout -= cycleTime;
resTimeout -= cycleTime;

let hasAck = responseListener.ack;

if (hasAck) {

if (resTimeout === Infinity) {
return;
}

if (ackTimeout <= 0 && !responseListener.ack) {
interval.cancel();
cycleTime = Math.min(resTimeout, 2000);

} else if (ackTimeout <= 0) {
return reject(new Error(`No ack for postMessage ${name} in ${CONFIG.ACK_TIMEOUT}ms`));
}

if (resTimeout <= 0 && !hasResult) {
interval.cancel();
} else if (resTimeout <= 0) {
return reject(new Error(`No response for postMessage ${name} in ${options.timeout || CONFIG.RES_TIMEOUT}ms`));
}

}, 100);
setTimeout(cycle, cycleTime);
};

setTimeout(cycle, cycleTime);
});
});

Expand Down

0 comments on commit 450ce51

Please # to comment.