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

fix: Ads not loading - Enqueue window post messages until it's ready #1349

Merged
merged 2 commits into from
Sep 25, 2018
Merged
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
56 changes: 13 additions & 43 deletions packages/ad/__tests__/utils/webview-event-callback-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export default () => {
beforeEach(() => {
jest.useFakeTimers();
window = {
reactBridgePostMessage: jest.fn().mockImplementation((data, origin) => {
window.reactBridgePostMessageDecoded(JSON.parse(data), origin);
postMessage: jest.fn().mockImplementation(data => {
window.reactBridgePostMessageDecoded(JSON.parse(data));
}),
originalPostMessage: {},
reactBridgePostMessageDecoded: jest.fn(),
requestAnimationFrame: realWindow.requestAnimationFrame,
setTimeout: realWindow.setTimeout,
Expand Down Expand Up @@ -43,31 +44,6 @@ export default () => {
expect(window.eventCallback).toEqual(expect.any(Function));
});

it("posts a delayed message to the parent when the event callback is called", () => {
webviewEventCallbackSetup({ window });
window.eventCallback("TYPE", "DETAIL");
expect(window.reactBridgePostMessageDecoded).not.toHaveBeenCalled();
jest.runAllTimers();
expect(window.reactBridgePostMessageDecoded).toHaveBeenCalledWith(
{
isTngMessage: true,
type: "TYPE",
detail: "DETAIL"
},
"*"
);
});

it("preferrs reactBridgePostMessage if available", () => {
webviewEventCallbackSetup({ window });
window.postMessage = jest.fn();
window.reactBridgePostMessage = jest.fn();
window.eventCallback("TYPE", "DETAIL");
jest.runAllTimers();
expect(window.postMessage).toHaveBeenCalledTimes(0);
expect(window.reactBridgePostMessage).toHaveBeenCalledTimes(1);
});

it("falls abck to postMessage if reactBridgePostMessage if not available", () => {
webviewEventCallbackSetup({ window });
window.postMessage = jest.fn();
Expand All @@ -81,14 +57,11 @@ export default () => {
webviewEventCallbackSetup({ window });
errorHandler(error);
jest.runAllTimers();
expect(window.reactBridgePostMessageDecoded).toHaveBeenCalledWith(
{
isTngMessage: true,
type: "error",
detail: messageDetail
},
"*"
);
expect(window.reactBridgePostMessageDecoded).toHaveBeenCalledWith({
isTngMessage: true,
type: "error",
detail: messageDetail
});
};

it("posts a message to the parent when the global error handler is called", () => {
Expand Down Expand Up @@ -127,13 +100,10 @@ export default () => {
webviewEventCallbackSetup({ window });
window.console.error("a", "b", "c");
jest.runAllTimers();
expect(window.reactBridgePostMessageDecoded).toHaveBeenCalledWith(
{
isTngMessage: true,
type: "error",
detail: "a\nb\nc"
},
"*"
);
expect(window.reactBridgePostMessageDecoded).toHaveBeenCalledWith({
isTngMessage: true,
type: "error",
detail: "a\nb\nc"
});
});
};
47 changes: 35 additions & 12 deletions packages/ad/src/utils/webview-event-callback-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,42 @@

const webviewEventCallbackSetup = options => {
const { window } = options;

// Enqueue window messages until it's ready - See https://github.com/facebook/react-native/issues/11594#issuecomment-274689549 for details
let isReactNativePostMessageReady = !!window.originalPostMessage;
const queue = [];
let currentPostMessageFn = function store(message) {
if (queue.length > 100) queue.shift();
queue.push(message);
};

const sendQueue = () => {
while (queue.length > 0) window.postMessage(queue.shift());
};

if (!isReactNativePostMessageReady) {
Object.defineProperty(window, "postMessage", {
configurable: true,
enumerable: true,
get() {
return currentPostMessageFn;
},
set(fn) {
currentPostMessageFn = fn;
isReactNativePostMessageReady = true;
window.setTimeout(sendQueue, 0);
}
});
}

window.eventCallback = (type, detail) => {
// delay until next frame as React Native message bridge is not set up immediately
window.setTimeout(() => {
const method = window.reactBridgePostMessage || window.postMessage;
method(
JSON.stringify({
isTngMessage: true,
type,
detail
}),
"*"
craigbilner marked this conversation as resolved.
Show resolved Hide resolved
);
}, 300);
window.postMessage(
JSON.stringify({
isTngMessage: true,
type,
detail
})
);
};
window.addEventListener("error", ev => {
const file = (ev.filename || "").substring(0, 100);
Expand Down