Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit bd208a6

Browse files
author
Daniel Brain
committed
Increase ack timeout for windows known to have post-robot
1 parent 17075b7 commit bd208a6

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/conf/config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ export let CONFIG : Object = {
1111
BRIDGE_TIMEOUT: 5000,
1212
CHILD_WINDOW_TIMEOUT: 5000,
1313

14-
ACK_TIMEOUT: (window.navigator.userAgent.match(/MSIE/i) !== -1 && !__TEST__) ? 2000 : 1000,
15-
RES_TIMEOUT: __TEST__ ? 2000 : -1,
14+
ACK_TIMEOUT: 2000,
15+
ACK_TIMEOUT_KNOWN: 10000,
16+
RES_TIMEOUT: __TEST__ ? 2000 : -1,
1617

1718
ALLOWED_POST_MESSAGE_METHODS: {
1819
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE ]: true,

src/drivers/receive/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isWindowClosed, type CrossDomainWindowType } from 'cross-domain-utils/s
44
import { addEventListener, noop } from 'belter/src';
55

66
import { CONSTANTS, POST_MESSAGE_NAMES_LIST } from '../../conf';
7-
import { deserializeMethods } from '../../lib';
7+
import { deserializeMethods, markWindowKnown } from '../../lib';
88
import { global } from '../../global';
99

1010
import { RECEIVE_MESSAGE_TYPES } from './types';
@@ -75,6 +75,8 @@ export function receiveMessage(event : MessageEvent) {
7575
return;
7676
}
7777

78+
markWindowKnown(source);
79+
7880
if (!message.sourceDomain || typeof message.sourceDomain !== 'string') {
7981
throw new Error(`Expected message to have sourceDomain`);
8082
}

src/interface.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { global } from './global';
66
import { on, send } from './public';
77

88
export * from './public';
9+
export { markWindowKnown } from './lib';
910
export { cleanUpWindow } from './clean';
1011
export { ZalgoPromise as Promise } from 'zalgo-promise/src';
1112
export let bridge = __POST_ROBOT__.__IE_POPUP_SUPPORT__ ? require('./bridge/interface') : null;

src/lib/ready.js

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { global } from '../global';
1010

1111

1212
global.readyPromises = global.readyPromises || new WeakMap();
13+
global.knownWindows = global.knownWindows || new WeakMap();
1314

1415
export function onHello(handler : ({ source? : CrossDomainWindowType, origin? : string }) => void) {
1516
global.on(CONSTANTS.POST_MESSAGE_NAMES.HELLO, { domain: CONSTANTS.WILDCARD }, ({ source, origin }) => {
@@ -24,6 +25,14 @@ export function sayHello(win : CrossDomainWindowType) : ZalgoPromise<{ origin :
2425
});
2526
}
2627

28+
export function markWindowKnown(win : CrossDomainWindowType) {
29+
global.knownWindows.set(win, true);
30+
}
31+
32+
export function isWindowKnown(win : CrossDomainWindowType) : boolean {
33+
return global.knownWindows.get(win);
34+
}
35+
2736
export function initOnReady() {
2837

2938
onHello(({ source, origin }) => {

src/public/client.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { uniqueID, isRegex } from 'belter/src';
88

99
import { CONFIG, CONSTANTS } from '../conf';
1010
import { sendMessage, addResponseListener, deleteResponseListener, markResponseListenerErrored, type ResponseListenerType } from '../drivers';
11-
import { onChildWindowReady, sayHello } from '../lib';
11+
import { onChildWindowReady, sayHello, isWindowKnown } from '../lib';
1212
import { global } from '../global';
1313

1414
global.requestPromises = global.requestPromises || new WeakMap();
@@ -168,8 +168,11 @@ export function request(options : RequestOptionsType) : ZalgoPromise<ResponseMes
168168
return resolve();
169169
}
170170

171-
let ackTimeout = CONFIG.ACK_TIMEOUT;
172-
let resTimeout = options.timeout || CONFIG.RES_TIMEOUT;
171+
let totalAckTimeout = isWindowKnown(win) ? CONFIG.ACK_TIMEOUT_KNOWN : CONFIG.ACK_TIMEOUT;
172+
let totalResTimeout = options.timeout || CONFIG.RES_TIMEOUT;
173+
174+
let ackTimeout = totalAckTimeout;
175+
let resTimeout = totalResTimeout;
173176

174177
let cycleTime = 100;
175178

@@ -204,10 +207,10 @@ export function request(options : RequestOptionsType) : ZalgoPromise<ResponseMes
204207
cycleTime = Math.min(resTimeout, 2000);
205208

206209
} else if (ackTimeout === 0) {
207-
return reject(new Error(`No ack for postMessage ${ name } in ${ getDomain() } in ${ CONFIG.ACK_TIMEOUT }ms`));
210+
return reject(new Error(`No ack for postMessage ${ name } in ${ getDomain() } in ${ totalAckTimeout }ms`));
208211

209212
} else if (resTimeout === 0) {
210-
return reject(new Error(`No response for postMessage ${ name } in ${ getDomain() } in ${ options.timeout || CONFIG.RES_TIMEOUT }ms`));
213+
return reject(new Error(`No response for postMessage ${ name } in ${ getDomain() } in ${ totalResTimeout }ms`));
211214
}
212215

213216
setTimeout(cycle, cycleTime);

0 commit comments

Comments
 (0)