Skip to content

Commit

Permalink
Use window.open as contingency to refocus window
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Mar 7, 2019
1 parent 61d8b91 commit f7c3f8e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/serialize/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,29 @@ type SerializedProxyWindow = {|
focus : () => ZalgoPromise<void>,
isClosed : () => ZalgoPromise<boolean>,
setLocation : (string) => ZalgoPromise<void>,
getName : () => ZalgoPromise<?string>,
setName : (string) => ZalgoPromise<void>,
getInstanceID : () => ZalgoPromise<string>
|};

function getSerializedWindow(id : string, win : CrossDomainWindowType, { send } : { send : SendType }) : SerializedProxyWindow {
let windowName;

return {
id,
type: getOpener(win) ? WINDOW_TYPE.POPUP : WINDOW_TYPE.IFRAME,
getInstanceID: () => getWindowInstanceID(win, { send }),
getInstanceID: memoizePromise(() => getWindowInstanceID(win, { send })),
close: () => ZalgoPromise.try(() => {
win.close();
}),
focus: () => ZalgoPromise.try(() => {
getName: () => ZalgoPromise.try(() => {
if (isWindowClosed(win)) {
return;
}

return windowName;
}),
focus: () => ZalgoPromise.try(() => {
win.focus();
}),
isClosed: () => ZalgoPromise.try(() => {
Expand Down Expand Up @@ -74,6 +84,8 @@ function getSerializedWindow(id : string, win : CrossDomainWindowType, { send }
if (win.frameElement) {
win.frameElement.setAttribute('name', name);
}

windowName = name;
})
};
}
Expand All @@ -85,6 +97,7 @@ export class ProxyWindow {
actualWindow : CrossDomainWindowType
actualWindowPromise : ZalgoPromise<CrossDomainWindowType>
send : SendType
name : string

constructor(serializedWindow : SerializedProxyWindow, actualWindow? : ?CrossDomainWindowType, { send } : { send : SendType }) {
this.serializedWindow = serializedWindow;
Expand All @@ -93,7 +106,6 @@ export class ProxyWindow {
if (actualWindow) {
this.setWindow(actualWindow);
}
this.serializedWindow.getInstanceID = memoizePromise(this.serializedWindow.getInstanceID);
}

getType() : $Values<typeof WINDOW_TYPE> {
Expand Down Expand Up @@ -121,7 +133,16 @@ export class ProxyWindow {
}

focus() : ZalgoPromise<ProxyWindow> {
return this.serializedWindow.focus().then(() => this);
return ZalgoPromise.try(() => {
return ZalgoPromise.all([
this.isPopup() && this.serializedWindow.getName().then(name => {
if (name) {
window.open('', name);
}
}),
this.serializedWindow.focus()
]);
}).then(() => this);
}

isClosed() : ZalgoPromise<boolean> {
Expand Down

0 comments on commit f7c3f8e

Please # to comment.