Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepnume committed Nov 25, 2018
1 parent 808b350 commit 0567410
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions src/serialize/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ProxyWindow {
// $FlowFixMe
this.actualWindow.name = name;
// $FlowFixMe
if (this.actualWindow.frameElement) {
if (this.actualWindow.c) {
// $FlowFixMe
this.actualWindow.frameElement.setAttribute('name', name);
}
Expand Down Expand Up @@ -131,51 +131,61 @@ export class ProxyWindow {
}
}

serialize() : SerializedProxyWindow {
return this.serializedWindow;
}

static serialize(win : CrossDomainWindowType | ProxyWindow) : SerializedProxyWindow {
if (ProxyWindow.isProxyWindow(win)) {
// $FlowFixMe
return win.serialize();
}

return {
serializedID: uniqueID(),
getInstanceID: () => getWindowInstanceID(win),
close: () => ZalgoPromise.try(() => {
win.close();
}),
focus: () => ZalgoPromise.try(() => {
win.focus();
}),
setLocation: (href) => ZalgoPromise.try(() => {
// $FlowFixMe
win.location = href;
}),
setName: (name) => ZalgoPromise.try(() => {
// $FlowFixMe
win.name = name;
})
};
}

static deserialize(serializedWindow : SerializedProxyWindow) : ProxyWindow {
return new ProxyWindow(serializedWindow);
}

static isProxyWindow(obj : mixed) : boolean {
return obj instanceof ProxyWindow;
}

static toProxyWindow(win : CrossDomainWindowType) : ProxyWindow {
return ProxyWindow.deserialize(ProxyWindow.serialize(win));
}
}

export type SerializedWindow = CustomSerializedType<typeof SERIALIZATION_TYPE.CROSS_DOMAIN_WINDOW, SerializedProxyWindow>;

global.serializedWindows = global.serializedWindows || new WeakMap();

export function serializeWindow(destination : CrossDomainWindowType, domain : string | Array<string>, win : CrossDomainWindowType) : SerializedWindow {
return global.serializedWindows.getOrSet(win, () => {
return serializeType(SERIALIZATION_TYPE.CROSS_DOMAIN_WINDOW, ProxyWindow.isProxyWindow(win)
? {
// $FlowFixMe
serializedID: win.getSerializedID(),
// $FlowFixMe
getInstanceID: () => win.getInstanceID(),
close: () => win.close(),
focus: () => win.focus(),
// $FlowFixMe
setLocation: (href) => win.setLocation(href),
// $FlowFixMe
setName: (name) => win.setName(name)
}
: {
serializedID: uniqueID(),
getInstanceID: () => getWindowInstanceID(win),
close: () => ZalgoPromise.try(() => win.close()),
focus: () => ZalgoPromise.try(() => win.focus()),
setLocation: (href) => ZalgoPromise.try(() => {
win.location = href;
}),
setName: (name) => ZalgoPromise.try(() => {
// $FlowFixMe
win.name = name;
})
}
);
});
return global.serializedWindows.getOrSet(win, () =>
serializeType(SERIALIZATION_TYPE.CROSS_DOMAIN_WINDOW, ProxyWindow.serialize(win))
);
}

global.deseserializedWindows = {};

export function deserializeWindow(source : CrossDomainWindowType, origin : string, win : SerializedProxyWindow) : ProxyWindow {
return getOrSet(global.deseserializedWindows, win.serializedID, () => {
return new ProxyWindow(win);
});
return getOrSet(global.deseserializedWindows, win.serializedID, () => ProxyWindow.deserialize(win));
}

0 comments on commit 0567410

Please # to comment.