@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
60 lines • 1.89 kB
JavaScript
import { ERROR_MESSAGE } from '../errors';
import { HostEvent } from '../types';
/**
* Reloads the ThoughtSpot iframe.
* @param iFrame
*/
export const reload = (iFrame) => {
const src = iFrame.src;
iFrame.src = '';
setTimeout(() => {
iFrame.src = src;
}, 100);
};
/**
* Post iframe message.
* @param iFrame
* @param message
* @param message.type
* @param message.data
* @param thoughtSpotHost
* @param channel
*/
function postIframeMessage(iFrame, message, thoughtSpotHost, channel) {
return iFrame.contentWindow.postMessage(message, thoughtSpotHost, [channel === null || channel === void 0 ? void 0 : channel.port2]);
}
export const TRIGGER_TIMEOUT = 30000;
/**
*
* @param iFrame
* @param messageType
* @param thoughtSpotHost
* @param data
*/
export function processTrigger(iFrame, messageType, thoughtSpotHost, data) {
return new Promise((res, rej) => {
if (messageType === HostEvent.Reload) {
reload(iFrame);
return res(null);
}
const channel = new MessageChannel();
channel.port1.onmessage = ({ data: responseData }) => {
var _a;
channel.port1.close();
const error = responseData.error || ((_a = responseData === null || responseData === void 0 ? void 0 : responseData.data) === null || _a === void 0 ? void 0 : _a.error);
if (error) {
rej(error);
}
else {
res(responseData);
}
};
// Close the messageChannel and resolve the promise if timeout.
setTimeout(() => {
channel.port1.close();
res(new Error(ERROR_MESSAGE.TRIGGER_TIMED_OUT));
}, TRIGGER_TIMEOUT);
return postIframeMessage(iFrame, { type: messageType, data }, thoughtSpotHost, channel);
});
}
//# sourceMappingURL=processTrigger.js.map