Skip to content

Commit

Permalink
Merge pull request #540 from aeternity/hotfix/poll-aepps-in-reverse-i…
Browse files Browse the repository at this point in the history
…frame

hotfix: Poll aepps in reverse iframe
  • Loading branch information
mradkov authored Oct 5, 2020
2 parents b9d3cb5 + 38fdb4a commit 9d084f9
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions src/lib/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,30 +154,44 @@ export default {
});

if (IN_FRAME) {
const connectedFrames = new Set();
const connectToFrame = target => {
if (connectedFrames.has(target)) return;
connectedFrames.add(target);
const connection = BrowserWindowMessageConnection({ target });
const originalConnect = connection.connect;
connection.connect = function connect(onMessage) {
originalConnect.call(this, (data, origin, source) => {
if (source !== target) return;
onMessage(data, origin, source);
});
};
sdk.addRpcClient(connection);
sdk.shareWalletInfo(connection.sendMessage.bind(connection));
setTimeout(() => sdk.shareWalletInfo(connection.sendMessage.bind(connection)), 3000);
const getArrayOfAvailableFrames = () => [
window.parent,
...times(window.parent.frames.length, i => window.parent.frames[i]),
];
const executeAndSetInterval = (handler, timeout) => {
handler();
return setInterval(handler, timeout);
};

connectToFrame(window.parent);
const connectToParentFrames = () =>
times(window.parent.frames.length, i => window.parent.frames[i])
.filter(frame => frame !== window)
.forEach(connectToFrame);
connectToParentFrames();
setInterval(connectToParentFrames, 3000);
const connectedFrames = new Set();
executeAndSetInterval(
() =>
getArrayOfAvailableFrames()
.filter(frame => frame !== window)
.forEach(target => {
if (connectedFrames.has(target)) return;
connectedFrames.add(target);
const connection = BrowserWindowMessageConnection({ target });
const originalConnect = connection.connect;
let intervalId;
connection.connect = function connect(onMessage) {
originalConnect.call(this, (data, origin, source) => {
if (source !== target) return;
clearInterval(intervalId);
onMessage(data, origin, source);
});
};
sdk.addRpcClient(connection);
intervalId = executeAndSetInterval(() => {
if (!getArrayOfAvailableFrames().includes(target)) {
clearInterval(intervalId);
return;
}
sdk.shareWalletInfo(connection.sendMessage.bind(connection));
}, 3000);
}),
3000,
);
}

await store.commit('initSdk', sdk);
Expand Down

1 comment on commit 9d084f9

@davidyuk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.