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

Commit 57ac6cb

Browse files
author
Daniel Brain
committed
Simplify window serialization
1 parent ecb357e commit 57ac6cb

File tree

1 file changed

+61
-99
lines changed

1 file changed

+61
-99
lines changed

src/serialize/window.js

+61-99
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

3-
import { isSameDomain, isWindowClosed, type CrossDomainWindowType, type DomainMatcher, getOpener, WINDOW_TYPE, isWindow } from 'cross-domain-utils/src';
3+
import { isSameDomain, isWindowClosed, type CrossDomainWindowType,
4+
type DomainMatcher, getOpener, WINDOW_TYPE, isWindow, assertSameDomain } from 'cross-domain-utils/src';
45
import { ZalgoPromise } from 'zalgo-promise/src';
56
import { uniqueID, memoizePromise } from 'belter/src';
67
import { serializeType, type CustomSerializedType } from 'universal-serialize/src';
@@ -32,6 +33,51 @@ type SerializedProxyWindow = {|
3233
getInstanceID : () => ZalgoPromise<string>
3334
|};
3435

36+
function getSerializedWindow(id : string, win : CrossDomainWindowType, { send } : { send : SendType }) : SerializedProxyWindow {
37+
return {
38+
id,
39+
type: getOpener(win) ? WINDOW_TYPE.POPUP : WINDOW_TYPE.IFRAME,
40+
getInstanceID: () => getWindowInstanceID(win, { send }),
41+
close: () => ZalgoPromise.try(() => {
42+
win.close();
43+
}),
44+
focus: () => ZalgoPromise.try(() => {
45+
win.focus();
46+
}),
47+
isClosed: () => ZalgoPromise.try(() => {
48+
return isWindowClosed(win);
49+
}),
50+
setLocation: (href) => ZalgoPromise.try(() => {
51+
if (isSameDomain(win)) {
52+
try {
53+
if (win.location && typeof win.location.replace === 'function') {
54+
// $FlowFixMe
55+
win.location.replace(href);
56+
return;
57+
}
58+
} catch (err) {
59+
// pass
60+
}
61+
}
62+
63+
win.location = href;
64+
}),
65+
setName: (name) => ZalgoPromise.try(() => {
66+
if (__POST_ROBOT__.__IE_POPUP_SUPPORT__) {
67+
linkWindow({ win, name });
68+
}
69+
70+
win = assertSameDomain(win);
71+
72+
win.name = name;
73+
74+
if (win.frameElement) {
75+
win.frameElement.setAttribute('name', name);
76+
}
77+
})
78+
};
79+
}
80+
3581
export class ProxyWindow {
3682

3783
isProxyWindow : true = true
@@ -43,11 +89,11 @@ export class ProxyWindow {
4389
constructor(serializedWindow : SerializedProxyWindow, actualWindow? : ?CrossDomainWindowType, { send } : { send : SendType }) {
4490
this.serializedWindow = serializedWindow;
4591
this.actualWindowPromise = new ZalgoPromise();
92+
this.send = send;
4693
if (actualWindow) {
4794
this.setWindow(actualWindow);
4895
}
4996
this.serializedWindow.getInstanceID = memoizePromise(this.serializedWindow.getInstanceID);
50-
this.send = send;
5197
}
5298

5399
getType() : $Values<typeof WINDOW_TYPE> {
@@ -63,67 +109,23 @@ export class ProxyWindow {
63109
}
64110

65111
setLocation(href : string) : ZalgoPromise<ProxyWindow> {
66-
return ZalgoPromise.try(() => {
67-
if (this.actualWindow) {
68-
this.actualWindow.location = href;
69-
} else {
70-
return this.serializedWindow.setLocation(href);
71-
}
72-
}).then(() => this);
112+
return this.serializedWindow.setLocation(href).then(() => this);
73113
}
74114

75115
setName(name : string) : ZalgoPromise<ProxyWindow> {
76-
return ZalgoPromise.try(() => {
77-
if (this.actualWindow) {
78-
if (!isSameDomain(this.actualWindow)) {
79-
throw new Error(`Can not set name for window on different domain`);
80-
}
81-
// $FlowFixMe
82-
this.actualWindow.name = name;
83-
// $FlowFixMe
84-
if (this.actualWindow.frameElement) {
85-
// $FlowFixMe
86-
this.actualWindow.frameElement.setAttribute('name', name);
87-
}
88-
89-
if (__POST_ROBOT__.__IE_POPUP_SUPPORT__) {
90-
91-
linkWindow({ win: this.actualWindow, name });
92-
}
93-
94-
} else {
95-
return this.serializedWindow.setName(name);
96-
}
97-
}).then(() => this);
116+
return this.serializedWindow.setName(name).then(() => this);
98117
}
99118

100119
close() : ZalgoPromise<ProxyWindow> {
101-
return ZalgoPromise.try(() => {
102-
if (this.actualWindow) {
103-
this.actualWindow.close();
104-
} else {
105-
return this.serializedWindow.close();
106-
}
107-
}).then(() => this);
120+
return this.serializedWindow.close().then(() => this);
108121
}
109122

110123
focus() : ZalgoPromise<ProxyWindow> {
111-
return ZalgoPromise.try(() => {
112-
if (this.actualWindow) {
113-
this.actualWindow.focus();
114-
}
115-
return this.serializedWindow.focus();
116-
}).then(() => this);
124+
return this.serializedWindow.focus().then(() => this);
117125
}
118126

119127
isClosed() : ZalgoPromise<boolean> {
120-
return ZalgoPromise.try(() => {
121-
if (this.actualWindow) {
122-
return isWindowClosed(this.actualWindow);
123-
} else {
124-
return this.serializedWindow.isClosed();
125-
}
126-
});
128+
return this.serializedWindow.isClosed();
127129
}
128130

129131
getWindow() : ?CrossDomainWindowType {
@@ -132,6 +134,7 @@ export class ProxyWindow {
132134

133135
setWindow(win : CrossDomainWindowType) {
134136
this.actualWindow = win;
137+
this.serializedWindow = getSerializedWindow(this.serializedWindow.id, win, { send: this.send });
135138
this.actualWindowPromise.resolve(win);
136139
}
137140

@@ -165,11 +168,7 @@ export class ProxyWindow {
165168
}
166169

167170
getInstanceID() : ZalgoPromise<string> {
168-
if (this.actualWindow) {
169-
return getWindowInstanceID(this.actualWindow, { send: this.send });
170-
} else {
171-
return this.serializedWindow.getInstanceID();
172-
}
171+
return this.serializedWindow.getInstanceID();
173172
}
174173

175174
serialize() : SerializedProxyWindow {
@@ -214,53 +213,16 @@ export class ProxyWindow {
214213
return win;
215214
}
216215

216+
// $FlowFixMe
217+
const realWin : CrossDomainWindowType = win;
218+
217219
// $FlowFixMe
218220
return windowStore('winToProxyWindow').getOrSet(win, () => {
219221
const id = uniqueID();
222+
const serializedWindow = getSerializedWindow(id, realWin, { send });
223+
const proxyWindow = new ProxyWindow(serializedWindow, realWin, { send });
220224

221-
return globalStore('idToProxyWindow').set(id, new ProxyWindow({
222-
id,
223-
// $FlowFixMe
224-
type: getOpener(win) ? WINDOW_TYPE.POPUP : WINDOW_TYPE.IFRAME,
225-
// $FlowFixMe
226-
getInstanceID: () => getWindowInstanceID(win, { send }),
227-
close: () => ZalgoPromise.try(() => {
228-
win.close();
229-
}),
230-
focus: () => ZalgoPromise.try(() => {
231-
win.focus();
232-
}),
233-
isClosed: () => ZalgoPromise.try(() => {
234-
// $FlowFixMe
235-
return isWindowClosed(win);
236-
}),
237-
setLocation: (href) => ZalgoPromise.try(() => {
238-
// $FlowFixMe
239-
if (isSameDomain(win)) {
240-
try {
241-
if (win.location && typeof win.location.replace === 'function') {
242-
// $FlowFixMe
243-
win.location.replace(href);
244-
return;
245-
}
246-
} catch (err) {
247-
// pass
248-
}
249-
}
250-
251-
// $FlowFixMe
252-
win.location = href;
253-
}),
254-
setName: (name) => ZalgoPromise.try(() => {
255-
if (__POST_ROBOT__.__IE_POPUP_SUPPORT__) {
256-
// $FlowFixMe
257-
linkWindow({ win, name });
258-
}
259-
// $FlowFixMe
260-
win.name = name;
261-
})
262-
// $FlowFixMe
263-
}, win, { send }));
225+
return globalStore('idToProxyWindow').set(id, proxyWindow);
264226
});
265227
}
266228
}

0 commit comments

Comments
 (0)